ObjectDetectorTask:对象检测器管道已重置:MLKIT

时间:2019-09-14 14:15:10

标签: android firebase-mlkit

我在Android应用程序中使用Mlkit对象检测API实时检测对象。这是我的代码

private AtomicBoolean flag = new AtomicBoolean(false);

private void startDetection(){
    final FirebaseVisionObjectDetectorOptions options = new FirebaseVisionObjectDetectorOptions.Builder()
            .setDetectorMode(FirebaseVisionObjectDetectorOptions.STREAM_MODE)
            .enableClassification()
            .build();
    final FirebaseVisionObjectDetector objectDetector =
            FirebaseVision.getInstance().getOnDeviceObjectDetector(options);

    timer = new Timer();
    timer.schedule(new TimerTask() {
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            try {
                if(flag.compareAndSet(false, true)) {
                    status.setText("Processing");
                    result.setText("Starting");
                    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(textureView.getBitmap());
                    objectDetector.processImage(image)
                            .addOnSuccessListener(
                                    new OnSuccessListener<List<FirebaseVisionObject>>() {
                                        @Override
                                        public void onSuccess(List<FirebaseVisionObject> detectedObjects) {
                                            /*StringBuilder wew = new StringBuilder();

                                            for (FirebaseVisionObject obj : detectedObjects) {
                                                Integer id = obj.getTrackingId();
                                                Rect bounds = obj.getBoundingBox();

                                                // If classification was enabled:
                                                int category = obj.getClassificationCategory();
                                                Float confidence = obj.getClassificationConfidence();
                                                wew.append(category);
                                            }
                                            result.setText(wew.toString());*/
                                            result.setText(Integer.toString(detectedObjects.size()));
                                            flag.set(false);
                                        }
                                    })
                            .addOnFailureListener(
                                    new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            result.setText(e.getMessage());
                                        }
                                    });
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
                result.setText(e.getMessage());
            }
        }
    },0,2000);
}

对象检测列表为0,我是从logcat那里得到的

  

ObjectDetectorTask:对象检测器管道已重置

为什么会这样?

0 个答案:

没有答案