ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.46k stars 16.28k forks source link

Using the coordinates for each object #7964

Closed engineermechat closed 2 years ago

engineermechat commented 2 years ago

Search before asking

Question

Hello, i need your help on using the coordinates for my project. I trained my own custom dataset which contains 16 classes with yolov5s. It is detecting the objects almost perfectly on videos and real-time. Detecting is ok but i need to use the coordinates for calculating the positions and distance between objects in same class or in two different classes. I used two ways to access the coordinates.

First one is 'pred' ,when i print it on console with print(pred) i get the all coordinates of each object on the frame:

tensor([[747.00000, 401.00000, 114.00000, 712.00000, 0.87, 16], [117.00000, 196.00000, 100.00000, 711.00000, 0.89, 15], [423.00000, 430.00000, 516.00000, 720.00000, 0.78, 15], [981.00000, 310.00000, 103.00000, 419.00000, 0.92, 15]])

Also i tried print(pred[0][0][5]) , print(pred[0][0][4]) , print(pred[0][0][0])...... They printed smoothly the correct values.

Second one is this code :

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)

It prints like this on the console :

bounding box is 747 401 114 712 bounding box is 117 196 100 711 bounding box is 423 430 516 720 bounding box is 981 310 103 419

Problem: I can observe the coordinates of each frame while processing but can't use it properly. I tried to filter with class numbers:

if(c==15): print(pred)

or my second way

But it prints the all coordinates of all detected objects not only 15 and it doesn't print when the frame has two or more 15 class objects. I want to seperate the coordinates values of each bbox to use for disered object. My project is similar with social distance projects , therefore i searched a lot on social distance projects with yolov5 to find an understandable example for my project but i couldn't find. They just explain how to calculate the distance or how to write a function but i need to parse and use the coordinates first.

To make my need clear, i want to give an example project. Example: Picture a robotic arm, it detecs the 16 different types objects on a table and calculate of positions and grip them in a order which you determine in program(' first: take class 13 objects , second: take class 8 objects......')

My own project is different but i think the example points out the need, "parsing the coordinates for each object".

I am at the beginning with python so it will be perfect if you share an example on detect.py for solving my problem.

English is not my native language , i am sorry for my bad english.

Note: I modified detect.py file and working with it on my local computer.

Additional

No response

github-actions[bot] commented 2 years ago

πŸ‘‹ Hello @engineermechat, 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.

Requirements

Python>=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

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

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

glenn-jocher commented 2 years ago

@engineermechat πŸ‘‹ 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!

engineermechat commented 2 years ago

Thanks for your attention glenn, it works nice and it is working without detect.py but my problem is not about handling the coordinates. My main problem is " i can't use it as i want". I want to choose, filter and take the coordinates values like any other variables. Let me give an example, I am getting the values like yours:

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

However i want to use only 'person' or 'tie' coordinates to calculates distance between person and tie , between Zinedine Zidane and Carlo Ancelotti , between any other objects... I can also handle the coordinates in detect.py like i said in my first message but i dont know how to use them because of my limited knowledge on python and pytorch.

glenn-jocher commented 2 years ago

@engineermechat you probably want to return results in xywh format and then just take the distance between centers, i.e.

distance = ((results.xywh[0][0:2] - results.xywh[1][0:2]) ** 2).sum()
engineermechat commented 2 years ago

Yes, one of my need is taking the distance, but it is not enough for me. I must indentify the coordinates for classes like distance between class 15 object and class 9 object.

glenn-jocher commented 2 years ago

@MarGraco πŸ‘‹ 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!

github-actions[bot] commented 2 years ago

πŸ‘‹ 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 ⭐!