-
-
Notifications
You must be signed in to change notification settings - Fork 16.1k
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 get cropped images from --save-crop in sequential order similar to original image? #7580
Comments
👋 Hello @ashmalvayani, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution. 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 training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available. For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com. RequirementsPython>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started: git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit. |
@ashmalvayani 👋 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. Crops are generally saved by order of confidence. detect.pyCrops will be saved under python detect.py --save-crop YOLOv5 PyTorch HubCrops will be saved under 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! |
Hi @ashmalvayani If you are using torch hub, look at #36. For sorted results i.e. to sort license plate digit detection left-to-right (x-axis):
Hope it helps! |
I have the same issue as @ashmalvayani. The saved cropped sequence is random. Any ways to let the save crop save from the most left x-axis to the most right x-axis? (Will edit some parts of the code in detect.py help?) |
@Andy31798 not random. Sorted by confidence. |
@glenn-jocher is there any parts in detect.py that I can modify so that the saved sequence will follow the order from the most left x-axis to the most right x-axis? |
@Andy31798 the for loop that processes the results is here: Lines 158 to 172 in c4cb7c6
|
@glenn-jocher can you recommend any changes in lines 158-172 to sort the results. I also found the same problem in this issue, can you tell the logic to sort via x-axis? |
### Solved In the yolov5 directory, you'll have a detect.py file, there's a code line for NMS (Non-Max Suppression) in line 126 (depending upon the time you're cloning the GitHub repository it may vary and add the following code.
This piece of code between ## solved my problem, however, it's giving me reversed sorted output so it's advised to use accordingly to your problem by removing sorted() for eg and many more. |
Hello @Ammar-Azman! Thanks for sharing the solution that worked best for you. I'm sure it will be helpful to others who are having a similar issue. It's great to see that you were able to make that small modification to detect.py, which is the part of the code that processes detection results. You simply added a line of code that sorts the detections from left to right based on their x-axis coordinates. It's important to know that sorting the results in a particular way depends on your specific use-case. In the solution you provided, you sorted the detections from left to right based on x-axis, which helped solve your issue. However, be aware that this solution will not work in all cases, as the properties of your input image and what you want to extract from it can vary. Thanks for reaching out and please let us know if you have any other questions or issues! |
Search before asking
SOLVED, see my last comment.
Question
I've run the inference of detecting multiple digits from an image, and the model is detecting all the numbers correctly; however, when I am cropping these digits and saving them using --save-crop, the outputs that I am getting are not in the order which is necessary for me to further link them to my next step.
the output should be 0 0 1 2 7, whereas the output is 1 7 0 2 0.
Is there any way to preserve the order? or anything through which I can rename the saved cropped images myself?
Additional
The text was updated successfully, but these errors were encountered: