在 Android 移动视觉中使用管道检测文本时获取帧
Get Frame when detecting text using a pipeline in Android Mobile Vision
我正在使用类似于 Google CodeLabs sample code 的管道检测文本。如何获取 CameraSource 发送到 TextRecognizer 的预览帧?
预览帧未发送到文本识别器之外。但是,您可以制作一个 class 来包装文本识别器,在检测之前接收预览帧。请参阅类似的讨论 。
首先,实现一个检测器class来包装文本识别器:
class MyTextRecognizer extends Detector<TextBlock> {
private Detector<TextBlock> mDelegate;
MyTextRecognizer(Detector<TextBlock> delegate) {
mDelegate = delegate;
}
public SparseArray<TextBlock> detect(Frame frame) {
// *** add your code to access the preview frame here
return mDelegate.detect(frame);
}
public boolean isOperational() {
return mDelegate.isOperational();
}
public boolean setFocus(int id) {
return mDelegate.setFocus(id);
}
}
用您的 class 包装文本识别器,并将您的 class 传递到相机源中。它看起来像这样:
TextRecognizer textRecognizer = new TextRecognizer.Builder(context)
.build();
TextRecognizer myTextRecognizer = new MyTextRecognizer(textRecognizer);
myTextRecognizer.setProcessor(/* include your processor here */);
mCameraSource = new CameraSource.Builder(context, myTextRecognizer)
.build();
您的 MyTextRecognizer 将首先使用原始帧数据调用。
请注意,如果设备旋转,图像可能不是直立的。您可以通过框架的 metadata.getRotation 方法获取方向。
提醒一句:一旦检测方法returns,您不应该访问帧像素数据。由于相机源回收图像缓冲区,一旦方法 returns.
最终将覆盖帧对象的内容
我正在使用类似于 Google CodeLabs sample code 的管道检测文本。如何获取 CameraSource 发送到 TextRecognizer 的预览帧?
预览帧未发送到文本识别器之外。但是,您可以制作一个 class 来包装文本识别器,在检测之前接收预览帧。请参阅类似的讨论
首先,实现一个检测器class来包装文本识别器:
class MyTextRecognizer extends Detector<TextBlock> {
private Detector<TextBlock> mDelegate;
MyTextRecognizer(Detector<TextBlock> delegate) {
mDelegate = delegate;
}
public SparseArray<TextBlock> detect(Frame frame) {
// *** add your code to access the preview frame here
return mDelegate.detect(frame);
}
public boolean isOperational() {
return mDelegate.isOperational();
}
public boolean setFocus(int id) {
return mDelegate.setFocus(id);
}
}
用您的 class 包装文本识别器,并将您的 class 传递到相机源中。它看起来像这样:
TextRecognizer textRecognizer = new TextRecognizer.Builder(context)
.build();
TextRecognizer myTextRecognizer = new MyTextRecognizer(textRecognizer);
myTextRecognizer.setProcessor(/* include your processor here */);
mCameraSource = new CameraSource.Builder(context, myTextRecognizer)
.build();
您的 MyTextRecognizer 将首先使用原始帧数据调用。
请注意,如果设备旋转,图像可能不是直立的。您可以通过框架的 metadata.getRotation 方法获取方向。
提醒一句:一旦检测方法returns,您不应该访问帧像素数据。由于相机源回收图像缓冲区,一旦方法 returns.
最终将覆盖帧对象的内容