虹软人脸识别sdk使用-android(一)
生活随笔
收集整理的這篇文章主要介紹了
虹软人脸识别sdk使用-android(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android+Camera+SurfaceView自定義相機系列在這篇文章中已經介紹了如何使用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,maxFaceNum, initMask);
int code_init = faceEngine.init(getApplicationContext(), DetectMode.ASF_DETECT_MODE_VIDEO, DetectFaceOrientPriority.ASF_OP_ALL_OUT, scale, maxFaceNum, initMask);
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 = CameraUtils.getmCamera();
camera.setDisplayOrientation(90);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewFormat(ImageFormat.NV21);
camera.setParameters(parameters);
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] nv21, Camera camera) {
// 這里面的Bytes的數據就是NV21格式的數據
previewSize = camera.getParameters().getPreviewSize();
/*
* 顯示人臉個數
* */
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 = faceEngine.getAge(ageInfoList);
// 獲取第一個人臉的年齡信息,多人臉情況進行循環即可
if (ageCode == ErrorInfo.MOK && ageInfoList.size() > 0) {
if (ageInfoList.size() > 0) {
Log.i(TAG, "age of the first face is : " + ageInfoList.get(0).getAge());
// age.setText("年齡:"+ageInfoList.get(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("年齡:" + ageInfoList.get(0).getAge());
}
if (ageInfoList.size() == 2){
age.setText("年齡:"+ageInfoList.get(0).getAge()+" ,"+ageInfoList.get(1).getAge());
}
if (ageInfoList.size() == 0) {
age.setText("年齡:--");
}
/*
* 顯示性別
* */
List<GenderInfo> genderInfoList = new ArrayList<>();
int genderCode = faceEngine.getGender(genderInfoList);
// 獲取第一個人臉的性別信息,多人臉情況進行循環即可
if (genderCode == ErrorInfo.MOK) {
if (genderInfoList.size() > 0) {
Log.i(TAG, "gender of the first face is : " +
genderInfoList.get(0).getGender());
} else {
Log.i(TAG, "no face processed");
}
} else {
Log.i(TAG, "get gender failed, code is : " + genderCode);
}
if (genderInfoList.size() > 0 && genderInfoList.get(0).getGender() == 0) {
gender.setText("年齡:M");
}
if (genderInfoList.size() > 0 && genderInfoList.get(0).getGender() == 1) {
gender.setText("年齡:F");
}
if (genderInfoList.size() == 2 ){
gender.setText("年齡:"+gen(genderInfoList.get(0).getGender())+gen(genderInfoList.get(1).getGender()));
}
if (genderInfoList.size() == 0) {
gender.setText("年齡:--");
}
/*
* 活體
* */
List<LivenessInfo> livenessInfoList = new ArrayList<>();
int livenessCode = faceEngine.getLiveness(livenessInfoList);
// RGB活體不支持多人臉,因此只能拿第1個活體信息
if (livenessCode == ErrorInfo.MOK) {
if (livenessInfoList.size() > 0) {
Log.i(TAG, "liveness of the first face is : " +
livenessInfoList.get(0).getLiveness());
} else {
Log.i(TAG, "no face processed");
}
} else {
Log.i(TAG, "get liveness failed, code is : " + livenessCode);
}
if (livenessInfoList.size() > 0 && livenessInfoList.get(0).getLiveness() == 1) {
liveness.setText("活體:Y");
} else {
liveness.setText("活體:--");
}
if (livenessInfoList.size() == 2 ){
liveness.setText("活體:"+Live(livenessInfoList.get(0).getLiveness())+Live(livenessInfoList.get(1).getLiveness()));
}
View Code
所以寫完這些就完成了檢測人臉數,年齡等。在這里我們先是使用textView的方式將這些信息顯示在屏幕上。后面我們在移植虹軟的人臉識別框,就可以去掉這些代碼;
4、布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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>
<com.example.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
android:id="@+id/btn_init"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="初始化" />
<Button
android:id="@+id/btn_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切換相機" />
<Button
android:id="@+id/btn_focus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="focus"
android:text="聚焦" />
<Button
android:id="@+id/btn_facerect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="facerect"
android:text="人臉框" />
</LinearLayout>
</FrameLayout>
View Code
我們在點擊初始化的按鈕的時候調用init()函數。就可以了
這些就是我們需要的虹軟的人臉識別SDK的基礎功能。完整的代碼下載鏈接:https://download.csdn.net/download/hequnwang10/14150596
總結
以上是生活随笔為你收集整理的虹软人脸识别sdk使用-android(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS ID选择器&通配选择器
- 下一篇: 大数据基本概念介绍