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. 保存激活码,下 ...
随机推荐
- MySQL数据库常用命令和概念 (1)
一.数据库的创建: 1.创建一个名称为mydb1的数据库 create database mydb1; 2.创建一个使用utf8字符集的mydb2数据库. create database mydb2 ...
- feifeicms后台任意文件读取
前台大略看了下,本身内容比较简单,经过“洗礼”后以及没什么问题了,基本上输入都过滤了. 这次审计找到了一个后台的任意文件读取,可以读取数据库配置文件. 在DataAction.class.php文件中 ...
- FB面经 Prepare: Largest Island
Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...
- IconFont --阿里巴巴矢量库
超多的图标网站,可自己设置颜色,然后下载. IconFont --阿里巴巴矢量库
- #WEB安全基础 : HTTP协议 | 0x9 GET和POST请求以及请求URI的方式
请求URI的方式 1.URI为完整的请求URI GET http://hackr.jp/index.htm HTTP/1.1 2.在首部字段Host中写明域名或IP地址 GET/index.htm H ...
- OpenGL——外部读档+异常报错
从外部读取shader文件: 先添加Shader类: 再创建vertexSource.txt和fragmentSource.txt两个文件: 如图填入shader: 在shader.h宣告: 在sha ...
- PHP、mySQL及Navicat安装·Mac
PHP配置 Mac系统对开发人员非常友好,除了自带Apache外,还带有能与Apache相匹配的服务器端脚本语言PHP,因此,Mac中PHP的启动只需要在Apache服务中进行一下超级简单的配置即可直 ...
- Python之io概念
""" 同步,异步: 强调结果,调用者最终是否得到想要的结构 阻塞非阻塞: 强调时间是否等待 io二个阶段 1.数据准备阶段 2.内核空间复制回用户空间缓冲区阶段 发生i ...
- 论文阅读(Lukas Neumann——【ICCV2017】Deep TextSpotter_An End-to-End Trainable Scene Text Localization and Recognition Framework)
Lukas Neumann——[ICCV2017]Deep TextSpotter_An End-to-End Trainable Scene Text Localization and Recogn ...
- 用mongols轻松打造websocket应用
用websocket做聊天系统是非常合适的. mongols是一个运行于linux系统之上的开源c++库,可轻松开启一个websocket服务器. 首先,build一个websocket服务器. #i ...