ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.84k stars 16.37k forks source link

How could I get the xywh and similarity #10466

Closed qwerty6688 closed 1 year ago

qwerty6688 commented 1 year ago

Search before asking

Question

I searched the issues and I found:https://github-com.translate.goog/ultralytics/yolov5/issues/7964?_x_tr_sl=en&_x_tr_tl=zh-CN&_x_tr_hl=en&_x_tr_pto=wapp
However, I do not know how to Get the coordinates and width and height of the second detected object。
For example, if there are two people in a photo, how could I get the position of the second people.

Also, glenn-jocher put code but I can not run it.
`# 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`


it show me the error:
Exception has occurred: NameError name 'results' is not defined File "/workspaces/yolov5-test/detect.py", line 197, in run print(results.pandas().xyxy[0]) File "/workspaces/yolov5-test/detect.py", line 275, in main run(**vars(opt)) File "/workspaces/yolov5-test/detect.py", line 280, in main(opt) NameError: name 'results' is not defined

I tried to:
` # Stream results im0 = annotator.result() if view_img: if platform.system() == 'Linux' and p not in windows: windows.append(p) cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) cv2.imshow(str(p), im0) cv2.waitKey(1) # 1 millisecond

        # Save results (image with detections)
        if save_img:
            if dataset.mode == 'image':
                cv2.imwrite(save_path, im0)

                #print("*******>>>>>",im0.shape[0],im0.shape[1])        >>>图片大小-宽度高度

                #print("**",results.pandas()) #ERROR
                #x1 = int(xyxy[0].item())
                #y1 = int(xyxy[1].item())
                #x2 = int(xyxy[2].item())
                #y2 = int(xyxy[3].item( ))
                #what is these item? how could I get the type and the next item.

                print(xyxy[0].item(),xyxy[1].item(),xyxy[2].item(),xyxy[3].item())

                #print(xywh) #ERROR`

Additional

So how could I get the xywh, type and similarity. I know nothing about python.

github-actions[bot] commented 1 year ago

👋 Hello @qwerty6688, 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

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 year ago

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

qwerty6688 commented 1 year ago

@glenn-jocher what's means
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # yolov5n - yolov5x6 official model
Is that means download modle form internet?
How could I change the code if I want to use a modle that is on my computer?

qwerty6688 commented 1 year ago

@glenn-jocher Also, how could I choose the way to detect the photo(CPU,GPU) Thank you very much.

glenn-jocher commented 1 year ago

@qwerty6688 see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for examples of loading local models. You can send the model to cpu or GPU using model.cpu() or model.cuda() before inference.

github-actions[bot] commented 1 year 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 ⭐!

Symbadian commented 1 year ago

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

Hi @glenn-jocher, followed your steps and this print method is not working for my custom model

results.pandas().xyxy[0].value_counts('name') 

I am getting these results for this function results.pandas().xyxy

results

image 2/6: 447x612 (no detections)
image 3/6: 406x612 1 running
image 4/6: 185x272 2 running
image 5/6: 281x179 1 walking
image 6/6: 1233x840 1 boxing

which I don't want and cannot use...

I am trying to get the result for my custom model as per your example above using your print method below and its a futile experience so far.

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

Can you assist me to understand what I am wrong, please? Thanx loads for acknowledging my digital presence! I really appreciate this loads

glenn-jocher commented 12 months ago

@Symbadian i'm glad to be of assistance! The results you're seeing indicate that there are no detections on the first 2 images and some detection results on the following ones. It appears that the model is not producing the expected object detections. Here are a few points to review:

  1. Model Training: Ensure that your custom model was trained effectively on a well-balanced dataset to generate accurate detections.

  2. Requirements: Make sure that your custom model meets the minimum input requirements for object detection, such as image size and format.

  3. Inference Input: Verify that the images you are using for inference contain the type of objects that your model was trained to recognize.

  4. Code Debugging: Check for potential issues in the custom model's code, such as any custom modifications or changes to the evaluation function.

If you are still encountering issues or have specific queries about your custom model, feel free to share more details, and I'll be happy to assist further! Good luck 🍀