虹软 最近开放了人脸识别的SDK引擎(免费的哦),刚好有Android版的,就体验了一波。下面来说说Android版的SDK使用心得:
在官网下载SDK引擎后集成到你的项目中,然后我在此举例说下其中一个sample:人脸识别
官方sample是这样的:
AFR_FSDKInterface engine = new AFR_FSDKEngine();
//用来存放提取到的人脸信息, face_1 是注册的人脸,face_2 是要识别的人脸
AFR_FSDKFace face1 = new AFR_FSDKFace();
AFR_FSDKFace face2 = new AFR_FSDKFace();
//初始化人脸识别引擎,使用时请替换申请的 APPID 和 SDKKEY
AFR_FSDKError error = engine.AFR_FSDK_InitialEngine("APPID", "SDKKEY");
Log.d("com.arcsoft", "AFR_FSDK_InitialEngine = " + error.getCode());
//输入的 data 数据为 NV21 格式(如 Camera 里 NV21 格式的 preview 数据);人脸坐标一般使用人脸检测返回的 Rect 传入;人脸角度请按照人脸检测引擎返回的值传入。
error = engine.AFR_FSDK_ExtractFRFeature(data1, width, height, AFR_FSDKEngine.CP_PAF_NV21, new Rect(210, 178, 478, 446), AFR_FSDKEngine.AFR_FOC_0, face1);
Log.d("com.arcsoft", "Face=" + face1.getFeatureData()[0]+ "," + face1.getFeatureData()[1] + "," + face1.getFeatureData()[2] + "," + error.getCode());
error = engine.AFR_FSDK_ExtractFRFeature(data1, width, height, AFR_FSDKEngine.CP_PAF_NV21, new Rect(210, 170, 470, 440), AFR_FSDKEngine.AFR_FOC_0, face2);
Log.d("com.arcsoft", "Face=" + face2.getFeatureData()[0]+ "," + face2.getFeatureData()[1] + "," + face2.getFeatureData()[2] + "," + error.getCode());
//score 用于存放人脸对比的相似度值
AFR_FSDKMatching score = new AFR_FSDKMatching(); error = engine.AFR_FSDK_FacePairMatching(face1, face2, score); Log.d("com.arcsoft", "AFR_FSDK_FacePairMatching=" + error.getCode()); Log.d("com.arcsoft", "Score:" + score.getScore());
//销毁人脸识别引擎
error = engine.AFR_FSDK_UninitialEngine();
Log.d("com.arcsoft", "AFR_FSDK_UninitialEngine : " + error.getCode());
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
//这里的data数据就是NV21格式,可以在这里处理人脸检测
}
});
注意:Camera里的PictureCallback(),也就是我们拍照的回调,里面也有个data,但是这里的data格式不是NV21的,所以在这里传入的data是不能处理人脸识别的
人脸检测还可以利用已有的图像进行检测,但是注意,Android里对图像处理的接口最常用的是BitMap吧。我们需要把BitMap解码为RGB转为NV21才能检测图像中的人脸。这里提供一种转换工具类,当然大神可以无视这个自己写哈:
public static byte[] getNV21(int inputWidth, int inputHeight, Bitmap scaled) throws Exception {
int[] argb = new int[inputWidth * inputHeight];
scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
scaled.recycle();
return yuv;
}
public static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) throws Exception {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}