-
-
Notifications
You must be signed in to change notification settings - Fork 16.5k
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 the coordinates of the bounding box in YOLO object detection? #388
Comments
Hello @milind-soni, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook , 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 that 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:
For more information please visit https://www.ultralytics.com. |
@milind-soni detection results are available here: Lines 92 to 102 in ea34f84
|
@glenn-jocher could you help me crop the detected objects using the bounding box coordinates |
I too have the same issue |
@fahimnawaz7 you can get the coordinates within detect.py, or you can use YOLOv5 from PyTorch Hub. See https://pytorch.org/hub/ultralytics_yolov5/ import cv2
import torch
from PIL import Image
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).autoshape() # for PIL/cv2/np inputs and NMS
# Images
for f in ['zidane.jpg', 'bus.jpg']: # download 2 images
print(f'Downloading {f}...')
torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/releases/download/v1.0/' + f, f)
img1 = Image.open('zidane.jpg') # PIL image
img2 = cv2.imread('bus.jpg')[:, :, ::-1] # OpenCV image (BGR to RGB)
imgs = [img1, img2] # batched list of images
# Inference
results = model(imgs, size=640) # includes NMS
# Results
results.print() # print results to screen
results.show() # display results
results.save() # save as results1.jpg, results2.jpg... etc.
# Data
print('\n', results.xyxy[0]) # print img1 predictions
# x1 (pixels) y1 (pixels) x2 (pixels) y2 (pixels) confidence class
# tensor([[7.47613e+02, 4.01168e+01, 1.14978e+03, 7.12016e+02, 8.71210e-01, 0.00000e+00],
# [1.17464e+02, 1.96875e+02, 1.00145e+03, 7.11802e+02, 8.08795e-01, 0.00000e+00],
# [4.23969e+02, 4.30401e+02, 5.16833e+02, 7.20000e+02, 7.77376e-01, 2.70000e+01],
# [9.81310e+02, 3.10712e+02, 1.03111e+03, 4.19273e+02, 2.86850e-01, 2.70000e+01]]) |
Hello, is it good practice to modify the standard detect.py? what about if there is a new version? could we upgrade our code without any damage. How about we call detect.py with --save.txt, we parse the bounding boxes and we crop the image after that? |
@AmelNozieres yes YOLOv5 Hub models can be created from custom weights. See PyTorch Hub Tutorial for details: Tutorials
|
@glenn-jocher is It possible to load custom weights in an environment that doesnt have internet access via yolov5 hub workflow? All examples I have come across require online access to the related github repo. |
@fcakyon yes, if the hub cache and models are stored locally stored internet access is not needed. |
@mycuriosity123 see torch hub page: |
@mycuriosity123 its assumed that users have at least a working knowledge of python here. To produce bounding box coordinates you simply copy and paste the code at the link I provided you: import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# Images
dir = 'https://github.com/ultralytics/yolov5/raw/master/data/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')] # batched list of images
# Inference
results = model(imgs)
# Results
results.print()
results.save() # or .show()
# Data
print(results.xyxy[0]) # print img1 predictions (pixels)
# x1 y1 x2 y2 confidence class
# tensor([[7.50637e+02, 4.37279e+01, 1.15887e+03, 7.08682e+02, 8.18137e-01, 0.00000e+00],
# [9.33597e+01, 2.07387e+02, 1.04737e+03, 7.10224e+02, 5.78011e-01, 0.00000e+00],
# [4.24503e+02, 4.29092e+02, 5.16300e+02, 7.16425e+02, 5.68713e-01, 2.70000e+01]]) |
@mycuriosity123 all the question you are asking are explained in the torch hub tutorial. I strongly recommend you start there. Tutorials |
have you achieved bounding box co ordinates? |
are you able to crop the detected objects? |
@mycuriosity123, I don't know if this is what your looking for but if you need the bounding boxes generated by yolov5 you have to add --save-txt to your command |
thanks @AmelNozieres |
I've managed to distignuish the frames that have labelled bounding boxes in them by saving the txt bounding box coords found and put them into txt files, which then gives me an insight into what frame has labels and which ones don't since the txt files are saved as 00321.txt therefore I know frame 321 has a bounding box. is there a way to only call images that only have associated txt files? |
####################################>>>>>>>>>> START of modified code <<<<<<<<<<<##########################
####################################>>>>>>>>>> END of modified code <<<<<<<<<<<########################## |
Is it possible to get the pixel's values and coordinates under the bounding boxes (I'm using yolov5 PyTorch)? |
@besmaGuesmi you can modify detect.py prediction labels on L158: Lines 156 to 162 in 1f31b7c
|
I'll try it and back to you! thanks @glenn-jocher |
@besmaGuesmi 👋 Hello! Thanks for asking about improving YOLOv5 🚀 training results. Most of the time good results can be obtained with no changes to the models or training settings, provided your dataset is sufficiently large and well labelled. If at first you don't get good results, there are steps you might be able to take to improve, but we always recommend users first train with all default settings before considering any changes. This helps establish a performance baseline and spot areas for improvement. If you have questions about your training results we recommend you provide the maximum amount of information possible if you expect a helpful response, including results plots (train losses, val losses, P, R, mAP), PR curve, confusion matrix, training mosaics, test results and dataset statistics images such as labels.png. All of these are located in your We've put together a full guide for users looking to get the best results on their YOLOv5 trainings below. Dataset
Model SelectionLarger models like YOLOv5x and YOLOv5x6 will produce better results in nearly all cases, but have more parameters, require more CUDA memory to train, and are slower to run. For mobile deployments we recommend YOLOv5s/m, for cloud deployments we recommend YOLOv5l/x. See our README table for a full comparison of all models.
python train.py --data custom.yaml --weights yolov5s.pt
yolov5m.pt
yolov5l.pt
yolov5x.pt
custom_pretrained.pt
python train.py --data custom.yaml --weights '' --cfg yolov5s.yaml
yolov5m.yaml
yolov5l.yaml
yolov5x.yaml Training SettingsBefore modifying anything, first train with default settings to establish a performance baseline. A full list of train.py settings can be found in the train.py argparser.
Further ReadingIf you'd like to know more a good place to start is Karpathy's 'Recipe for Training Neural Networks', which has great ideas for training that apply broadly across all ML domains: |
plot_one_box ain't working anymore. |
@Nishant123412 image annotations are now done using the new Annotator() class. See PR #4591 for details. |
I found the easiest way
|
@glenn-jocher what I found is number of objects found using detect.py and torch.hub.load are different. Can you please tell me why is that? Am I doing something wrong or is it something else? |
@debjyoti003 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀. We've created a few short guidelines below to help users provide what we need in order to get started investigating a possible problem. How to create a Minimal, Reproducible ExampleWhen asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:
For Ultralytics to provide assistance your code should also be:
If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem. Thank you! 😃 |
Can we hide labels and confidence scores using this code, I just want to plot the bounding box while doing GUI inference? |
@DrkPlne 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using Simple Inference ExampleThis example loads a pretrained YOLOv5s model from PyTorch Hub as import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # yolov5n - yolov5x6 official model
# 'custom', 'path/to/best.pt') # custom model
# Images
im = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, URL, PIL, OpenCV, numpy, list
# Inference
results = model(im)
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0] # im predictions (tensor)
results.pandas().xyxy[0] # im predictions (pandas)
# xmin ymin xmax ymax confidence class name
# 0 749.50 43.50 1148.0 704.5 0.874023 0 person
# 2 114.75 195.75 1095.0 708.0 0.624512 0 person
# 3 986.00 304.00 1028.0 420.0 0.286865 27 tie See YOLOv5 PyTorch Hub Tutorial for details. Good luck 🍀 and let us know if you have any other questions! |
How can i adjust the bounding box thickness in this code ? newbie in using yolov5 thanks for the reply in advance |
@muelclamor you might modify the source code or annotator arguments here and point pytorch hub to a local YOLOv5 clone: Lines 651 to 664 in 29d79a6
|
@glenn-jocher thank you it work and i have another question, is it possible to assign a variable for a certain bounding box or is it not ? i want to assign a variable when the yolov5 detect a certain object then connect it to my excel to output it via arduino thanks in advance |
@muelclamor you can export |
Gostaria de saber tambem! |
@rodrigoviannini 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using Simple Inference ExampleThis example loads a pretrained YOLOv5s model from PyTorch Hub as import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # yolov5n - yolov5x6 official model
# 'custom', 'path/to/best.pt') # custom model
# Images
im = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, URL, PIL, OpenCV, numpy, list
# Inference
results = model(im)
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0] # im predictions (tensor)
results.pandas().xyxy[0] # im predictions (pandas)
# xmin ymin xmax ymax confidence class name
# 0 749.50 43.50 1148.0 704.5 0.874023 0 person
# 2 114.75 195.75 1095.0 708.0 0.624512 0 person
# 3 986.00 304.00 1028.0 420.0 0.286865 27 tie
results.pandas().xyxy[0].value_counts('name') # class counts (pandas)
# person 2
# tie 1 See YOLOv5 PyTorch Hub Tutorial for details. Good luck 🍀 and let us know if you have any other questions! |
Simple Example for Custom Modelimport torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5m, yolov5l, yolov5x, etc.
# model = torch.hub.load('ultralytics/yolov5', 'custom', 'path/to/best.pt') # custom trained model
# Images
im = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, URL, PIL, OpenCV, numpy, list
# Inference
results = model(im)
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0] # im predictions (tensor)
results.pandas().xyxy[0] # im predictions (pandas) |
simple you can use "--save-txt" and or "--save-crop" arguments in python detect.py Example in colab |
which variable specifically hold values of co ordinates of bounding boxes? |
I have few questions. I am trying to measure the height and width of an object in YoloV5. The road map I am having in my mind is that the coordinates of bounding box are available and can be saved with --save-txt command, so with these bounding box coordinates we can calculate Pixel in selected area with OpenCV and as per the size of the image we can calculate height and width although better way is to use Aruco marker but I am leaving the Aruco marker step for now. |
Hi glenn-jocher, |
too hard to me |
Maybe it will be easier to use |
Is it possible to get coordinates for the bounding boxes when the object detection is used on a video? Can't seem to find this anywhere. |
@mathhauk 👋 Hello! Yes, it's absolutely possible to obtain bounding box coordinates from YOLOv5 object detection in a video. In fact, this can be easily achieved using YOLOv5 by saving detection results to a text file using the Afterwards, these coordinates can be utilized to calculate pixel locations using OpenCV or to convert to absolute coordinates based on the size of the image or frame from the video. If you have any other questions or need further assistance, feel free to ask! |
Hi @glenn-jocher, I wanted to ask you if there is any possibility to extract detected image as nparray to carry out further processing, for example ocr. I saw that results.crop(save=False) returns 'Im', what seems to be image array containging information about detected object limited by the boundingbox. |
@HankSteel77 yes, it is indeed possible to extract the detected image as an nparray for further processing such as OCR. You can achieve this by using the |
@glenn-jocher Thanks for fast answer! I wanted to ask if there is build in functionality that allows to extract data as array like results.ims[0] or something that extends functionality of results.crop. I searched the common.py file but i had trouble to understand how to simply obtain information only about 'Im' array. In a nutshell, i just wanted to know if there is build in function that allows to receive information about 'Im' in "air', without saving the and parsing data. I would be grateful if you told me how to do it. |
@HankSteel77 🎉 Yes, there is a straightforward built-in functionality in YOLOv5 that enables you to obtain the "Im" array without having to save and parse the data. You can access the "Im" array directly using the |
i want to extract the class id from the bounding box but
|
@Manaschintawar hello! To extract the class ID along with the bounding box details, you can access the results = model.predict(frame)
df = results[0].pandas().xyxy[0] # results in a DataFrame
for index, row in df.iterrows():
print(row['class']) # This prints the class ID This will give you the class ID for each detected object in your frame. If you have any more questions, feel free to ask! |
what is the syntax for yolov8??
…On Fri, 29 Mar 2024 at 01:32, Glenn Jocher ***@***.***> wrote:
@Manaschintawar <https://github.com/Manaschintawar> hello! To extract the
class ID along with the bounding box details, you can access the
results[0].pandas().xyxy attribute, which provides a Pandas DataFrame
including class IDs. Here's how you can modify your code:
results = model.predict(frame)df = results[0].pandas().xyxy[0] # results in a DataFrame
for index, row in df.iterrows():
print(row['class']) # This prints the class ID
This will give you the class ID for each detected object in your frame. If
you have any more questions, feel free to ask!
—
Reply to this email directly, view it on GitHub
<#388 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BBPKZ6BYN6WZTHJQJJC7PHLY2RSOFAVCNFSM4OYYCLIKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBSGYYDCMRXHE4A>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@Manaschintawar hey there! YOLOv8 employs similar syntax for working with results as YOLOv5. For extracting class IDs and bounding boxes, you can use the results = model.predict(frame)
df = results.pandas().xyxy[0] # Obtain a DataFrame with detection details
for index, row in df.iterrows():
print(row['class']) # Print out the class ID If YOLOv8 has brought any specific changes or additional features, I recommend checking out the official documentation or the updates section of the GitHub repo. Let me know if there's anything else you need! 🌟 |
hai sir, can you give code for appear the coordinate with bounding box after Run evaluation image, |
|
Hello @tejasri19, Great to hear that you have a working solution! If you want to display the coordinates of the bounding boxes on the evaluation image, you can modify your code to include drawing the bounding boxes on the image. Here's an example of how you can achieve this using OpenCV: import cv2
import matplotlib.pyplot as plt
# Load the image
image_path = "path to image"
image = cv2.imread(image_path)
# Perform inference
results = model(image_path)
# Extract bounding box coordinates and class names
boxes = []
scores = []
for box in results[0].boxes:
cords = box.xyxy[0].tolist()
x1, y1, x2, y2 = [round(x) for x in cords]
score = box.conf[0].item() # Assuming the confidence score is available in box.conf
cls = results[0].names[box.cls[0].item()]
boxes.append([x1, y1, x2, y2, score, cls])
scores.append(score)
# Draw the bounding box on the image
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(image, f'{cls} {score:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the image with bounding boxes
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
print("Boxes:", boxes)
print("Scores:", scores) This code will draw the bounding boxes and class labels on the image and display it using |
❔Question
I need to get the bounding box coordinates generated in an image using the object detection. How do I achieve that
The text was updated successfully, but these errors were encountered: