Pet*_*ela 5 android barcode-scanner surfaceview firebase firebase-mlkit
我正在使用Android firebase-ml-vision使用SurfaceView和相框的连续ByteBuffer扫描条形码。我以ML工具包快速入门项目作为起点,并且效果很好。
我项目的目的是识别与条形码关联的产品,并将其添加到扫描项目列表中。
相机对焦后,条形码处理器将多次检测到相同的条形码,因此您将在一秒钟内扫描20条而不是1条条形码。
这是来自CamereSource.FrameProcessingRunnable.run的 javadoc
Run Code Online (Sandbox Code Playgroud)* As long as the processing thread is active, this executes detection on frames continuously. * The next pending frame is either immediately available or hasn't been received yet. Once it * is available, we transfer the frame info to local variables and run detection on that frame. * It immediately loops back for the next frame without pausing.
我曾尝试在FrameProcessingRunnable中添加“已暂停”检查,但由于至少一个帧已被送入检测,因此我仍然至少两次被识别相同的条形码:
private class FrameProcessingRunnable implements Runnable {
private volatile boolean paused = false;
.
.
.
public void pause() {
synchronized (lock) {
this.paused = true;
lock.notifyAll();
}
}
public void resume() {
synchronized (lock) {
this.paused = false;
lock.notifyAll();
}
}
public void run() {
.
.
.
synchronized (processorLock) {
if (!paused) {
Log.d(TAG, "Process an image");
frameProcessor.process(...
Run Code Online (Sandbox Code Playgroud)
由于无法暂停,因此我选择了从缓冲区中检测到条形码时停止并开始:
private CameraSourcePreview preview;
public void pauseImageProcessing() {
preview.stop();
try {
preview.start(cameraSource, graphicOverlay);
} catch (IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
此方法有效,但是大约需要1秒钟的延迟才能再次启动,相机开始对焦并且可以检测到下一个条形码。无疑,这种方法还会消耗不必要的资源。您可能会说很好,但是在此视频中,您将看到带有Stop&Start的相机扫描仪和Bluetooth扫描仪之间的区别:
我正在寻找一种解决方案,该解决方案将在成功检测到一个帧之后立即丢弃所有帧并重新开始,但是到目前为止,我失败了。我每秒使用20帧。
VissionProcessorBase确实有用于节流的代码
// Whether we should ignore process(). This is usually caused by feeding input data faster than
// the model can handle.
private final AtomicBoolean shouldThrottle = new AtomicBoolean(false);
Run Code Online (Sandbox Code Playgroud)
但是还远远不够我的需求:(
一旦相机对焦,条码处理器就会多次检测到同一个条码,因此您可以在一秒钟内扫描 20 个而不是 1 个条码。
VisionProcessorBase.java
private void detectInVisionImage(
FirebaseVisionImage image,
final FrameMetadata metadata) {
detectInImage(image)
.addOnSuccessListener(
new OnSuccessListener<T>() {
@Override
public void onSuccess(final T results) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
shouldThrottle.set(false);
}
},1000);
VisionProcessorBase.this.onSuccess(results, metadata);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
shouldThrottle.set(false);
}
},1000);
VisionProcessorBase.this.onFailure(e);
}
});
// Begin throttling until this frame of input has been processed, either in onSuccess or
// onFailure.
shouldThrottle.set(true);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
877 次 |
| 最近记录: |