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

How to get the coordinates of the bounding box in YOLO object detection? #388

Closed
milind-soni opened this issue Jul 13, 2020 · 61 comments · Fixed by #7129
Closed

How to get the coordinates of the bounding box in YOLO object detection? #388

milind-soni opened this issue Jul 13, 2020 · 61 comments · Fixed by #7129
Labels
question Further information is requested

Comments

@milind-soni
Copy link

milind-soni commented Jul 13, 2020

❔Question

I need to get the bounding box coordinates generated in an image using the object detection. How do I achieve that

@milind-soni milind-soni added the question Further information is requested label Jul 13, 2020
@github-actions
Copy link
Contributor

github-actions bot commented 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 Open In Colab, 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:

  • 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.

For more information please visit https://www.ultralytics.com.

@glenn-jocher
Copy link
Member

@milind-soni detection results are available here:

yolov5/detect.py

Lines 92 to 102 in ea34f84

# Write results
for *xyxy, conf, cls in det:
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

@ankamdeepika
Copy link

@glenn-jocher could you help me crop the detected objects using the bounding box coordinates

@fahimnawaz7
Copy link

I too have the same issue

@glenn-jocher
Copy link
Member

glenn-jocher commented 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/

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]])

@AmelNozieres
Copy link

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
Copy link
Member

glenn-jocher commented Feb 16, 2021

@AmelNozieres yes YOLOv5 Hub models can be created from custom weights. See PyTorch Hub Tutorial for details:

Tutorials

@fcakyon
Copy link
Member

fcakyon commented 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
Copy link
Member

@fcakyon yes, if the hub cache and models are stored locally stored internet access is not needed.

@glenn-jocher
Copy link
Member

@glenn-jocher
Copy link
Member

glenn-jocher commented 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:

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]])

@glenn-jocher
Copy link
Member

glenn-jocher commented 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
Copy link

❔Question

I need to get the bounding box coordinates generated in an image using the object detection. How do I achieve that

have you achieved bounding box co ordinates?

@mycuriosity123
Copy link

@glenn-jocher could you help me crop the detected objects using the bounding box coordinates

are you able to crop the detected objects?

@AmelNozieres
Copy link

@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
!python detect.py --weights runs/train/yolov5s_results/weights/best.pt --img 416 --conf 0.4 --source ../test/images --save-txt
after the detection is achieved you will see that for each image you have now a txt file with the bounding boxes.
If you need to crop the image with this bounding boxes you can add some lines in detect.py. There is another issue with the code shared here #803

@mycuriosity123
Copy link

@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
!python detect.py --weights runs/train/yolov5s_results/weights/best.pt --img 416 --conf 0.4 --source ../test/images --save-txt
after the detection is achieved you will see that for each image you have now a txt file with the bounding boxes.
If you need to crop the image with this bounding boxes you can add some lines in detect.py. There is another issue with the code shared here #803

thanks @AmelNozieres
hurray working!!!

@SWRIGH211
Copy link

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?

@AdityaKaran109
Copy link

@glenn-jocher could you help me crop the detected objects using the bounding box coordinates

                if save_img or view_img:  # Add bbox to image
                    label = '%s %.2f' % (names[int(cls)], conf)

####################################>>>>>>>>>> START of modified code <<<<<<<<<<<##########################
#############>> commented this line to not to print bounding box on image
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

                    x1 = int(xyxy[0].item())
                    y1 = int(xyxy[1].item())
                    x2 = int(xyxy[2].item())
                    y2 = int(xyxy[3].item())
                    bbox_points=[x1, y1, x2, y2]
                    confidence_score = conf
                    class_index = cls
                    object_name = names[int(cls)]

                    print('bounding box is ', x1, y1, x2, y2)
                    print('class index is ', class_index)
                    print('detected object name is ', object_name)
                    original_img = im0
                    
                    i=0
                    for i in bbox_points:
                    	cropped_img = im0[y1:y2, x1:x2]
                    	cv2.imwrite("/content/drive/MyDrive/YOLOV5_CUSTOM/crop/thumbnail%04i.png" %i, cropped_img) ###-----put the output folder path here---####
                    	i+=1

                    #cv2.imwrite('test.png',cropped_img) ### >>>>>> to retain all cropped picture give different name for each pictures, else it will overwrite and only last image will be saved.

####################################>>>>>>>>>> END of modified code <<<<<<<<<<<##########################

@Guemann-ui
Copy link

Guemann-ui commented Jul 28, 2021

Is it possible to get the pixel's values and coordinates under the bounding boxes (I'm using yolov5 PyTorch)?
Thanks.

@glenn-jocher
Copy link
Member

@besmaGuesmi you can modify detect.py prediction labels on L158:

yolov5/detect.py

Lines 156 to 162 in 1f31b7c

