无法使用Firebase mlkit对象检测功能检测对象

时间:2019-06-15 01:16:01

标签: android firebase-mlkit

我正在开发一个应用程序来检测摄像头上的汽车,并且我正在使用Firebase mlkit来完成这项工作。我在OnSuccessListener上遇到问题。我根本看不到它检测到对象。我做了吐司来展示detectedobjects.size,但它什么也没有。

public class MainActivity extends AppCompatActivity {

    private CameraView cameraview;
    private String TAG;
    Context tt = this;
    DrawRectangle dr;
    TextView label;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraview = findViewById(R.id.cameraView);
        cameraview.setLifecycleOwner(this);
        label=findViewById(R.id.label);
        FirebaseApp.initializeApp(this);
        cameraview.addFrameProcessor(new FrameProcessor() {
            @Override
            @WorkerThread
            public void process(@NonNull com.otaliastudios.cameraview.Frame frame) {
                FirebaseVisionImage image = null;
                FirebaseVisionObjectDetector objectDetector = null;

                    byte[] data = frame.getData();
                    FirebaseVisionImageMetadata imageMetadata = new FirebaseVisionImageMetadata.Builder()
                            .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
                            .setHeight(480)
                            .setWidth(360)
                            //.setRotation(90)
                            .build();
                    image = FirebaseVisionImage.fromByteArray(data, imageMetadata);
                    FirebaseVisionObjectDetectorOptions options = new FirebaseVisionObjectDetectorOptions.Builder()
                            .setDetectorMode(FirebaseVisionObjectDetectorOptions.SINGLE_IMAGE_MODE)
                            .enableClassification()
                            .build();
                    objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(options);
                    objectDetector.processImage(image)
                            .addOnSuccessListener(
                                    new OnSuccessListener<List<FirebaseVisionObject>>() {
                                        @Override
                                        public void onSuccess(List<FirebaseVisionObject> detectedObjects) {
                                            Toast.makeText(tt, String.valueOf(detectedObjects.size()), Toast.LENGTH_SHORT).show();

                                            for (FirebaseVisionObject obj : detectedObjects) {
                                                Integer id = obj.getTrackingId();
                                                Rect bounds = obj.getBoundingBox();
                                                int cat =obj.getClassificationCategory();
                                                String cot= String.valueOf(cat);
                                                label.setText(obj.getEntityId());
                                                //dr = new DrawRectangle(tt,obj);
                                            }
                                        }
                                    })
                            .addOnFailureListener(
                                    new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Toast.makeText(tt, "Oops, something went wrong!",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    });
                }

        });

    }
}

0 个答案:

没有答案