虹软⼈脸识别sdk使⽤-android(⼀)
在这篇⽂章中已经介绍了如何使⽤camera+surfaceview
在此基础上我们介绍如何接⼊虹软的⼈脸识别功能。⾸先⾃⼰去注册虹软的开发者账号,并⾃⾏下载SDK,SDK的APP_ID和SDK_KEY是唯⼀的,可以激活⼀台设备,完成认证后,可以⽤于激活100台设备。
⼀、SDK接⼊配置
1、在官⽅⽂档中可以看到主要将⼏个jar包和⼏个so⽂件接⼊到你的项⽬⽬录下。完成这步就算完成了接⼊SDK。
2、激活引擎
我们在⾃定义的camera的项⽬中,将激活与初始化封装起来,重新写init()函数
private void init() {
/*
* 激活
* */
String APP_ID = "换成⾃⼰的ID";
String SDK_KEY = "换成⾃⼰的key";
int code = FaceEngine.activeOnline(MainActivity_l.this, APP_ID, SDK_KEY);
if (code == ErrorInfo.MOK) {
Log.i(TAG, "activeOnline success");
Toast.makeText(MainActivity_l.this, "激活成功", Toast.LENGTH_SHORT).show();
} else if (code == ErrorInfo.MERR_ASF_ALREADY_ACTIVATED) {
Log.i(TAG, "already activated");
Toast.makeText(MainActivity_l.this, "已经激活", Toast.LENGTH_SHORT).show();
} else {
Log.i(TAG, "activeOnline failed, code is : " + code);
}
/*
* 初始化
* */
faceEngine = new FaceEngine();
/
/ int code_init = faceEngine.init(getApplicationContext(),DetectMode.ASF_DETECT_MODE_VIDEO, DetectFaceOrientPriority.ASF_OP_0_ONLY, scale,maxF int code_init = faceEngine.init(getApplicationContext(), DetectMode.ASF_DETECT_MODE_VIDEO, DetectFaceOrientPriority.ASF_OP_ALL_OUT, scale, max if (code_init != ErrorInfo.MOK) {
Toast.makeText(this, "init failed, code is : " + code,
Toast.LENGTH_SHORT).show();
} else {
Log.i(TAG, "init success");
Toast.makeText(MainActivity_l.this, "初始化成功", Toast.LENGTH_SHORT).show();
}
}
View Code
这样我们就完成了如何激活与初始化
3、属性检测
我们使⽤虹软SDK主要是使⽤他进⾏⼈脸追踪和⼈脸识别等功能,⽐如画⼈脸框,显⽰性别,年龄,⼈脸数,是否活体等功能,所以我们继
续在init()添加功能
/*
* 检测属性
* */
Camera camera = Camera();
camera.setDisplayOrientation(90);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final Camera.Parameters parameters = Parameters();
parameters.setPreviewFormat(ImageFormat.NV21);
camera.setParameters(parameters);
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] nv21, Camera camera) {
// 这⾥⾯的Bytes的数据就是NV21格式的数据
previewSize = Parameters().getPreviewSize();
/*
* 显⽰⼈脸个数
* */
faceInfoList = new ArrayList<>();
faceInfoList = new ArrayList<>();
// long start = System.currentTimeMillis();
int code = faceEngine.detectFaces(nv21, previewSize.width, previewSize.height,
FaceEngine.CP_PAF_NV21, faceInfoList);
if (code == ErrorInfo.MOK && faceInfoList.size() > 0) {
Log.i(TAG, "detectFaces, face num is : " + faceInfoList.size());
// Toast.makeText(MainActivity.this,"检测成功",Toast.LENGTH_SHORT).show();
} else {
Log.i(TAG, "no face detected, code is : " + code);
// Toast.makeText(MainActivity.this,"检测失败",Toast.LENGTH_SHORT).show();
}
face_num.setText("⼈脸数:" + faceInfoList.size());
/*
* 显⽰年龄
* */
processMask = FaceEngine.ASF_AGE | FaceEngine.ASF_GENDER | FaceEngine.ASF_FACE3DANGLE | FaceEngine.ASF_LIVENESS; int faceProcessCode = faceEngine.process(nv21, previewSize.width, previewSize.height,
FaceEngine.CP_PAF_NV21, faceInfoList, processMask);
if (faceProcessCode == ErrorInfo.MOK) {
Log.i(TAG, "process success");
// Toast.makeText(MainActivity.this,"属性成功",Toast.LENGTH_SHORT).show();
} else {
Log.i(TAG, "process failed, code is : " + faceProcessCode);
// Toast.makeText(MainActivity.this,"属性失败",Toast.LENGTH_SHORT).show();
}
List<AgeInfo> ageInfoList = new ArrayList<>();
int ageCode = Age(ageInfoList);
// 获取第⼀个⼈脸的年龄信息,多⼈脸情况进⾏循环即可
if (ageCode == ErrorInfo.MOK && ageInfoList.size() > 0) {
if (ageInfoList.size() > 0) {
Log.i(TAG, "age of the first face is : " + (0).getAge());
// age.setText("年龄:"+(0).getAge());
} else {
Log.i(TAG, "no face processed");
// age.setText("年龄:--");
}
} else {
Log.i(TAG, "get age failed, code is : " + ageCode);
}
if (ageInfoList.size() == 1) {
age.setText("年龄:" + (0).getAge());
}
if (ageInfoList.size() == 2){
age.setText("年龄:"+(0).getAge()+" ,"+(1).getAge());
}
if (ageInfoList.size() == 0) {
age.setText("年龄:--");
}
/*
* 显⽰性别
* */
List<GenderInfo> genderInfoList = new ArrayList<>();
int genderCode = Gender(genderInfoList);
// 获取第⼀个⼈脸的性别信息,多⼈脸情况进⾏循环即可
if (genderCode == ErrorInfo.MOK) {
if (genderInfoList.size() > 0) {
Log.i(TAG, "gender of the first face is : " +
Log.i(TAG, "gender of the first face is : " +
<(0).getGender());
} else {
Log.i(TAG, "no face processed");
}
} else {
Log.i(TAG, "get gender failed, code is : " + genderCode);
}
if (genderInfoList.size() > 0 && (0).getGender() == 0) {
gender.setText("年龄:M");
}
if (genderInfoList.size() > 0 && (0).getGender() == 1) {
gender.setText("年龄:F");
}
if (genderInfoList.size() == 2 ){
gender.setText("年龄:"+(0).getGender())+(1).getGender()));
}
if (genderInfoList.size() == 0) {
gender.setText("年龄:--");
}
/*
* 活体
* */
List<LivenessInfo> livenessInfoList = new ArrayList<>();
int livenessCode = Liveness(livenessInfoList);
// RGB活体不⽀持多⼈脸,因此只能拿第1个活体信息
if (livenessCode == ErrorInfo.MOK) {
if (livenessInfoList.size() > 0) {
Log.i(TAG, "liveness of the first face is : " +
<(0).getLiveness());
} else {
Log.i(TAG, "no face processed");
}
} else {
Log.i(TAG, "get liveness failed, code is : " + livenessCode);
}
if (livenessInfoList.size() > 0 && (0).getLiveness() == 1) {
sdkliveness.setText("活体:Y");
} else {
liveness.setText("活体:--");
}
if (livenessInfoList.size() == 2 ){
liveness.setText("活体:"+(0).getLiveness())+(1).getLiveness()));
}
View Code
所以写完这些就完成了检测⼈脸数,年龄等。在这⾥我们先是使⽤textView的⽅式将这些信息显⽰在屏幕上。后⾯我们在移植虹软的⼈脸识别框,就可以去掉这些代码;
4、布局⽂件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity_l">
<FrameLayout
android:id="@+id/layout_aspect"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
&le.camera.widget.FaceRectView android:id="@+id/face_rect_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="top"
android:gravity="center">
<TextView
android:id="@+id/face_num"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="⼈脸数:--"
android:textColor="#000000"
/
>
<TextView
android:id="@+id/age"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="@id/face_num" android:text="年龄:--"
android:textColor="#000000"
/>
<TextView
android:id="@+id/gender"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="@id/age"
android:text="性别:--"
android:textColor="#000000"
/>
<TextView
android:id="@+id/liveness"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="@id/gender" android:text="活体:--"
android:textColor="#000000"
/
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:gravity="center">
<Button
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论