Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to crop images after detection in yolov5s #1028

Closed
harshmgoyal opened this issue Sep 24, 2020 · 11 comments
Closed

how to crop images after detection in yolov5s #1028

harshmgoyal opened this issue Sep 24, 2020 · 11 comments
Labels
question Further information is requested Stale

Comments

@harshmgoyal
Copy link

❔Question

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

Additional context

@harshmgoyal harshmgoyal added the question Further information is requested label Sep 24, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Sep 24, 2020

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:

  • Cloud-based AI systems operating on hundreds of HD video streams in realtime.
  • Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
  • Custom data training, hyperparameter evolution, and model exportation to any destination.

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

@cjsLindquist
Copy link

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
Copy link
Contributor

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
Copy link

#2608 (comment)

U can refer to my reply in the above link.

@burhr2
Copy link
Contributor

burhr2 commented Apr 16, 2021

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
Copy link
Member

@harshmgoyal @cjsLindquist Prediction box cropping is now available in YOLOv5 via PR #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
Copy link

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
Copy link
Member

@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. 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
Copy link

bachimanchiajay commented Oct 18, 2022 via email

@gobspn
Copy link

gobspn commented May 8, 2023

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

@glenn-jocher
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

7 participants