if save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
plot_one_box(xyxy, im0, label=label, color=colors(c, True), line_thickness=line_thickness)
if save_crop:
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)

@Guemann-ui
Copy link

I'll try it and back to you! thanks @glenn-jocher

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 28, 2021

@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 project/name directory, typically yolov5/runs/train/exp.

We've put together a full guide for users looking to get the best results on their YOLOv5 trainings below.

Dataset

  • Images per class. ≥ 1500 images per class recommended
  • Instances per class. ≥ 10000 instances (labeled objects) per class recommended
  • Image variety. Must be representative of deployed environment. For real-world use cases we recommend images from different times of day, different seasons, different weather, different lighting, different angles, different sources (scraped online, collected locally, different cameras) etc.
  • Label consistency. All instances of all classes in all images must be labelled. Partial labelling will not work.
  • Label accuracy. Labels must closely enclose each object. No space should exist between an object and it's bounding box. No objects should be missing a label.
  • Background images. Background images are images with no objects that are added to a dataset to reduce False Positives (FP). We recommend about 0-10% background images to help reduce FPs (COCO has 1000 background images for reference, 1% of the total). No labels are required for background images.

COCO Analysis

Model Selection

Larger 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.

YOLOv5 Models

  • Start from Pretrained weights. Recommended for small to medium sized datasets (i.e. VOC, VisDrone, GlobalWheat). Pass the name of the model to the --weights argument. Models download automatically from the latest YOLOv5 release.
python train.py --data custom.yaml --weights yolov5s.pt
                                             yolov5m.pt
                                             yolov5l.pt
                                             yolov5x.pt
                                             custom_pretrained.pt
  • Start from Scratch. Recommended for large datasets (i.e. COCO, Objects365, OIv6). Pass the model architecture yaml you are interested in, along with an empty --weights '' argument:
python train.py --data custom.yaml --weights '' --cfg yolov5s.yaml
                                                      yolov5m.yaml
                                                      yolov5l.yaml
                                                      yolov5x.yaml

Training Settings

Before 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.

  • Epochs. Start with 300 epochs. If this overfits early then you can reduce epochs. If overfitting does not occur after 300 epochs, train longer, i.e. 600, 1200 etc epochs.
  • Image size. COCO trains at native resolution of --img 640, though due to the high amount of small objects in the dataset it can benefit from training at higher resolutions such as --img 1280. If there are many small objects then custom datasets will benefit from training at native or higher resolution. Best inference results are obtained at the same --img as the training was run at, i.e. if you train at --img 1280 you should also test and detect at --img 1280.
  • Batch size. Use the largest --batch-size that your hardware allows for. Small batch sizes produce poor batchnorm statistics and should be avoided.
  • Hyperparameters. Default hyperparameters are in hyp.scratch.yaml. We recommend you train with default hyperparameters first before thinking of modifying any. In general, increasing augmentation hyperparameters will reduce and delay overfitting, allowing for longer trainings and higher final mAP. Reduction in loss component gain hyperparameters like hyp['obj'] will help reduce overfitting in those specific loss components. For an automated method of optimizing these hyperparameters, see our Hyperparameter Evolution Tutorial.

Further Reading

If 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:
http://karpathy.github.io/2019/04/25/recipe/

@Nishant123412
Copy link

@milind-soni detection results are available here:

yolov5/detect.py

Lines 92 to 102 in ea34f84

# Write results
for *xyxy, conf, cls in det:
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

plot_one_box ain't working anymore.

@glenn-jocher
Copy link
Member

@Nishant123412 image annotations are now done using the new Annotator() class. See PR #4591 for details.

@baha2046a
Copy link

I found the easiest way

import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5l6', pretrained=True)

frame = cv2.imread(path)
detections = model(frame[..., ::-1])
results = detections.pandas().xyxy[0].to_dict(orient="records")
for result in results:
                con = result['confidence']
                cs = result['class']
                x1 = int(result['xmin'])
                y1 = int(result['ymin'])
                x2 = int(result['xmax'])
                y2 = int(result['ymax'])
                # Do whatever you want
                cv2.rectangle(frame, (x1, y1), (x2, y2), COLORS[0], 2)

@debjyoti003
Copy link

@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?

@glenn-jocher
Copy link
Member

glenn-jocher commented Dec 23, 2021

@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 Example

When 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:

  • Minimal – Use as little code as possible to produce the problem
  • Complete – Provide all parts someone else needs to reproduce the problem
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • Current – Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • Unmodified – Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code ⚠️.

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! 😃

@mansi733
Copy link

I found the easiest way

import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5l6', pretrained=True)

