Mobile Vision API - 连接新的检测器对象以继续帧处理

Mobile Vision API - concatenate new detector object to continue frame processing

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

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

谢谢, 卢西奥

是的,这是可能的。您需要创建自己的 Detector subclass,它包装 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);
  }
}

你会用你的 class 包裹面部检测器,然后将你的 class 传递到相机源。它看起来像这样:

    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 方法获取方向。

提醒一句:一旦检测方法returns,您不应该访问帧像素数据。由于相机源回收图像缓冲区,一旦方法 returns.

最终将覆盖框架对象的内容。

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

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

另请注意 FaceTrackerFactoryMultiProcessor 的结合使用 那里有描述。

这是我确定的最终解决方案。它假定框位于屏幕中央。

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

像这样将 class 包裹在你的检测器中

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
BoxDetector boxDetector = new BoxDetector(barcodeDetector, heightPx, widthPx);

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

使用 @MCR BoxDetector class 然后按照以下步骤操作。

我只是给出有关文本识别器的示例,因此您可以这样设置

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

在此处设置 boxDetecotr

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

                }
            });