ArcFace人脸识别SDK
申请APPKEY及秘钥
1. 首先就是去官网申请APPKEY,各种密匙,然后在下载jar包,这些就不一一给大家讲解了。注意一下,要在app的gradle里面加上这句话,不然可能会造成so库加载不了的错误。 sourceSets { main { jniLibs.srcDirs = ['libs'] } }
2.接下里就需要进行开发了。就拿人脸检测的功能来说吧,首先需要 对引擎初始化:AFD_FSDKEngine engine1 = new AFD_FSDKEngine(); AFD_FSDKError err = engine1.AFD_FSDK_InitialFaceEngine(Config.APP_ID, Config.FD_KEY, AFD_FSDKEngine.AFD_OPF_0_HIGHER_EXT, 16, 5);`我们还需要一个集合,用来存放我们检测到的人脸: List
下面是工具类decodeImage和getNv21的代码 //getNv21 和 decodeImage 是照片格式的转化工具 public byte[] getNv21(Bitmap mBitmap) { byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight() * 3 / 2]; ImageConverter convert = new ImageConverter(); convert.initial(mBitmap.getWidth(), mBitmap.getHeight(), ImageConverter.CP_PAF_NV21); if (convert.convert(mBitmap, data)) { Log.e('TAG', 'convert ok!'); } convert.destroy(); return data; } public static Bitmap decodeImage(String path) { Bitmap res; try { ExifInterface exif = new ExifInterface(path); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); BitmapFactory.Options op = new BitmapFactory.Options(); op.inSampleSize = 1; op.inJustDecodeBounds = false; //op.inMutable = true; res = BitmapFactory.decodeFile(path, op); //rotate and scale. Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } Bitmap temp = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), matrix, true); Log.d('com.arcsoft', 'check target Image:' + temp.getWidth() + 'X' + temp.getHeight()); if (!temp.equals(res)) { res.recycle(); } return temp; } catch (Exception e) { e.printStackTrace(); } return null; }
对格式进行转化完成后,就开始进行人脸的检测了。 err = engine1.AFD_FSDK_StillImageFaceDetection(data1, bitmap1.getWidth(), bitmap1.getHeight(), AFD_FSDKEngine.CP_PAF_NV21, result); Log.e('TAG', 'getBit: ' + result.size());我们可以查看集合result的size,来确定是否检测到人脸。在代码的最后,一定要对初始化的引擎进行销毁处理。不然程序会因为内存问题而崩溃。 ` engine1.AFD_FSDK_UninitialFaceEngine();
人脸对比是在人脸检测的基础上进行的,在一张照片上先检测到人脸的信息,然后再将人脸的信息进行比对。 List result = new ArrayList(); 上面已经介绍了,检测到的人脸信息都是存放在result的集合中的, 然后是创建两个存放人脸点位信息的类 AFR_FSDKFace face1 = new AFR_FSDKFace();AFR_FSDKFace face2 = new AFR_FSDKFace(); 将检测到的人脸信息的点位信息存放到 face类中 //新建两个AFR_FSDKFace类,保存人脸特征信息 AFR_FSDKFace face1 = new AFR_FSDKFace(); AFR_FSDKFace face2 = new AFR_FSDKFace(); //对人脸特征信息的检测 er = engine_camera.AFR_FSDK_ExtractFRFeature(data_image, bitmap_idcard.getWidth(), bitmap_idcard.getHeight(), AFR_FSDKEngine.CP_PAF_NV21, new Rect(result_image.get(0).getRect()), result_image.get(0).getDegree(), face1); er = engine_camera.AFR_FSDK_ExtractFRFeature(data, wid, hei, AFR_FSDKEngine.CP_PAF_NV21, new Rect(result_fd.get(0).getRect()), result_fd.get(0).getDegree(), face2); 最后的比对的相似度信息存放在score中, float score_face = score.getScore();
我们可以通过这种方式得到 我们想要的相似度信息,最后得到的数据是float类型的。到此虹软的基本使用流程就是这样了,大家可以看一下,比对一下,根据自己的需求来进行选择。