2017-04-12 70 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

找到