vladiH / flutter_vision

A Flutter plugin for managing both Yolov5 model and Tesseract v4, accessing with TensorFlow Lite 2.x. Support object detection, segmentation and OCR on both iOS and Android.
https://pub.dev/packages/flutter_vision
MIT License
72 stars 31 forks source link

Cropping the detected region #28

Closed mahdi-madadi closed 1 year ago

mahdi-madadi commented 1 year ago

Please help me to crop the region of interest in flutter(Only the part of image where the model detected). I have trained a yolov8 model that detect some specific text on drug image, now I want to crop the image and display only the detected region not all image, but right now I can not crop properly the region, and I have trouble. It crop but other region not the region of interest

vladiH commented 1 year ago

You can use the "box"( [x1:left, y1:top, x2:right, y2:bottom, class_confidence]) coordinates to define a cropping region on the original image. Here's a high-level idea of how you can approach this:

  1. Get the "box" coordinates from the YoloV8 model's output.

  2. Use these coordinates to define the region of interest (ROI) on the image. You can calculate the width and height of the ROI using x2 - x1 and y2 - y1.

  3. Crop the image to the ROI. You can use a widget like Image Cropper or simply use the Flutter Image widget with a clip property to display only the cropped region.

Here's a code snippet illustrating how to crop and display the detected region using the Flutter Image widget:

// Assuming you have the box coordinates
final x1 = box['x1'];
final y1 = box['y1'];
final x2 = box['x2'];
final y2 = box['y2'];

// Calculate width and height of the ROI
final roiWidth = x2 - x1;
final roiHeight = y2 - y1;

// Crop the image to the ROI
final croppedImage = Image.memory(
  Uint8List.fromList(imageBytes), // Replace with your image data
  width: roiWidth,
  height: roiHeight,
  fit: BoxFit.fill,
  alignment: Alignment(x1 / originalImageWidth, y1 / originalImageHeight),
);

// Display the cropped region
return croppedImage;