The Object detection sample is working well in detecting objects and rounding the objects as rectangles, however I want to save those highlighted images as bitmaps and save to the gallery.
I was able to get the empty white bitmaps by taking the boundaries of the boxes. However I want the full image cropped and saved to my phone gallery or as a file. How do I do it?
Here’s how you can save the cropped images with detected objects to your phone gallery or as files:
1. Capture the full image:
Before processing the image for object detection, make sure you capture the entire image you want to save. This could be done through the camera app or by loading an existing image.
2. Get the object bounding boxes:
Once you have the full image, proceed with your object detection and retrieve the bounding boxes for each identified object. These boxes define the rectangular areas surrounding the detected objects.
3. Crop the image based on bounding boxes:
Loop through each bounding box.
Use the box coordinates (top, left, width, height) to extract the corresponding section from the original full image. You can achieve this by slicing the image array based on these values.
Create a new image (bitmap) with the same dimensions as the extracted section.
Copy the pixels from the extracted section to the new bitmap.
4. Save the cropped image:
Choose your preferred saving method:
Gallery: Use the appropriate API provided by your mobile development framework (e.g., MediaStore in Android) to save the cropped bitmap to the phone’s gallery.
File: Specify the desired file path and write the cropped bitmap data (e.g., using PNG format) to that file.
Additional tips:
You can enhance the saved image by adding information like the detected object label or confidence score alongside the cropped area.
Consider incorporating error handling to gracefully handle potential issues like invalid bounding boxes or saving failures.
I hope this helps! Feel free to ask if you have any further questions or need clarification on specific steps.