Android(安卓)NDK开发:SeetaFace2实现人脸特征点检测
16lz
2022-03-11
目录
效果展示
p01.gif代码解释
本次效果的实现是在以下三篇文章的知识基础上进行的:
●SeetaFace2库的引入请查看:Android NDK开发:SeetaFace2人脸识别算法简介
●人脸检测请查看:Android NDK开发:SeetaFace2实现人脸检测
/** * 特征点检测 */public class PointDetectorActivity extends AppCompatActivity { private Button mBt; private ImageView mImg; private FaceDetector2 faceDetector; private PointDetector2 pointDetector; private SeetaRect[] faceRects; private Bitmap bitmap; private SeetaImageData seetaImageData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pointdetector); //将模型拷贝到SD卡中 //FileUtil.CopyAssets(this,"SeetaFaceDetector2.0.ats",Environment.getExternalStorageDirectory()+ File.separator+"SeetaFaceDetector2.0.ats"); initView(); initFace(); mBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //如果图像中检测出人脸了再进行绘制 if(faceRects.length>0){ //这里必须进行copy否则修改不了 Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true); //利用Bitmap创建Canvas,为了在图像上绘制人脸区域 Canvas canvas = new Canvas(copy); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); //绘制出所有的检测出来的人脸的区域 for(int i = 0 ; i < faceRects.length ; i++){ SeetaRect faceRect = faceRects[i]; SeetaPointF[] seetaPoints = pointDetector.Detect(seetaImageData, faceRect);//根据检测到的人脸进行特征点检测 if(seetaPoints.length>0){ for(SeetaPointF seetaPoint:seetaPoints){ //绘制特征点 canvas.drawCircle((float) seetaPoint.x,(float) seetaPoint.y,5,paint); } } } mImg.setImageBitmap(copy); } } }); } /** * 初始化人脸检测器 */ private void initFace() { //初始化检测器(参数是模型在SD卡的位置) faceDetector = new FaceDetector2(Environment.getExternalStorageDirectory()+ File.separator+"seetaface"+File.separator+"SeetaFaceDetector2.0.ats"); pointDetector = new PointDetector2(Environment.getExternalStorageDirectory()+ File.separator+"seetaface"+File.separator+"SeetaPointDetector2.0.pts5.ats"); //特征点 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.heying); //利用SeetaFace2提供的转换方法获取SeetaRect(人脸识别结果) seetaImageData = ConvertUtil.ConvertToSeetaImageData(bitmap); faceRects = faceDetector.Detect(seetaImageData); } private void initView() { mBt = findViewById(R.id.bt_face); mImg = findViewById(R.id.img); } @Override protected void onDestroy() { super.onDestroy(); if(faceDetector!=null){ faceDetector.dispose(); } if(pointDetector!=null){ pointDetector.dispose(); } }}
案例源码
https://github.com/myml666/Seetaface2
更多相关文章
- Android(安卓)Animation (动画设计)
- Android(安卓)图片设置圆角
- android检测当前网络是否可用
- Android的绘制文本对象FontMetrics的介绍及绘制文本
- 移动安全之Android安全检测工具大全
- Android实现九宫格图案解锁
- Android(安卓)Studio 检测不到 Genymotion 模拟器
- Android(安卓)Canvas drawArc方法介绍
- 百度地图SDK for Android(安卓)v2.1.2全新发布