人脸识别开发实践总结

最近因为公司业务,酒店自助办理入住,需要用到人脸识别的功能,我们选用的是第三方腾讯云的SDK,用了获取唇语验证码和活体核身两个接口来实现的。业务需求是这样的,先通过摄像头识别到人脸之后,点击开始,获取唇语验证码,然后普通话均匀读出数字,但是期间也遇到了一些问题,再此记录一下,先看看我们的界面。

5132864-1b75bf2fd5770d85.png
朗读数字.png

遇到的问题

  1. 视频画面被压缩的问题
    如上图中,视频画面是一个圆形的,最开始的思路,直接用TextureView,限制宽高是一样的,最后显示出来视频画面是变形的,并且占不满整个圆形,后来一路查找之后,想设置Camare的宽高比例为1:1,就能和圆形的宽高比一致了,后来发现,可以占满圆形,但是画面比例还是会变形,后来查看了腾讯云SDK里面设置视频画面的宽高比例是写死的4:3,发现问题了所在。这里有3个比例,TextureView的宽高比,Camare的宽高比,视频画面Camcorder的宽高比,要3者保持一致才能解决画面变形的问题,问题的就纠结点在于之前一直是想改变其他两个比例来适配TextureView(1:1)的,现在转换一个思路,就是把TextureView的宽高比例设置成4:3,比如之前宽高都为230dp,现在宽就是230*4/3了,多出来的部分用白色盖住,不显示就可以了。
  • Camcorder的宽高
    代码里写死的比例4:3
    5132864-8347b5795818990e.jpg
    人脸识别1.jpg
  • TextureView的宽高
<TextureView
android:id="@+id/textureView"
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_gravity="center" />

// 修正宽高比
textureView.viewTreeObserver.addOnGlobalLayoutListener(mCameraOnGlobalLayoutListener)

/**
 * 修正检测人脸视频画面宽高比
 */
private val mCameraOnGlobalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {
    override fun onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            camera_preview.viewTreeObserver.removeOnGlobalLayoutListener(this)
        } else {
            camera_preview.viewTreeObserver.removeGlobalOnLayoutListener(this)
        }

        val w = camera_preview.width
        val whRatio = 4.toFloat() / 3 // 宽:高 = 4:3,这个比例是腾讯云api里写死的CameraHelper里
        val newH = Math.round(w * whRatio)
        val lp = camera_preview.layoutParams
        lp.height = newH
        camera_preview.layoutParams = lp
        LogUtil.e("TextureView,onGlobalLayout(): w=$w, h=$newH")
    }
}
  • Camare的宽高
/**
* 在摄像头启动前设置参数
*
* @param camera
*/
public static void setCameraParams(Context context, Camera camera, float curWidth) {
    if (context.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        camera.setDisplayOrientation(90);
    } else {
        camera.setDisplayOrientation(0);
    }
    // 获取摄像头支持的pictureSize列表
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
    // 设置PreviewSize
    Camera.Size previewSize = getPropPreviewSize(previewSizes, curWidth);
    parameters.setPreviewSize(previewSize.width, previewSize.height);

    parameters.setJpegQuality(100);
    if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        // 连续对焦
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }
    camera.cancelAutoFocus();
    camera.setParameters(parameters);
}

/**
* 获取摄像头符合规定的Size
*/
public static Camera.Size getPropPreviewSize(List<Camera.Size> list, float curWidth) {
    Collections.sort(list, (a, b) -> {
        int aPixels = a.height * a.width;
        int bPixels = b.height * b.width;
        if (bPixels < aPixels) {
            return -1;
        }
        if (bPixels > aPixels) {
            return 1;
        }
        return 0;
    });
    int i = 0;
    for (Camera.Size s : list) {
        LogUtil.i("list--width--" + s.width + "-----list--height--" + s.height);
        //宽高比4/3
        if (s.width >= ScreenUtil.dip2px(curWidth) && equalRate(s, 1.33f)) {
            LogUtil.i("PreviewSize:w = " + s.width + ",h = " + s.height);
            break;
        }
        i++;
    }
    if (i == list.size()) {
        LogUtil.i("如果没找到,就选最大的size");
        //如果没找到,就选最大的size
        i = 0;
    }
    return list.get(i);
}
  1. 息屏之后,再打开这个页面,画面会卡住的问题
 private var isCameraWithSurface = false // 相机和surface是否关联起来,处理息屏返回时画面卡住的BUG
/**
*  息屏之后再打开回调这个方法
*/
override fun onPause() {
    super.onPause()
    LogUtil.e("---onPause---")
    releaseCamera()
    isCameraWithSurface = false
}

textureView.surfaceTextureListener = mCameraTextureListener

/**
* 视频画面检测接口
* onSurfaceTextureUpdated方法里,重启摄像头
*/
private val mCameraTextureListener = object : TextureView.SurfaceTextureListener {

    override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
        LogUtil.d("TextureView onSurfaceTextureAvailable------")
        isCameraWithSurface = true
        if (mCamera != null) {
            CompareUtils.setCameraParams(mContext, mCamera, surfaceViewWidth)
            try {
                mCamera?.setFaceDetectionListener(mCameraFaceDetectionListener)
                mCamera?.setPreviewTexture(surface) // 使用SurfaceTexture
                mCamera?.startPreview()
                startFaceDetection()
            } catch (ioe: IOException) {
                // Something bad happened
                ioe.printStackTrace()
                LogUtil.e("Something bad happened")
            }
        }
    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
        LogUtil.d("TextureView onSurfaceTextureSizeChanged------")
    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
        LogUtil.d("TextureView onSurfaceTextureUpdated------")
        if (!isCameraWithSurface) {
            LogUtil.d("重新初始化------$mCamera")
            if (mCamera != null) {
                CompareUtils.setCameraParams(mContext, mCamera, surfaceViewWidth)
                try {
                    mCamera?.setFaceDetectionListener(mCameraFaceDetectionListener)
                    mCamera?.setPreviewTexture(surface) // 使用SurfaceTexture
                    mCamera?.startPreview()
                    startFaceDetection()
                    isCameraWithSurface = true
                } catch (ioe: IOException) {
                    // Something bad happened
                    ioe.printStackTrace()
                    LogUtil.e("Something bad happened")
                }
            }
        }
    }

    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
        LogUtil.e("TextureView onSurfaceTextureDestroyed")
        if (mCamera != null) {
            mCamera?.setFaceDetectionListener(null)
            mCamera?.stopPreview()
            mCamera?.release()
            mCamera = null
        }
        return true
    }
}

以上就是我踩过的坑,以后能帮助到有需要的朋友绕坑而行。

猜你喜欢

转载自blog.csdn.net/weixin_33774883/article/details/87218788
今日推荐