Tengine从入门到放弃 第5篇-Tengine 人脸检测 Android Camera版

1,503 阅读2分钟

Tengine 人脸检测 Android Camera版

这篇是第4篇-Tengine 人脸检测 Android版的扩展,我们将用TengineKit,把Android Camera输入的YUV格式的视频流转为一帧帧的Bitmap,然后用上第4篇写的人脸检测,快速的变成一个Android Camera应用。

源码在此处FaceDetector_Android_Camera

TengineKit

TengineKit是一个易于集成的人脸检测和人脸关键点SDK。它可以在各种手机上以非常低的延迟运行。

TengineKit图片处理功能

TengineKit.Image类封装了一系列工具API,帮助开发者快速方便的处理图片相关的功能,具体功能请查阅文档,这里我们将用这个TengineKit.Image里面的API实现Android前置摄像头视频流转为Bitmap(YUV_NV21转RGBA)。

实现

TengineKit的配置和Camera应用的结构都是参考这篇文章

用开源212点人脸关键点实现Android人脸实时打码,内附Github地址

我们拿核心部分讲一下

YUV转Bitmap

  1. previewWidth, previewHeight 是摄像头输入视频流的宽高
  2. outputWidth, outputHeight 是希望输出的宽高
  3. 为什么要旋转-90度和镜像翻转?因为我们用的前置摄像头,前置摄像头输出的视频流旋转-90度然镜像反正下,得到的就是我们正常看到的照片
@Override
protected void processImage(byte[] yuv, int previewWidth, int previewHeight, int outputWidth, int outputHeight) {

    if(faceBitmap != null){
        faceBitmap.recycle();
    }
    faceBitmap = com.tenginekit.Face.Image.convertCameraYUVData(
            yuv,
            previewWidth, previewHeight,
            outputWidth, outputHeight,
            - 90,
            true);

    faceInfos =  FaceDetector.detectByBitmap(faceBitmap);

    for(Bitmap bitmap : testFaceBitmaps){
        bitmap.recycle();
    }
    testFaceBitmaps.clear();

    runInBackground(new Runnable() {
        @Override
        public void run() {
            trackingOverlay.postInvalidate();
        }
    });
}

相对于上篇文章,这里给FaceDetector新封装了一个API,可以直接传入Bitmap得到人脸数据。

public static List<FaceInfo> detectByBitmap(Bitmap image){
    return detectByBytes(bitmap2Bytes(image), image.getWidth(), image.getHeight());
}

private static byte[] bitmap2Bytes(Bitmap image) {
    // calculate how many bytes our image consists of
    int bytes = image.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
    image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
    byte[] temp = buffer.array(); // Get the underlying array containing the
    return temp;
}

参考

  1. github.com/Linzaer/Ult…

  2. github.com/OAID/Tengin…

  3. github.com/OAID/Tengin…

  4. github.com/jiangzhongb…

源码

github.com/jiangzhongb…

知乎

zhuanlan.zhihu.com/p/203113925