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

Why doesn't ".crop()" or "--save-crop" do any cropping? #9585

Closed
1 task done
jmiller-dr opened this issue Sep 25, 2022 · 7 comments
Closed
1 task done

Why doesn't ".crop()" or "--save-crop" do any cropping? #9585

jmiller-dr opened this issue Sep 25, 2022 · 7 comments
Labels
question Further information is requested Stale

Comments

@jmiller-dr
Copy link

jmiller-dr commented Sep 25, 2022

Search before asking

Question

I use results.crop(), results.crop(save=True), and

python3 yolov5/detect.py --save-crop --data 'myfile.yaml' --source 'images/train/*.jpeg' --weights 'yolov5/runs/train/exp22/weights/best.pt'

and it saves the images, but all of them are the exact same dimensions as the original. How do I crop and what are the options to crop()? I don't see any documentation of it, just a couple of references to it in the tutorials that don't seem to actually crop. Is there some undocumented margin parameter or something?

Additional

No response

@jmiller-dr jmiller-dr added the question Further information is requested label Sep 25, 2022
@glenn-jocher
Copy link
Member

@jmiller-dr 👋 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!

@glenn-jocher
Copy link
Member

@jmiller-dr just tested myself, crops work correctly for me, I'm not able to reproduce any problem:

Screenshot 2022-09-25 at 20 21 53

@jmiller-dr
Copy link
Author

jmiller-dr commented Sep 25, 2022 via email

@glenn-jocher
Copy link
Member

@jmiller-dr that's strange. If you can help us reproduce this in a common environment like a Colab notebook we could get started debugging it. What steps did you take to produce the bug?

@Denizzje
Copy link

Denizzje commented Sep 29, 2022

Edit: I posted here because I thought were no crops but they are in: runs/detect//crops. So in my case runs/detect/exp14/crops. I misremembered them into there being a general crops folder and overlooked it in the folder itself.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 30, 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 ⭐!

@github-actions github-actions bot added the Stale label Oct 30, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2022
@glenn-jocher
Copy link
Member

@Denizzje no worries! It's great that you found the crops in the runs/detect/expN/crops directory. If there's anything else we can help with or if you have further questions, feel free to reach out. Good luck with your project!

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