ultralytics / yolov5

YOLOv5 ๐Ÿš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.2k stars 16.43k forks source link

how to crop images after detection in yolov5s #1028

Closed harshmgoyal closed 4 years ago

harshmgoyal commented 4 years ago

โ”Question

how to take out coordinates of bounding box image after detection ? or is there any other method to crop ?

Additional context

github-actions[bot] commented 4 years ago

Hello @harshmgoyal, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

If this is a bug report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom model or data training question, please note Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

For more information please visit https://www.ultralytics.com.

cjsLindquist commented 4 years ago

Setting the '--save-txt' argument on detect.py will output the bounding box locations and classes into /inference/output/ with one file per image. You can simply read in these files and crop the bboxes out of the appropriate images or video frames using OpenCV etc.

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

shivprasad94 commented 3 years ago

https://github.com/ultralytics/yolov5/issues/2608#issuecomment-809094537

U can refer to my reply in the above link.

burhr2 commented 3 years ago

Hi! have added the saving option of the detected objects which will be in project/name/cropped when using detect.py. I have made a PR to this repo but you can check the changes in my folk yolov5

glenn-jocher commented 3 years ago

@harshmgoyal @cjsLindquist Prediction box cropping is now available in YOLOv5 via PR https://github.com/ultralytics/yolov5/pull/2827! PyTorch Hub models can use results.crop() or detect.py can be called with the --save-crop argument. Example usage:

python detect.py --save-crop
Screenshot 2021-04-20 at 23 50 51
bachimanchiajay commented 2 years ago

How to extract text from the --save-crop images having multiple classes will be saving the images in a crop folder i need the text as well for that specific cropped image can anyone help me how we can do that.

glenn-jocher commented 2 years ago

@bachimanchiajay ๐Ÿ‘‹ Hello! Thanks for asking about cropping results with YOLOv5 ๐Ÿš€. Cropping bounding box detections can be useful for training classification models on box contents for example. This feature was added in PR https://github.com/ultralytics/yolov5/pull/2827. You can crop detections using either detect.py or YOLOv5 PyTorch Hub:

detect.py

Crops will be saved under runs/detect/exp/crops, with a directory for each class detected.

python detect.py --save-crop
Original Crop

YOLOv5 PyTorch Hub

Crops will be saved under runs/detect/exp/crops if save=True, and also returned as a dictionary with crops as numpy arrays.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, custom

# Images
img = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, PIL, OpenCV, numpy, list

# Inference
results = model(img)

# Results
crops = results.crop(save=True) 
# -- or --
crops = results.crop(save=True, save_dir='runs/detect/exp')  # specify save dir

Good luck ๐Ÿ€ and let us know if you have any other questions!

bachimanchiajay commented 2 years ago

I can able to see the cropped results but i have to extract text from the cropped images before cropping i have to pass these predicted coordina To the textract for ocr.

On Tue, 18 Oct 2022 at 5:25 PM, Glenn Jocher @.***> wrote:

@bachimanchiajay https://github.com/bachimanchiajay ๐Ÿ‘‹ Hello! Thanks for asking about cropping results with YOLOv5 ๐Ÿš€. Cropping bounding box detections can be useful for training classification models on box contents for example. This feature was added in PR #2827 https://github.com/ultralytics/yolov5/pull/2827. You can crop detections using either detect.py or YOLOv5 PyTorch Hub: detect.py

Crops will be saved under runs/detect/exp/crops, with a directory for each class detected.

python detect.py --save-crop

[image: Original] https://user-images.githubusercontent.com/26833433/139577918-65cc8f5d-741d-4a88-8753-0154013c624d.jpg

[image: Crop] https://user-images.githubusercontent.com/26833433/139577896-aa26b07c-466d-4057-9bfe-b01b56007587.png YOLOv5 PyTorch Hub

Crops will be saved under runs/detect/exp/crops if save=True, and also returned as a dictionary with crops as numpy arrays.

import torch

Model

model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5m, yolov5l, yolov5x, custom

Images

img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list

Inference

results = model(img)

Results

crops = results.crop(save=True)

-- or --

crops = results.crop(save=True, save_dir='runs/detect/exp') # specify save dir

Good luck ๐Ÿ€ and let us know if you have any other questions!

โ€” Reply to this email directly, view it on GitHub https://github.com/ultralytics/yolov5/issues/1028#issuecomment-1282267078, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHQB7G663IDUTMWR7QSBX2DWD2F3JANCNFSM4RXYISZQ . You are receiving this because you were mentioned.Message ID: @.***>

gobspn commented 1 year ago

Can I change the file extension from .jpg to .png before used '--save-crop'.

glenn-jocher commented 1 year ago

@gobspn hello! Yes, you can change the file extension from .jpg to .png before using '--save-crop' in detect.py or results.crop(save=True) in YOLOv5 PyTorch Hub. The saved cropped images will also have the same file extension as the input images.