Android虹软人脸识别sdk使用工具类
public class FaceUtil
{
private static final String TAG = FaceUtil.class.getSimpleName();
private static FaceUtil faceInstance = null;
public FaceDB mFaceDB;
private AFR_FSDKFace mAFR_FSDKFace;
private static Context mContext = null; //face detect
private AFT_FSDKVersion version = new AFT_FSDKVersion();
private AFT_FSDKEngine engine = new AFT_FSDKEngine();
private ASAE_FSDKVersion mAgeVersion = new ASAE_FSDKVersion();
private ASAE_FSDKEngine mAgeEngine = new ASAE_FSDKEngine();
private ASGE_FSDKVersion mGenderVersion = new ASGE_FSDKVersion();
private ASGE_FSDKEngine mGenderEngine = new ASGE_FSDKEngine();
private List<AFT_FSDKFace> result = new ArrayList<>();
private List<ASAE_FSDKAge> ages = new ArrayList<>();
private List<ASGE_FSDKGender> genders = new ArrayList<>();
private AFT_FSDKFace mAFT_FSDKFace = null;
private boolean openFace = false; public static FaceUtil getInstance(Context context)
{
if(faceInstance == null)
{
faceInstance = new FaceUtil(context);
}
return faceInstance;
} public FaceUtil(Context context)
{
mContext = context;
} public void setFaceDB()
{
//face
mFaceDB = new FaceDB(mContext.getExternalCacheDir().getPath());
Log.d(TAG,"getExternalCacheDir : "+mContext.getExternalCacheDir().getPath());
} public void initFaceDetect()
{
AFT_FSDKError err = engine.AFT_FSDK_InitialFaceEngine(FaceDB.appid, FaceDB.ft_key, AFT_FSDKEngine.AFT_OPF_0_HIGHER_EXT, 16, 5);
Log.d(TAG, "AFT_FSDK_InitialFaceEngine =" + err.getCode());
err = engine.AFT_FSDK_GetVersion(version);
Log.d(TAG, "AFT_FSDK_GetVersion:" + version.toString() + "," + err.getCode()); ASAE_FSDKError error = mAgeEngine.ASAE_FSDK_InitAgeEngine(FaceDB.appid, FaceDB.age_key);
Log.d(TAG, "ASAE_FSDK_InitAgeEngine =" + error.getCode());
error = mAgeEngine.ASAE_FSDK_GetVersion(mAgeVersion);
Log.d(TAG, "ASAE_FSDK_GetVersion:" + mAgeVersion.toString() + "," + error.getCode()); ASGE_FSDKError error1 = mGenderEngine.ASGE_FSDK_InitgGenderEngine(FaceDB.appid, FaceDB.gender_key);
Log.d(TAG, "ASGE_FSDK_InitgGenderEngine =" + error1.getCode());
error1 = mGenderEngine.ASGE_FSDK_GetVersion(mGenderVersion);
Log.d(TAG, "ASGE_FSDK_GetVersion:" + mGenderVersion.toString() + "," + error1.getCode()); } public void deInitFaceDetect()
{
AFT_FSDKError err = engine.AFT_FSDK_UninitialFaceEngine();
Log.d(TAG, "AFT_FSDK_UninitialFaceEngine =" + err.getCode()); ASAE_FSDKError err1 = mAgeEngine.ASAE_FSDK_UninitAgeEngine();
Log.d(TAG, "ASAE_FSDK_UninitAgeEngine =" + err1.getCode()); ASGE_FSDKError err2 = mGenderEngine.ASGE_FSDK_UninitGenderEngine();
Log.d(TAG, "ASGE_FSDK_UninitGenderEngine =" + err2.getCode());
} public boolean faceFeatureDetect(byte[] mYUVData,int width,int height)
{
boolean bDectectFace = false;
AFT_FSDKError err =engine.AFT_FSDK_FaceFeatureDetect(mYUVData, width, height, AFT_FSDKEngine.CP_PAF_NV21, result);
Log.d(TAG, "AFT_FSDK_FaceFeatureDetect =" + err.getCode());
Log.d(TAG, "Face=" + result.size()); if (!result.isEmpty())
{
for (AFT_FSDKFace face : result) {
Log.d(TAG, "Face:" + face.toString());
}
mAFT_FSDKFace = result.get(0).clone(); bDectectFace = true;
}
else
{
Log.d(TAG,"没有检测到人脸");
bDectectFace = false;
}
result.clear();
return bDectectFace;
} public AFT_FSDKFace getmAFT_FSDKFace()
{
return mAFT_FSDKFace;
} public String getAge(byte[] mYUVData,int width,int height,List<ASAE_FSDKFace> face1)
{
String age;
ASAE_FSDKError error1 = mAgeEngine.ASAE_FSDK_AgeEstimation_Image(mYUVData, width, height, AFT_FSDKEngine.CP_PAF_NV21, face1, ages);
Log.d("face", "ASAE_FSDK_AgeEstimation_Image:" + error1.getCode());
if(ages.size()== 0)
{
Log.d(TAG, "ages.size()== 0" );
age = "年龄未知";
}
else
{
Log.d(TAG, "age:" + ages.get(0).getAge());
age = ages.get(0).getAge() == 0 ? "年龄未知" : ages.get(0).getAge() + "岁";
}
return age;
} public String getGender(byte[] mYUVData,int width,int height,List<ASGE_FSDKFace> face2)
{
String gender;
ASGE_FSDKError error2 = mGenderEngine.ASGE_FSDK_GenderEstimation_Image(mYUVData, width, height, AFT_FSDKEngine.CP_PAF_NV21, face2, genders);
Log.d("face", ",ASGE_FSDK_GenderEstimation_Image:" + error2.getCode());
if(ages.size()== 0)
{
Log.d(TAG, "ages.size()== 0" );
gender = "性别未知";
}
else
{
Log.d(TAG, "gender:" + genders.get(0).getGender());
gender = genders.get(0).getGender() == -1 ? "性别未知" : (genders.get(0).getGender() == 0 ? "男" : "女");
}
return gender;
} /**
* @param path
* @return
*/
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(TAG, "check target Image:" + temp.getWidth() + "X" + temp.getHeight()); if (!temp.equals(res)) {
res.recycle();
}
return temp;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public void bitmapDetect(String name,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.d(TAG, "convert ok!");
}
convert.destroy(); AFD_FSDKEngine engine = new AFD_FSDKEngine();
AFD_FSDKVersion version = new AFD_FSDKVersion();
List<AFD_FSDKFace> result = new ArrayList<AFD_FSDKFace>();
AFD_FSDKError err = engine.AFD_FSDK_InitialFaceEngine(FaceDB.appid, FaceDB.fd_key, AFD_FSDKEngine.AFD_OPF_0_HIGHER_EXT, 16, 5);
Log.d(TAG, "AFD_FSDK_InitialFaceEngine = " + err.getCode());
if (err.getCode() != AFD_FSDKError.MOK) {
// Message reg = Message.obtain();
// reg.what = MSG_CODE;
// reg.arg1 = MSG_EVENT_FD_ERROR;
// reg.arg2 = err.getCode();
// mUIHandler.sendMessage(reg);
Log.d(TAG, "MSG_EVENT_FD_ERROR");
ToastUtil.showToast("sdk error :MSG_EVENT_FD_ERROR "+err.getCode());
}
err = engine.AFD_FSDK_GetVersion(version);
Log.d(TAG, "AFD_FSDK_GetVersion =" + version.toString() + ", " + err.getCode());
err = engine.AFD_FSDK_StillImageFaceDetection(data, mBitmap.getWidth(), mBitmap.getHeight(), AFD_FSDKEngine.CP_PAF_NV21, result);
Log.d(TAG, "AFD_FSDK_StillImageFaceDetection =" + err.getCode() + "<" + result.size()); if (!result.isEmpty()) {
AFR_FSDKVersion version1 = new AFR_FSDKVersion();
AFR_FSDKEngine engine1 = new AFR_FSDKEngine();
AFR_FSDKFace result1 = new AFR_FSDKFace();
AFR_FSDKError error1 = engine1.AFR_FSDK_InitialEngine(FaceDB.appid, FaceDB.fr_key);
Log.d(TAG, "AFR_FSDK_InitialEngine = " + error1.getCode());
if (error1.getCode() != AFD_FSDKError.MOK) {
// Message reg = Message.obtain();
// reg.what = MSG_CODE;
// reg.arg1 = MSG_EVENT_FR_ERROR;
// reg.arg2 = error1.getCode();
// mUIHandler.sendMessage(reg);
Log.d(TAG, "MSG_EVENT_FR_ERROR");
ToastUtil.showToast("sdk error :MSG_EVENT_FR_ERROR "+error1.getCode());
}
error1 = engine1.AFR_FSDK_GetVersion(version1);
Log.d(TAG, "FR=" + version.toString() + "," + error1.getCode()); //(210, 178 - 478, 446), degree = 1 780, 2208 - 1942, 3370
error1 = engine1.AFR_FSDK_ExtractFRFeature(data, mBitmap.getWidth(), mBitmap.getHeight(), AFR_FSDKEngine.CP_PAF_NV21, new Rect(result.get(0).getRect()), result.get(0).getDegree(), result1);
Log.d(TAG, "Face=" + result1.getFeatureData()[0] + "," + result1.getFeatureData()[1] + "," + result1.getFeatureData()[2] + "," + error1.getCode());
if(error1.getCode() == error1.MOK) {
mAFR_FSDKFace = result1.clone();
// int width = result.get(0).getRect().width();
// int height = result.get(0).getRect().height();
// Bitmap face_bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
// Canvas face_canvas = new Canvas(face_bitmap);
// face_canvas.drawBitmap(mBitmap, result.get(0).getRect(), new Rect(0, 0, width, height), null);
// Message reg = Message.obtain();
// reg.what = MSG_CODE;
// reg.arg1 = MSG_EVENT_REG;
// reg.obj = face_bitmap;
// mUIHandler.sendMessage(reg);
mFaceDB.addFace(name, mAFR_FSDKFace);
Log.d(TAG, "addFace MSG_EVENT_REG jxd");
ToastUtil.showToast(mContext.getString(R.string.register_face_success)+name);
} else {
// Message reg = Message.obtain();
// reg.what = MSG_CODE;
// reg.arg1 = MSG_EVENT_NO_FEATURE;
// mUIHandler.sendMessage(reg);
Log.d(TAG, "MSG_EVENT_NO_FEATURE");
ToastUtil.showToast(mContext.getString(R.string.no_feature));
}
error1 = engine1.AFR_FSDK_UninitialEngine();
Log.d(TAG, "AFR_FSDK_UninitialEngine : " + error1.getCode());
} else {
// Message reg = Message.obtain();
// reg.what = MSG_CODE;
// reg.arg1 = MSG_EVENT_NO_FACE;
// mUIHandler.sendMessage(reg);
Log.d(TAG, "MSG_EVENT_NO_FACE");
ToastUtil.showToast(mContext.getString(R.string.no_face));
}
err = engine.AFD_FSDK_UninitialFaceEngine();
Log.d(TAG, "AFD_FSDK_UninitialFaceEngine =" + err.getCode());
} public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
} /**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
} /**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
} /**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) { Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
}; try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
} public String getPath(Uri uri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(mContext, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0]; if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
} // TODO handle non-primary volumes
} else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(mContext, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0]; Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
}; return getDataColumn(mContext, contentUri, selection, selectionArgs);
}
}
}
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = mContext.getContentResolver().query(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
String end = img_path.substring(img_path.length() - 4);
if (0 != end.compareToIgnoreCase(".jpg") && 0 != end.compareToIgnoreCase(".png")) {
return null;
}
return img_path;
} public void setFaceFunction()
{
openFace = SharedPreferenceUtil.getBoolean(OPEN_FACE, Constant.FACE, false);
openFace = !openFace;
SharedPreferenceUtil.putBoolean(OPEN_FACE, Constant.FACE, openFace);
if(openFace)
{
ToastUtil.showToast(R.string.open_face);
}
else
{
ToastUtil.showToast(R.string.close_face);
}
} public boolean getFaceIsOpen()
{
return openFace;
} }
Android虹软人脸识别sdk使用工具类的更多相关文章
- 虹软人脸识别SDK接入Milvus实现海量人脸快速检索
一.背景 人脸识别是近年来最热门的计算机视觉领域的应用之一,而且现在已经出现了非常多的人脸识别算法,如:DeepID.FaceNet.DeepFace等等.人脸识别被广泛应用于景区.客运.酒店.办公室 ...
- 虹软人脸识别SDK在网络摄像头中的实际应用
目前在人脸识别领域中,网络摄像头的使用很普遍,但接入网络摄像头和人脸识别SDK有一定门槛,在此篇中介绍过虹软人脸识别SDK的接入流程,本文着重介绍网络摄像头获取视频流并处理的流程(红色框内),以下内容 ...
- Android 关于虹软人脸识别SDK引擎使用总结
虹软 最近开放了人脸识别的SDK引擎(免费的哦),刚好有Android版的,就体验了一波.下面来说说Android版的SDK使用心得: ArcFace 虹软人脸认知引擎简介 目前开放的版本有人脸比对( ...
- 基于虹软的Android的人脸识别SDK使用测试
现在有很多人脸识别的技术我们可以拿来使用:但是个人认为还是离线端的SDK比较实用:所以个人一直在搜集人脸识别的SDK:原来使用开源的OpenCV:最近有个好友推荐虹软的ArcFace, 闲来无事就下来 ...
- 关于虹软人脸识别SDK的接入
背景: 虹软的人脸识别还是不错的,在官方注册一个账号,成为开发者,下载SDK的jar包,在开发者中心,找一个demo就可以开始做了,安装里边的逻辑,先看理解代码,然后就可以控制代码,完成自己想要的功能 ...
- 虹软人脸识别SDK的接入方法
背景: 虹软的人脸识别还是不错的,在官方注册一个账号,成为开发者,下载SDK的jar包,在开发者中心,找一个demo就可以开始做了,安装里边的逻辑,先看理解代码,然后就可以控制代码,完成自己想要的功能 ...
- Android开发 打开已存在的项目(以虹软人脸识别sdk的demo为例)
详细流程参考博客https://blog.csdn.net/z979451341/article/details/79468785 个人遇到的问题与注意点 1.下载Demo后忘记修改appid和sdk ...
- Android 虹软人脸识别SDK-人脸对比
准备 : 登录官方网站,获取SDK,进行个人验证后新建项目,获取APP_ID,和SDK_KEY: https://ai.arcsoft.com.cn/ucenter/resource/build/in ...
- 虹软人脸识别SDK(java+linux/window)
虹软官网:http://www.arcsoft.com.cn/ 登录后要实名认证才可以使用sdk. 下图这两个是我选择的,window版本地开发测试,linux版是生产环境使用. 1. 保存激活码,下 ...
随机推荐
- jQuery动画方法
下面介绍一些使用jQuery实现动画的方法: html中有如下代码: <button id="btn-box1">show</button> <but ...
- Ubuntu server LTS 16.04安装SSH以及连接问题
1.SSH安装 出现问题: 登录到Ubuntu服务器,执行以下命令: sudo apt-get install openssh-server 出现以下错误: 解决办法: 1)确保服务器能出外网,比如说 ...
- 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(一)
前段时间面试被问到了数据库方面的知识:比如选择什么样的数据库,如何优化,怎么加索引,于是想到了自己动手测试一下常用数据库的性能: 第一步,下载好JMeter之后打开运行.话说这个JMeter打开还真是 ...
- selenium处理元素定位到了点击无效问题
在WEB自动化测试过程中,经常会遇到这样的问题: 元素定位到了,但是点击无效?有人可能会问了,怎么判断元素定位到了,这个问题很好判断 1.给元素加高亮显示 self.driver.execute_sc ...
- python练习--利用while循环和if语句,完成猜骰子的数字大小
#exampleimport random# 骰子投掷的随机叔numnum = random.randint(1,6)# 输入一个猜测的数字temp = input("请输入一个整数:&qu ...
- Windows7 IIS 出现错误,并非所有的功能被成功更改
(1)进入UAC,将其调至最低. 具体操作方法:点击[开始],在程序搜索中输入UAC,回车后则进入UAC控制界面. UAC,用户帐户控制设置如下,这个设置主要是避免Windows7种由于用户帐户权限控 ...
- C# 中List<T>与DataSet之间的转换
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- django 后端反向生成url
导入模块 from django.core.urlresolvers import reverse reverse() reverse(viewname, urlconf=None, args=Non ...
- turtle画王思聪吃热狗(杨艳春,何金凝小组)
点击此处查看视频:http://v.douyin.com/RCY8GD/import turtle as t t.setup(450,300) t.pensize(4) t.color('black' ...
- flask 电子邮件进阶实践-用模板发送163邮件
电子邮件进阶实践 下面来学习构建邮件的HTML正文,并使用模板组织内容. 一封电子邮件的正文可以是纯文本(text/plain),也可以是HTML格式的文本(text/html).处于全面的考虑,一封 ...