frame = cv2.imread(path)
detections = model(frame[..., ::-1])
results = detections.pandas().xyxy[0].to_dict(orient="records")
for result in results:
                con = result['confidence']
                cs = result['class']
                x1 = int(result['xmin'])
                y1 = int(result['ymin'])
                x2 = int(result['xmax'])
                y2 = int(result['ymax'])
                # Do whatever you want
                cv2.rectangle(frame, (x1, y1), (x2, y2), COLORS[0], 2)

Can we hide labels and confidence scores using this code, I just want to plot the bounding box while doing GUI inference?

@glenn-jocher
Copy link
Member

glenn-jocher commented Jun 6, 2022

@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 detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

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!

@muelclamor
Copy link

@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]])

How can i adjust the bounding box thickness in this code ? newbie in using yolov5 thanks for the reply in advance

@glenn-jocher
Copy link
Member

@muelclamor you might modify the source code or annotator arguments here and point pytorch hub to a local YOLOv5 clone:

yolov5/models/common.py

Lines 651 to 664 in 29d79a6

if show or save or render or crop:
annotator = Annotator(im, example=str(self.names))
for *box, conf, cls in reversed(pred): # xyxy, confidence, class
label = f'{self.names[int(cls)]} {conf:.2f}'
if crop:
file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None
crops.append({
'box': box,
'conf': conf,
'cls': cls,
'label': label,
'im': save_one_box(box, im, file=file, save=save)})
else: # all others
annotator.box_label(box, label if labels else '', color=colors(cls))

@muelclamor
Copy link

@muelclamor you might modify the source code or annotator arguments here and point pytorch hub to a local YOLOv5 clone:

yolov5/models/common.py

Lines 651 to 664 in 29d79a6

if show or save or render or crop:
annotator = Annotator(im, example=str(self.names))
for *box, conf, cls in reversed(pred): # xyxy, confidence, class
label = f'{self.names[int(cls)]} {conf:.2f}'
if crop:
file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None
crops.append({
'box': box,
'conf': conf,
'cls': cls,
'label': label,
'im': save_one_box(box, im, file=file, save=save)})
else: # all others
annotator.box_label(box, label if labels else '', color=colors(cls))

@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

@glenn-jocher
Copy link
Member

@muelclamor you can export results.pandas() to Excel, see https://pandas.pydata.org/docs/reference/api/pandas.ExcelWriter.html

@rodrigoviannini
Copy link

rodrigoviannini commented Jul 13, 2022

Olá, é uma boa prática modificar o detect.py padrão? e se houver uma nova versão? poderíamos atualizar nosso código sem nenhum dano. Que tal chamarmos detect.py com --save.txt, analisamos as caixas delimitadoras e cortamos a imagem depois disso? Também existe uma função como torch.hub.load para chamar com meus pesos?

Gostaria de saber tambem!

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 13, 2022

@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 detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

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!

BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this issue Aug 26, 2022
@OttomanZ
Copy link

Simple Example for Custom Model

import 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)

@daquarti
Copy link

simple you can use "--save-txt" and or "--save-crop" arguments in python detect.py

Example in colab
!python detect.py --weights /content/yolov5/runs/train/exp/weights/best.pt --img 640 --conf 0.01 --source data/images/ --save-txt --save-crop

@CyberpunkCoder101
Copy link

@milind-soni detection results are available here:

yolov5/detect.py

Lines 92 to 102 in ea34f84

# Write results
for *xyxy, conf, cls in det:
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

which variable specifically hold values of co ordinates of bounding boxes?

@arshanabbas
Copy link

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.

@lienpham83
Copy link

Hi glenn-jocher,
I would like get the bounding box with the absolute coordinates from YOLOv5 predictions. Could you please help me to do this? In the Simple Example for Custom Model, I can get only the relative coordinates. Many thanks

@swn0000
Copy link

swn0000 commented Jan 27, 2023

too hard to me

@ChanHaoHao
Copy link

Maybe it will be easier to use --save-txt to get the coordinates of the boxes of each figure in YOLO form.

@mathhauk
Copy link

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.

@glenn-jocher
Copy link
Member

@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!

@HankSteel77
Copy link

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
Copy link
Member

@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
Copy link

@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
Copy link
Member

@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
Copy link

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 😣!!

@glenn-jocher
Copy link
Member

@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!

@Manaschintawar
Copy link

Manaschintawar commented Apr 8, 2024 via email

@glenn-jocher
Copy link
Member

@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 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! 🌟

@Muhnajh
Copy link

Muhnajh commented May 10, 2024

@milind-soni detection results are available here:

yolov5/detect.py

Lines 92 to 102 in ea34f84

# Write results
for *xyxy, conf, cls in det:
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

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
Copy link

tejasri19 commented Aug 15, 2024

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.

@glenn-jocher
Copy link
Member

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 matplotlib. If you encounter any issues or have further questions, feel free to ask! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.