-
-
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 crop the detected object after training using YOLO? #5412
Comments
@samahwaleed 👋 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.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! |
Thank you. but I want to crop the images using the results saved in txt file.
I got this error,
|
@samahwaleed crops are produced by the same text file predictions, but sure you can write your own script to do this as well. |
This is my code.
|
👋 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 ⭐! |
is there a method to cut-save multiple files in multiple folders? |
@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.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! |
can we only detect 'person' class and only save this class? Thank you!!! |
python detect.py --source data/images --weights yolov5s.pt --classes 0 this might help you, use |
I just want to chime in that cropping the image using the coordinates from the txt-file does not give the same image as using the save-crops function. The save-crops function result in a slightly bigger crop compared to just using the coordinates. This can be a very important distinction for small detections/images and sequential yolo detections |
Is there a better of way of doing this in YOLOv8 ? |
@varunpusarla hello, YOLOv8 is not an existing version of YOLO. The latest version is YOLOv5, which is currently maintained by Ultralytics. With YOLOv5, you can detect objects in images and videos using pre-trained models or custom trained models with your own dataset. If you have any specific questions about implementation or functionality with YOLOv5, feel free to ask and we'll be happy to help. Best regards. |
@glenn-jocher how can i maintain scale during cropping? because the generated crops vary in dimensions, yet i wound want to use the generated crops to measure object size. hence the dimensions need to be same scale |
@Sarai256 you can maintain the scale during cropping by calculating the width and height of the crop based on a fixed aspect ratio or a fixed scale factor. For example, if you want to maintain a fixed aspect ratio (e.g., 1:1) during cropping, you can calculate the width and height of the crop based on the longer side of the bounding box. Alternatively, if you want to maintain a fixed scale factor (e.g., 0.5), you can scale the width and height of the crop based on the original width and height of the image. By applying these calculations, you can ensure that the dimensions of the generated crops are in the same scale as the original objects. |
@glenn-jocher , thank you for the reply. however I am not sure to which part of the code i should include the scale factor, |
@Sarai256 Thank you for reaching out! In order to maintain a specific scale during cropping, you would need to modify the code in the The command One way to implement scaling is by modifying the cropping logic in the If you are a beginner, I would recommend going through the Feel free to ask any further questions if you need any clarification or assistance with the code. Happy coding! |
Search before asking
Question
I am using YOLO for model training. I used save-txt to save the results in txt files. I want to crop the detected object and get the pixel values for each detected object from the txt file. The txt file has labeled, bbox coordinates and score.
Any help would be highly appreciated!
Additional
No response
The text was updated successfully, but these errors were encountered: