【问题标题】:Can not get camera output, face detection android无法获取相机输出,人脸检测android
【发布时间】:2017-09-07 18:31:48
【问题描述】:

我正在尝试使用 google vision api 进行人脸检测和添加掩码(图形叠加),问题是在检测和添加掩码后我无法从相机获取输出。到目前为止,我已经从 github 尝试了这个解决方案,https://github.com/googlesamples/android-vision/issues/24 ,基于这个问题,我添加了一个自定义检测器类, Mobile Vision API - concatenate new detector object to continue frame processing 。并将其添加到 mydetector 类 How to create Bitmap from grayscaled byte buffer image? 中。

MyDetectorClass

class MyFaceDetector extends Detector<Face> 
{
    private Detector<Face> mDelegate;

    MyFaceDetector(Detector<Face> delegate) {
        mDelegate = delegate;
    }

    public SparseArray<Face> detect(Frame frame) {
        // *** add your custom frame processing code here
        ByteBuffer byteBuffer = frame.getGrayscaleImageData();
        byte[] bytes = byteBuffer.array();
        int w = frame.getMetadata().getWidth();
        int h = frame.getMetadata().getHeight();
        YuvImage yuvimage=new YuvImage(bytes, ImageFormat.NV21, w, h, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(0, 0, w, h), 100, baos); // Where 100 is the quality of the generated jpeg
        byte[] jpegArray = baos.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
        Log.e("got bitmap","bitmap val " + bitmap);
        return mDelegate.detect(frame);
    }

    public boolean isOperational() {
        return mDelegate.isOperational();
    }

    public boolean setFocus(int id) {
        return mDelegate.setFocus(id);
    }
}

帧处理

public SparseArray<Face> detect(Frame frame) 
{
    // *** add your custom frame processing code here
    ByteBuffer byteBuffer = frame.getGrayscaleImageData();
    byte[] bytes = byteBuffer.array();
    int w = frame.getMetadata().getWidth();
    int h = frame.getMetadata().getHeight();
    YuvImage yuvimage=new YuvImage(bytes, ImageFormat.NV21, w, h, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, w, h), 100, baos); // Where 100 is the quality of the generated jpeg
    byte[] jpegArray = baos.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
    Log.e("got bitmap","bitmap val " + bitmap);
    return mDelegate.detect(frame);
}

我得到一个旋转的位图,即没有我添加的蒙版(图形叠加)。我怎样才能获得带有蒙版的相机输出。

提前致谢。

【问题讨论】:

    标签: android face-detection google-vision


    【解决方案1】:

    简单的答案是:你不能。

    为什么? NV21 ByteBuffer 中的 Android 相机输出帧。并且您必须根据单独的位图中的地标点生成掩码,然后加入它们。 抱歉,Android Camera API 就是这样工作的。什么都做不了。您必须手动完成。

    另外,我不会获得相机预览,然后将其转换为 YuvImage,然后再转换为位图。该过程会消耗大量资源并使预览非常非常慢。相反,我会使用这种方法,它会更快并在内部旋转您的预览,这样您就不会浪费时间去做它:

    outputFrame = new Frame.Builder().setImageData(mPendingFrameData, mPreviewSize.getWidth(), mPreviewSize.getHeight(), ImageFormat.NV21)
                  .setId(mPendingFrameId)
                  .setTimestampMillis(mPendingTimeMillis)
                  .setRotation(mRotation)
                  .build();
    mDetector.receiveFrame(outputFrame);
    

    所有代码都可以在CameraSource.java找到

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 2014-02-21
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      相关资源
      最近更新 更多