android人脸检测点位置转换

android相机开发很多需要进行人脸检测,很多公司也推出了自己的人脸识别服务。比如:阿里、腾讯、虹软、Face++等等。但这些有缺点,就是必须集成第三方库联网激活,也增大了app。如果只需要检测人脸这种轻量级功能,用第三方库就有些重,幸好android自带了人脸检测功能。下来我们就主要看看Android如何实现人脸检测。

先上效果图:
人脸检测.gif

Android 实现人脸检主要调用一下接口和方法

mCamera!!.setFaceDetectionListener(object : Camera.FaceDetectionListener {
        override fun onFaceDetection(p0: Array<out Camera.Face>?, p1: Camera?) {
            var size = 0
            if (p0 != null) {
                size = p0.size
            }
            Log.i("face", "size: $size")
        }
    })
//开始人脸检测
mCamera!!.startFaceDetection()

以上代码就可以获取到人脸数量。

但是到这里不满足的开发者就会觉得,我还想往上面现实人脸框呢,总不能拿界面上不显示任何信息吧。
那下来咱们看看人脸框是如何现实的

首先明确一点,Camera.Face 这里面对应的坐标点和当前界面的坐标点不是一个意思,看看Android 中对Face类中Rect的介绍吧

#rect
Added in API level 14
Deprecated in API level 21
Bounds of the face. (-1000, -1000) represents the top-left of the camera field of view, and (1000, 1000) represents the bottom-right of the field of view. For example, suppose the size of the viewfinder UI is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0). The corresponding viewfinder rect should be (0, 0, 400, 240). It is guaranteed left < right and top < bottom. The coordinates can be smaller than -1000 or bigger than 1000. But at least one vertex will be within (-1000, -1000) and (1000, 1000).

The direction is relative to the sensor orientation, that is, what the sensor sees. The direction is not affected by the rotation or mirroring of Camera.setDisplayOrientation(int). The face bounding rectangle does not provide any information about face orientation.

原文地址 Camera.Face

英文不好理解的话咱们直接上图
Camera.Face 中坐标.png

如图,正常view (0,0) 坐标是在左上角,而相机返回的人脸坐标(0,0)实在界面中央,所以拿到的Face信息咱们要进行转换后才能使用!
转换方法如下:

     /**
     * 准备用于转换的矩阵工具
     *
     * @param isBackCamera  是否后置相机
     * @param displayOrientation  摄像头设置的角度
     * @param viewWidth  预览界面宽
     * @param viewHeight  预览界面高
     */
    fun prepareMatrix(isBackCamera: Boolean, displayOrientation: Int,
                      viewWidth: Int, viewHeight: Int): Matrix {
        val matrix = Matrix()
        //前置摄像头处理镜像关系
        matrix.setScale(1f, (if (!isBackCamera) -1 else 1).toFloat())
        matrix.postRotate(displayOrientation.toFloat())
        matrix.postScale(viewWidth / 2000f, viewHeight / 2000f)
        matrix.postTranslate(viewWidth / 2f, viewHeight / 2f)

        return matrix
    }

这也是android官方这转换方法,详见Camera.Face

咦?还没看懂。人脸识别更详细的看这里 Android人脸检测功能和检测特效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值