gpt4 book ai didi

android - 从 SurfaceView 使用 ByteBuffer 时如何在 android ML-kit 中暂停条形码扫描

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:09:33 29 4
gpt4 key购买 nike

上下文

我正在使用 Android firebase-ml-vision使用 SurfaceView 和图片帧的连续 ByteBuffer 扫描条形码。我用了ML kit quickstart project作为起点,效果很好。

我的项目的目标是识别与条形码关联的产品并将其添加到扫描项目列表中。

问题

一旦相机聚焦,条码处理器就会多次检测到相同的条码,因此您将在一秒钟内扫描 20 个而不是 1 个条码。

这是来自 CamereSource.FrameProcessingRunnable.run 的 javadoc

 * 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(...

使用停止和启动的解决方案

因为我无法让它暂停,所以我选择了在从缓冲区中检测到条形码时停止并启动:

private CameraSourcePreview preview;
public void pauseImageProcessing() {
preview.stop();
try {
preview.start(cameraSource, graphicOverlay);
} catch (IOException e) {
}
}

这有效,但在它再次启动之前有大约 1 秒的延迟,相机开始对焦并且可以检测到下一个条形码。毫无疑问,这种方法也会消耗不必要的资源。你可能会说这很好,但在 this video you'll see the difference between the camera scanner with Stop&Start and a Bluetooth scanner :

更好的方法

我正在寻找一种解决方案,它可以在成功检测到帧之后立即丢弃所有帧并重新开始,但到目前为止我都失败了。我使用的是每秒 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);

但这还远远不能满足我的需要 :(

最佳答案

Once the camera focuses, the barcode processor would detect the same barcode multiple times, so you would scan 20 rather than 1 barcode in a second.

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



}

关于android - 从 SurfaceView 使用 ByteBuffer 时如何在 android ML-kit 中暂停条形码扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51178376/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com