1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| class FRAbsLoop extends AbsLoop { AFR_FSDKVersion version = new AFR_FSDKVersion(); AFR_FSDKEngine engine = new AFR_FSDKEngine(); AFR_FSDKFace result = new AFR_FSDKFace(); List<FaceDB.FaceRegist> mResgist = ((Application)DetecterActivity.this.getApplicationContext()).mFaceDB.mRegister; List<ASAE_FSDKFace> face1 = new ArrayList<>(); List<ASGE_FSDKFace> face2 = new ArrayList<>(); @Override public void setup() { AFR_FSDKError error = engine.AFR_FSDK_InitialEngine(FaceDB.appid, FaceDB.fr_key); Log.d(TAG, "AFR_FSDK_InitialEngine = " + error.getCode()); error = engine.AFR_FSDK_GetVersion(version); Log.d(TAG, "FR=" + version.toString() + "," + error.getCode()); } @Override public void loop() { if (mImageNV21 != null) { final int rotate = mCameraRotate; long time = System.currentTimeMillis(); AFR_FSDKError error = engine.AFR_FSDK_ExtractFRFeature(mImageNV21, mWidth, mHeight, AFR_FSDKEngine.CP_PAF_NV21, mAFT_FSDKFace.getRect(), mAFT_FSDKFace.getDegree(), result); Log.d(TAG, "AFR_FSDK_ExtractFRFeature cost :" + (System.currentTimeMillis() - time) + "ms"); Log.d(TAG, "Face=" + result.getFeatureData()[0] + "," + result.getFeatureData()[1] + "," + result.getFeatureData()[2] + "," + error.getCode()); AFR_FSDKMatching score = new AFR_FSDKMatching(); float max = 0.0f; String name = null; for (FaceDB.FaceRegist fr : mResgist) { for (AFR_FSDKFace face : fr.mFaceList) { error = engine.AFR_FSDK_FacePairMatching(result, face, score); Log.d(TAG, "Score:" + score.getScore() + ", AFR_FSDK_FacePairMatching=" + error.getCode()); if (max < score.getScore()) { max = score.getScore(); name = fr.mName; } } } face1.clear(); face2.clear(); face1.add(new ASAE_FSDKFace(mAFT_FSDKFace.getRect(), mAFT_FSDKFace.getDegree())); face2.add(new ASGE_FSDKFace(mAFT_FSDKFace.getRect(), mAFT_FSDKFace.getDegree())); ASAE_FSDKError error1 = mAgeEngine.ASAE_FSDK_AgeEstimation_Image(mImageNV21, mWidth, mHeight, AFT_FSDKEngine.CP_PAF_NV21, face1, ages); ASGE_FSDKError error2 = mGenderEngine.ASGE_FSDK_GenderEstimation_Image(mImageNV21, mWidth, mHeight, AFT_FSDKEngine.CP_PAF_NV21, face2, genders); Log.d(TAG, "ASAE_FSDK_AgeEstimation_Image:" + error1.getCode() + ",ASGE_FSDK_GenderEstimation_Image:" + error2.getCode()); Log.d(TAG, "age:" + ages.get(0).getAge() + ",gender:" + genders.get(0).getGender()); final String age = ages.get(0).getAge() == 0 ? "年龄未知" : ages.get(0).getAge() + "岁"; final String gender = genders.get(0).getGender() == -1 ? "性别未知" : (genders.get(0).getGender() == 0 ? "男" : "女"); byte[] data = mImageNV21; YuvImage yuv = new YuvImage(data, ImageFormat.NV21, mWidth, mHeight, null); ExtByteArrayOutputStream ops = new ExtByteArrayOutputStream(); yuv.compressToJpeg(mAFT_FSDKFace.getRect(), 80, ops); final Bitmap bmp = BitmapFactory.decodeByteArray(ops.getByteArray(), 0, ops.getByteArray().length); try { ops.close(); } catch (IOException e) { e.printStackTrace(); } if (max > 0.6f) { final float max_score = max; Log.d(TAG, "fit Score:" + max + ", NAME:" + name); final String mNameShow = name; mHandler.removeCallbacks(hide); mHandler.post(new Runnable() { @Override public void run() { mTextView.setAlpha(1.0f); mTextView.setText(mNameShow); mTextView.setTextColor(Color.RED); mTextView1.setVisibility(View.VISIBLE); mTextView1.setText("置信度:" + (float)((int)(max_score * 1000)) / 1000.0); mTextView1.setTextColor(Color.RED); mImageView.setRotation(rotate); if (mCameraMirror) { mImageView.setScaleY(-1); } mImageView.setImageAlpha(255); mImageView.setImageBitmap(bmp); } }); } else { final String mNameShow = "未识别"; DetecterActivity.this.runOnUiThread(new Runnable() { @Override public void run() { mTextView.setAlpha(1.0f); mTextView1.setVisibility(View.VISIBLE); mTextView1.setText( gender + "," + age); mTextView1.setTextColor(Color.RED); mTextView.setText(mNameShow); mTextView.setTextColor(Color.RED); mImageView.setImageAlpha(255); mImageView.setRotation(rotate); if (mCameraMirror) { mImageView.setScaleY(-1); } mImageView.setImageBitmap(bmp); } }); } mImageNV21 = null; } } @Override public void over() { AFR_FSDKError error = engine.AFR_FSDK_UninitialEngine(); Log.d(TAG, "AFR_FSDK_UninitialEngine : " + error.getCode()); } }
|