You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to get the coordinates of the bounding box in YOLO object detection? #388
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:
Cloud-based AI systems operating on hundreds of HD video streams in realtime.
Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
Custom data training, hyperparameter evolution, and model exportation to any destination.
Reacted by Milind Soni, Salman Chen (陳成文; S.C.), zhocef, Sujay Sathya, Rohin Arora, Michael Zeng, Seonyoung Kim, Peter, Shreyas Shrawage, VeraYujiaDING and 18 more
Reacted by Akmal, Ruslan Murtazin, Ioanna Gogou, Kerolis Youssif Nasr, Felipe Lopes, Wingkit-Chou, Shang-Ching Liu, Jay , Shreyas Shrawage, elokuu and 23 more
Reacted by Nguyễn Minh Lý, Abhay Kumar Tiwari and jeannechaverot
Reacted by Minh Duc, Seif Mostafa, Kevin Sheu, Abhay Kumar Tiwari and Hisan
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?
Also is there a function as torch.hub.load to call with my weights?
Reacted by LI, Raman Jha, amaican, Hisan and Damian Ariel
@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.
@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:
Reacted by Pritom Mojumder, junaidalimughal, Shang-Ching Liu, Maaitrayo Das, elokuu, Marcello Graco, Surya Mandapati, Michael Shepherd, Nguyen Nguyen Anh, Apep13699 and 1 more
@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 --save-txt argument in the inference script. Each text file will contain the bounding box coordinates of the detected objects in YOLO format (x_center, y_center, width, height).
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 results.crop() method, which returns an image array containing the detected object limited by the bounding box. This can then be used for downstream tasks such as OCR. If you have any more questions or need additional assistance, feel free to ask!
@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 results.render() method. This method returns a list of cropped image arrays (not saved) from the results, making it simple and efficient to obtain the desired "Im" information. If you have any more questions or need further assistance, feel free to ask!
i want to extract the class id from the bounding box but
results = model.predict(frame)
a = results[0].boxes.xyxy
px = pd.DataFrame(a).astype("float")
for index, row in px.iterrows():
print(px) this is the code and when i am printing the value i dont get the class id in the last row of the extract data now for class id what should i do? plz help 😣!!
@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 DataFrameforindex, rowindf.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!
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.pandas().xyxy[0] attribute after making predictions. Here's a quick example:
results=model.predict(frame)
df=results.pandas().xyxy[0] # Obtain a DataFrame with detection detailsforindex, rowindf.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,
which part I must added from the code roboflow generate for Yolov5 which run at google colab
results = model("path to image")
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)
print("Boxes:", boxes)
print("Scores:", scores
```)
This is working.
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:
importcv2importmatplotlib.pyplotasplt# Load the imageimage_path="path to image"image=cv2.imread(image_path)
# Perform inferenceresults=model(image_path)
# Extract bounding box coordinates and class namesboxes= []
scores= []
forboxinresults[0].boxes:
cords=box.xyxy[0].tolist()
x1, y1, x2, y2= [round(x) forxincords]
score=box.conf[0].item() # Assuming the confidence score is available in box.confcls=results[0].names[box.cls[0].item()]
boxes.append([x1, y1, x2, y2, score, cls])
scores.append(score)
# Draw the bounding box on the imagecv2.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 boxesplt.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 matplotlib. If you encounter any issues or have further questions, feel free to ask! 😊
Activity
github-actions commentedon Jul 13, 2020
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.
glenn-jocher commentedon Jul 13, 2020
@milind-soni detection results are available here:
yolov5/detect.py
Lines 92 to 102 in ea34f84
ankamdeepika commentedon Jul 18, 2020
@glenn-jocher could you help me crop the detected objects using the bounding box coordinates
fahimnawaz7 commentedon Dec 24, 2020
I too have the same issue
glenn-jocher commentedon Dec 24, 2020
@fahimnawaz7 you can get the coordinates within detect.py, or you can use YOLOv5 from PyTorch Hub. See https://pytorch.org/hub/ultralytics_yolov5/
AmelNozieres commentedon Feb 16, 2021
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?
Also is there a function as torch.hub.load to call with my weights?
glenn-jocher commentedon Feb 16, 2021
@AmelNozieres yes YOLOv5 Hub models can be created from custom weights. See PyTorch Hub Tutorial for details:
Tutorials
fcakyon commentedon Feb 20, 2021
@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.
glenn-jocher commentedon Feb 20, 2021
@fcakyon yes, if the hub cache and models are stored locally stored internet access is not needed.
glenn-jocher commentedon Feb 24, 2021
@mycuriosity123 see torch hub page:
https://pytorch.org/hub/ultralytics_yolov5/
glenn-jocher commentedon Feb 24, 2021
@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:
glenn-jocher commentedon Feb 24, 2021
@mycuriosity123 all the question you are asking are explained in the torch hub tutorial. I strongly recommend you start there.
Tutorials
mycuriosity123 commentedon Feb 25, 2021
have you achieved bounding box co ordinates?
40 remaining items
ChanHaoHao commentedon Mar 6, 2023
Maybe it will be easier to use
--save-txtto get the coordinates of the boxes of each figure in YOLO form.mathhauk commentedon Mar 23, 2023
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.
Add PyTorch Hub `results.save(labels=False)` option (#7129)
glenn-jocher commentedon Nov 15, 2023
@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
--save-txtargument in the inference script. Each text file will contain the bounding box coordinates of the detected objects in YOLO format (x_center, y_center, width, height).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!
HankSteel77 commentedon Dec 11, 2023
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.
glenn-jocher commentedon Dec 11, 2023
@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
results.crop()method, which returns an image array containing the detected object limited by the bounding box. This can then be used for downstream tasks such as OCR. If you have any more questions or need additional assistance, feel free to ask!HankSteel77 commentedon Dec 11, 2023
@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.
glenn-jocher commentedon Dec 12, 2023
@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
results.render()method. This method returns a list of cropped image arrays (not saved) from the results, making it simple and efficient to obtain the desired "Im" information. If you have any more questions or need further assistance, feel free to ask!Manaschintawar commentedon Mar 28, 2024
i want to extract the class id from the bounding box but
results = model.predict(frame)
a = results[0].boxes.xyxy
px = pd.DataFrame(a).astype("float")
glenn-jocher commentedon Mar 28, 2024
@Manaschintawar hello! To extract the class ID along with the bounding box details, you can access the
results[0].pandas().xyxyattribute, which provides a Pandas DataFrame including class IDs. Here's how you can modify your code:This will give you the class ID for each detected object in your frame. If you have any more questions, feel free to ask!
Manaschintawar commentedon Apr 8, 2024
glenn-jocher commentedon Apr 9, 2024
@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.pandas().xyxy[0]attribute after making predictions. Here's a quick example: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! 🌟
Muhnajh commentedon May 10, 2024
hai sir, can you give code for appear the coordinate with bounding box after Run evaluation image,
which part I must added from the code roboflow generate for Yolov5 which run at google colab
tejasri19 commentedon Aug 15, 2024
glenn-jocher commentedon Aug 15, 2024
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:
This code will draw the bounding boxes and class labels on the image and display it using
matplotlib. If you encounter any issues or have further questions, feel free to ask! 😊