簡體   English   中英

從CameraSource裁剪臉部

[英]Crop face from the CameraSource

我正在實現google-vision人臉跟蹤器中給出的示例。 MyFaceDetector類:

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

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

    public SparseArray<Face> detect(Frame frame) {
        return mDelegate.detect(frame);
    }

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

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

}

FaceTrackerActivity類:

private void createCameraSource() {

    imageView = (ImageView) findViewById(R.id.face);

    FaceDetector faceDetector = new FaceDetector.Builder(this).build();
    myFaceDetector = new MyFaceDetector(faceDetector);
    myFaceDetector.setProcessor(new MultiProcessor.Builder<>(new GraphicFaceTrackerFactory())
            .build());
    mCameraSource = new CameraSource.Builder(this, myFaceDetector)
            .setRequestedPreviewSize(640, 480)
            .setFacing(CameraSource.CAMERA_FACING_FRONT)
            .setRequestedFps(60.0f)
            .build();

    if (!myFaceDetector.isOperational()) {
        Log.w(TAG, "Face detector dependencies are not yet available.");
    }
}

我需要裁切臉並將其設置在ImageView 我無法在此處實現我的自定義Frame frame.getBitmap()detect(Frame frame)始終返回null 我該如何實現?

如果框架最初是從位圖創建的,則frame.getBitmap()將僅返回值。 CameraSource提供的圖像信息是ByteBuffers而不是位圖,因此是可用的圖像信息。

frame.getGrayscaleImageData()將返回圖像數據。

frame.getMetadata()將返回元數據,例如圖像尺寸和圖像格式。

這在CameraSource.java

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

int w = outputFrame.getMetadata().getWidth();
int h = outputFrame.getMetadata().getHeight();
SparseArray<Face> detectedFaces = mDetector.detect(outputFrame);
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

if (detectedFaces.size() > 0) {
    ByteBuffer byteBufferRaw = outputFrame.getGrayscaleImageData();
    byte[] byteBuffer = byteBufferRaw.array();
    YuvImage yuvimage  = new YuvImage(byteBuffer, ImageFormat.NV21, w, h, null);

    Face face = detectedFaces.valueAt(0);
    int left = (int) face.getPosition().x;
    int top = (int) face.getPosition().y;
    int right = (int) face.getWidth() + left;
    int bottom = (int) face.getHeight() + top;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(left, top, right, bottom), 80, baos);
    byte[] jpegArray = baos.toByteArray();
    bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
}
((FaceTrackerActivity) mContext).setBitmapToImageView(bitmap);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM