2017-04-12 35 views
0

我正在尝试使用谷歌视觉api进行人脸检测和添加蒙版(图形覆盖),问题是我无法从相机检测到并添加mask.ou远后我得到了ouptut尝试从github,https://github.com/googlesamples/android-vision/issues/24这个解决方案,基于这个问题我添加了一个自定义检测器类, Mobile Vision API - concatenate new detector object to continue frame processing。并将其添加到我的检测器类How to create Bitmap from grayscaled byte buffer image?无法获取相机输出,人脸检测android

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); 
} 

我得到一个旋转后的位,也就是没有掩模(图形覆盖)我已经加入。如何可以我得到与掩模摄像机输出。

在此先感谢。

回答

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

找到