0 Replies - 674 Views - Last Post: 25 October 2019 - 11:07 AM

#1 TheStudent555   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-February 19

ML Kit: List<FirebaseVisionBarcode> is empty in onSuccess

Posted 25 October 2019 - 11:07 AM

After an image has been captured, that contains a UPC-A barcode, the onSuccess method of FirebaseVisionBarcodeDetector is called. However, the List<FirebaseVisionBarcode> is empty. Not sure what would cause this problem. Currently using a Nexus 5 device for testing.

  private void capture_image(){


        CameraManager camera_manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try{

            CameraCharacteristics camera_characteristics = camera_manager.getCameraCharacteristics(camera_device.getId());

            Size [] yuvSizes = null;
            if (camera_characteristics != null) {
                yuvSizes = camera_characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.YUV_420_888);
            }
            int width = 640;
            int height = 480;
            if (yuvSizes != null && 0 < yuvSizes.length) {
                width = yuvSizes[0].getWidth();
                height = yuvSizes[0].getHeight();
            }

            image_reader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 1);
            List<Surface> output_surface = new ArrayList<Surface>(2);
            output_surface.add(image_reader.getSurface());
            output_surface.add(new Surface(texture_view.getSurfaceTexture()));

            final CaptureRequest.Builder capture_builder = camera_device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            capture_builder.addTarget(image_reader.getSurface());
            capture_builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            capture_builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
            ImageReader.OnImageAvailableListener reader_listener = new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {

                    Image image = reader.acquireLatestImage();
                    upc_image = image;

                    if(image != null){

                        upc_image = image;

                        Scan scan = new Scan(Camera.this, Camera.this, upc_image);
                        scan.scanning();

                    }

                }
            };

            image_reader.setOnImageAvailableListener(reader_listener, background_handler);
            final CameraCaptureSession.CaptureCallback capture_callback = new CameraCaptureSession.CaptureCallback() {
                @Override
                public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                    super.onCaptureCompleted(session, request, result);
                    create_camera_preview();
                }
            };

            camera_device.createCaptureSession(output_surface, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession session) {

                    try{
                        session.capture(capture_builder.build(), capture_callback, background_handler);

                    }catch (CameraAccessException camera_access_error){
                        camera_access_error.printStackTrace();
                    }

                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession session) {

                }
            }, background_handler);

        }catch(CameraAccessException camera_access_error){
            camera_access_error.printStackTrace();
        }


        camera_open_close_lock.release();

    }





When the image is captured, and onImageAvailable is called, the width and height are 3264 and 2448 pixels respectively.
  image_reader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 1); //<-- width: 3264 height: 2448. Could this be a problem. Image is too big? 



Scan class
public void scanning(){

        FirebaseVisionImage image = null;

        try {
            image = FirebaseVisionImage.fromMediaImage(upc_image, Rotation_Compenstation());
        }catch(CameraAccessException camera_access_error){
            camera_access_error.printStackTrace();
        }

        try {
            Task<List<FirebaseVisionBarcode>> result = firebase_barcode_detector.detectInImage(image)
                    .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
                        @Override
                        public void onSuccess(List<FirebaseVisionBarcode> firebase_vision_barcodes) {
                            // Task was successful
                            for (FirebaseVisionBarcode firebase_vision_barcode : firebase_vision_barcodes) {

                 
                            }


                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception error) {
                            // Task was a failure
                 
                            error.printStackTrace();


                        }
                    }).addOnCanceledListener(new OnCanceledListener() {
                        @Override
                        public void onCanceled() {
                        
                        }
                    }).addOnCompleteListener(new OnCompleteListener<List<FirebaseVisionBarcode>>() {
                        @Override
                        public void onComplete(@NonNull Task<List<FirebaseVisionBarcode>> task) {
                       
                            Log.d(TAG, "Result: " + task.getResult().toString());
                        }
                    });
        }catch (Exception error){
            error.printStackTrace();
            Log.d(TAG, "Error, read fail");
        }


  }




When creating a FirebaseVisionImage a rotation compensation has to be calculated.

public int Rotation_Compenstation(){

        int current_device_rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        // current_device_rotation is 0
        int rotation_compensation = ORIENTATION.get(current_device_rotation);
        // rotation_compensation is 90
        CameraManager camera_manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        String cameraId = camera_manager.getCameraIdList()[0];
        
        int device_sensor_orientation = camera_manager.getCameraCharacteristics(cameraId).get(CameraCharacteristics.SENSOR_ORIENTATION);
        // device_sensor_oreintation is 90

        rotation_compensation = (rotation_compensation + device_sensor_orientation + 270) % 360;
        // rotation_compensation here is 90

        int result_rotation;
        switch(rotation_compensation){

            case 0:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_0;
                break;

            case 90:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_90;
                break;

            case 180:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_180;
                break;

            case 270:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_270;
                break;

                default:
                    result_rotation = FirebaseVisionImageMetadata.ROTATION_0;
                    Log.d(TAG, "Bad Rotation Value: " + result_rotation);
                    break;

        }

        return result_rotation;
        // result_rotation is 1


}





// current_device_rotation: 0
// device_sensor_orientation: 90
// rotation_compensation: 90
// result_rotation: 1





The phone device is always held upright (portrait). The barcode is upright (Capable of reading it from left to right). Not sure what the problem could be. I initially thought the image was to big as when I called


upc_image.getWidth() and upc_image.getHeight() // (of type Image [android.media.image]) the values were 3264 and 2448



the values were 3264 and 2448 pixels respectively. I have tried distancing the upc from the phone device, and that has not made an impact either. The List<FirebaseVisionBarcode> is still empty.

Does anybody know what could cause this issue.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1