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-save more than 1 image file? #8310

Closed
1 task done
Raymon360 opened this issue Jun 23, 2022 · 10 comments
Closed
1 task done

How to crop-save more than 1 image file? #8310

Raymon360 opened this issue Jun 23, 2022 · 10 comments
Labels
question Further information is requested Stale

Comments

@Raymon360
Copy link

Search before asking

Question

How to crop-save more than 1 image file in multiple folders with size according to trained detection model bounding box?

Additional

No response

@Raymon360 Raymon360 added the question Further information is requested label Jun 23, 2022
@barney2074
Copy link

barney2074 commented Jun 23, 2022

In detect.py- you have the --save-crop option

@glenn-jocher
Copy link
Member

@Raymon360 👋 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 .show(), .save(), .print(), .pandas(), etc.

Good luck 🍀 and let us know if you have any other questions!

@Raymon360
Copy link
Author

Thanks for the response, but can cut save detection be used to detect multiple image files at once?

e.g. 10 images from each subfolder

@barney2074
Copy link

@Raymon360
you might need to provide more info- sample, screenshot etc, or at least describe what you are doing
It sounds like you are feeding in a single image to detect.py. If you input multiple images (a folder, or glob path)- then you'll get multiple cropped outputs

@Raymon360
Copy link
Author

you might need to provide more info- sample, screenshot etc, or at least describe what you are doing
It sounds like you are feeding in a single image to detect.py. If you input multiple images (a folder, or glob path)- then you'll get multiple cropped outputs

can you provide a code example of what you are describing?

@barney2074
Copy link

Yolov5/Ultralytics documentation is pretty good- I'd start by reading it
image

@Raymon360
Copy link
Author

oh I just got it, thanks for helping me @barney2074 and @glenn-jocher

@Raymon360
Copy link
Author

Yolov5/Ultralytics documentation is pretty good- I'd start by reading it image

image

When I try it, the system can only work on 1 folder. How to make the system work if you have a lot of subfolders?
For example: "folders/subfolders/files"

@glenn-jocher
Copy link
Member

@Raymon360 google glob documentation if you're not familiar with it

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

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

3 participants