19

我想在应用程序中使用视觉 API 提供的新人脸检测功能以及额外的帧处理。为此,我需要访问由人脸检测器处理的相机帧,并使用人脸检测数据连接处理器。

正如我在示例中看到的,CameraSource 抽象了检测和摄像头访问,我无法访问正在处理的帧。是否有示例说明如何在此 API 中获取相机帧,或者创建并连接接收它的检测器?至少这是可能的吗?

谢谢,卢西奥

4

3 回答 3

23

对的,这是可能的。您需要创建自己的 Detector 子类,它包装 FaceDetector 并在检测方法中执行额外的帧处理代码。它看起来像这样:

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
    return mDelegate.detect(frame);
  }

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

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

您将使用您的课程包装面部检测器,并将您的课程传递给相机源。它看起来像这样:

    FaceDetector faceDetector = new FaceDetector.Builder(context)
            .build();
    MyFaceDetector myFaceDetector = new MyFaceDetector(faceDetector);

    myFaceDetector.setProcessor(/* include your processor here */);

    mCameraSource = new CameraSource.Builder(context, myFaceDetector)
            .build();

您的检测器将首先使用原始帧数据调用。

请注意,如果设备旋转,图像可能不是竖直的。您可以通过框架的 metadata.getRotation 方法获取方向。

请注意:一旦检测方法返回,您不应该访问帧像素数据。由于相机源回收图像缓冲区,一旦方法返回,帧对象的内容最终将被覆盖。

编辑:(附加说明)您还可以避免MyFaceDetector使用这样的MultiDetector的样板代码:

MultiDetector multiDetector = new MultiDetector.Builder()
    .add(new FaceDetector.Builder(context)
                .build())
    .add(new YourReallyOwnDetector())
    .build();

还要注意FaceTrackerFactory与那里描述的结合使用MultiProcessor

于 2015-08-31T14:54:30.453 回答
22

这是我确定的最终解决方案。它假定框在屏幕上居中。

public class BoxDetector extends Detector {
    private Detector mDelegate;
    private int mBoxWidth, mBoxHeight;

    public BoxDetector(Detector delegate, int boxWidth, int boxHeight) {
        mDelegate = delegate;
        mBoxWidth = boxWidth;
        mBoxHeight = boxHeight;
    }

    public SparseArray detect(Frame frame) {
        int width = frame.getMetadata().getWidth();
        int height = frame.getMetadata().getHeight();
        int right = (width / 2) + (mBoxHeight / 2);
        int left = (width / 2) - (mBoxHeight / 2);
        int bottom = (height / 2) + (mBoxWidth / 2);
        int top = (height / 2) - (mBoxWidth / 2);

        YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(left, top, right, bottom), 100, byteArrayOutputStream);
        byte[] jpegArray = byteArrayOutputStream.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);

        Frame croppedFrame =
                new Frame.Builder()
                        .setBitmap(bitmap)
                        .setRotation(frame.getMetadata().getRotation())
                        .build();

        return mDelegate.detect(croppedFrame);
    }

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

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

像这样将此类包装在您的检测器中

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
BoxDetector boxDetector = new BoxDetector(barcodeDetector, heightPx, widthPx);
于 2017-05-11T12:13:15.030 回答
1

根据用户(新开发人员)的要求,如何设置盒子检测器。你可以这样使用

使用@MCRBoxDetector 类,然后按照以下步骤操作。

我只是举个关于文本识别器的例子,所以你可以这样设置

TextRecognizer mTextRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
BoxDetector boxDetector = new BoxDetector(mTextRecognizer, heightPx, widthPx);

在这里设置 boxDetectr

boxDetector.setProcessor(new Detector.Processor<TextBlock>() {
                @Override
                public void release() {

                }

                @Override
                public void receiveDetections(Detector.Detections<TextBlock> detections) {
                    SparseArray<TextBlock> items = detections.getDetectedItems();
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < items.size(); ++i) {
                        TextBlock item = items.valueAt(i);
                        if (item != null && item.getValue() != null) {
                            stringBuilder.append(item.getValue() + " ");
                        }
                    }

                    final String fullText = stringBuilder.toString();
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        public void run() {
                            // here full string(fullText) you can get whatever is it scanned.
                        }
                    });

                }
            });
于 2019-05-06T11:20:33.027 回答