Search code examples
javaandroidbitmapcrop

Bounding Box Cropping


I did object detection. And I got it in the bounding box. I'm trying to crop inside the bounding box. but it doesn't work. in debug mode, it gets stuck on the if line. THANKS!!!

         result.setLocation(location);
         mappedRecognitions.add(result);
 
                                    if ((int) location.left > 0 && (int) location.top > 0 && ((int) location.left + (int) location.width()) < xyBitmap.getWidth()
                                          && ((int) location.top + (int) location.height()) < xyBitmap.getHeight()) {
                                     mycroppedBitmap = Bitmap.createBitmap(xyBitmap,
                                            (int) location.left,
                                            (int) location.top,
                                            (int) location.width(),
                                            (int) location.height()
                                    );                                      
 mycroppedBitmap = Bitmap.createScaledBitmap(mycroppedBitmap, 224, 224, true);

Coordinates

enter image description here
enter image description here

DetectorActivity.java File

https://github.com/usertttwm/CropAndClassificationModel/blob/main/DetectorActivity.java

For classification after crop: TFLiteObjectDetectionAPIModel.java

https://github.com/usertttwm/CropAndClassificationModel/blob/main/TFLiteObjectDetectionAPIModel

NEW!!!!!!

if (1==1) {

mycroppedBitmap = Bitmap.createBitmap(croppedBitmap,
1,1,224,224

);

final String resultLabel = detector1.recognizeImage1(mycroppedBitmap);

RESULTS croppedBitmap

croppedBitmap mycroppedBitmap

enter image description here

DETECTIVE (I'm trying to crop the bounding box)

enter image description here

UPDATE!!!!!!!!!!!!!!!!!!!!!!!

 mappedRecognitions.add(result);//TRAY DETECTIVE
                            Bitmap copyOfOriginalBitmap = rgbFrameBitmap.copy(rgbFrameBitmap.getConfig(), true);
                            Bitmap secondBitmap = Bitmap.createBitmap(copyOfOriginalBitmap,
                                    (int)  location.left, (int)location.top, (int)location.right, (int)location.bottom);
                            Bitmap secondScaledBitmap = Bitmap.createScaledBitmap(secondBitmap, 224, 224, true);

                            final String resultLabel = detector1.recognizeImage1(secondScaledBitmap);

error

CROP WORKİNG UPDATE

 result.setLocation(location);
                        mappedRecognitions.add(result);
                        Bitmap copyOfOriginalBitmap = rgbFrameBitmap.copy(rgbFrameBitmap.getConfig(), true);
                        Bitmap secondBitmap = Bitmap.createBitmap(copyOfOriginalBitmap,
                                (int)location.left, (int)location.top, (int)location.right-(int)location.left, (int)location.bottom-(int)location.top);
                        Bitmap secondScaledBitmap = Bitmap.createScaledBitmap(secondBitmap, 224, 224, true);

                        final String resultLabel = detector1.recognizeImage1(secondScaledBitmap);

CROP cropTray

------------------RESOURCES--------------

https://github.com/AarohiSingla/TFLite-Object-Detection-Android-App-Tutorial-Using-YOLOv5 I used it for object detection with yolo.

https://www.youtube.com/watch?v=ZepT3N6aNFM&t=881s https://github.com/ivangrov/TensorFlow-Lite-Age-Gender-Estimation-on-Android/blob/main/tflite/TFLiteObjectDetectionAPIModel.java

I used it to pass through the crop and classification model.


Solution

  • Its a little hard to debug this without the code and the information that you provided, but here are some clear steps on how I would solve this and maybe you can check on your side and see where your bug is. What I can see is that you are trying to crop the second image from the first cropped image, which is wrong, you should try to crop the image for the second interpreter from the original image, so the process should look smth like this:

    1. First create a copy of the original bitmap (since you are scaling it down)
    Bitmap originalBitmap = ...; // your original bitmap here
    Bitmap copyOfOriginalBitmap = originalBitmap.copy(originalBitmap.getConfig(), true);
    
    1. After you run first interpreter (using originalBitmap) you will get the results that coordinates are in range from 0 to width or height of the Interpreter input (this can change, on your logs I see that coordinates are scaled up which is what we need)
    Recognition myObject = ...; // you can get the detection that you want here, you can do this with a simple for loop
    // now create second bitmap by cropping the original and using the scaled up coordinates
    Bitmap secondBitmap = Bitmap.createBitmap(copyOfOriginalBitmap, myObject.location.left, myObject.location.top, myObject.location.right, myObject.location.bottom)
    // now create the second scaled bitmap for your second Interpreter with the width and height of the interpreter size
    Bitmap secondScaledBitmap = Bitmap.createScaledBitmap(secondBitmap, width, height, true)
    

    And you should be good to go.