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 · 59 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 · 59 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?

KSGulin added a commit to neuralmagic/yolov5 that referenced this issue Apr 14, 2022
* Fix TensorRT potential unordered binding addresses (ultralytics#5826)

* feat: change file suffix in pythonic way

* fix: enforce binding addresses order

* fix: enforce binding addresses order

* Handle non-TTY `wandb.errors.UsageError` (ultralytics#5839)

* `try: except (..., wandb.errors.UsageError)`

* bug fix

* Avoid inplace modifying`imgs` in `LoadStreams` (ultralytics#5850)

When OpenCV retrieving image fail, original code would modify source images **inplace**, which may result in plotting bounding boxes on a black image. That is, before inference, source image `im0s[i]` is OK, but after inference before `Process predictions`,  `im0s[i]` may have been changed.

* Update `LoadImages` `ret_val=False` handling (ultralytics#5852)

Video errors may occur.

* Update val.py (ultralytics#5838)

* Update val.py

Solving Non-ASCII character '\xf0' error during runtime

* Update val.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update TorchScript suffix to `*.torchscript` (ultralytics#5856)

* Add `--workers 8` argument to val.py (ultralytics#5857)

* Update val.py

Add an option to choose number of workers if not called by train.py

* Update comment

* 120 char line width

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update `plot_lr_scheduler()` (ultralytics#5864)

shallow copy modify originals

* Update `nl` after `cutout()` (ultralytics#5873)

* `AutoShape()` models as `DetectMultiBackend()` instances (ultralytics#5845)

* Update AutoShape()

* autodownload ONNX

* Cleanup

* Finish updates

* Add Usage

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* fix device

* Update hubconf.py

* Update common.py

* smart param selection

* autodownload all formats

* autopad only pytorch models

* new_shape edits

* stride tensor fix

* Cleanup

* Single-command multiple-model export (ultralytics#5882)

* Export multiple models in series

Export multiple models in series by adding additional `*.pt` files to the `--weights` argument, i.e.:

```bash
python export.py --include tflite --weights yolov5n.pt  # export 1 model
python export.py --include tflite --weights yolov5n.pt yolov5s.pt yolov5m.pt yolov5l.pt yolov5x.pt  # export 5 models
```

* Update export.py

* Update README.md

* `Detections().tolist()` explicit argument fix (ultralytics#5907)

debugged for missigned Detections attributes

* Update wandb_utils.py (ultralytics#5908)

* Add *.engine (TensorRT extensions) to .gitignore (ultralytics#5911)

* Add *.engine (TensorRT extensions) to .gitignore

* Update .dockerignore

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Add ONNX inference providers (ultralytics#5918)

* Add ONNX inference providers

Fix for ultralytics#5916

* Update common.py

* Add hardware checks to `notebook_init()` (ultralytics#5919)

* Update notebook

* Update notebook

* update string

* update string

* Updates

* Updates

* Updates

* check both ipython and psutil

* remove sample_data if is_colab

* cleanup

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Revert "Update `plot_lr_scheduler()` (ultralytics#5864)" (ultralytics#5920)

This reverts commit 360eec6.

* Absolute '/content/sample_data' (ultralytics#5922)

* Default PyTorch Hub to `autocast(False)` (ultralytics#5926)

* Fix ONNX opset inconsistency with parseargs and run args (ultralytics#5937)

* Make `select_device()` robust to `batch_size=-1` (ultralytics#5940)

* Find out a bug. When set batch_size = -1 to use the autobatch.

reproduce:

* Fix type conflict

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* fix .gitignore not tracking existing folders (ultralytics#5946)

* fix .gitignore not tracking existing folders

fix .gitignore so that the files that are in the repository are actually being tracked.

Everything in the data/ folder is ignored, which also means the subdirectories are ignored. Fix so that the subdirectories and their contents are still tracked.

* Remove data/trainings

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update `strip_optimizer()` (ultralytics#5949)

Replace 'training_result' with 'best_fitness' in strip_optimizer() to match key with ckpt from train.py

* Add nms and agnostic nms to export.py (ultralytics#5938)

* add nms and agnostic nms to export.py

* fix agnostic implies nms

* reorder args to group TF args

* PEP8 120 char

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Refactor NUM_THREADS (ultralytics#5954)

* Fix Detections class `tolist()` method (ultralytics#5945)

* Fix tolist() to add the file for each Detection

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix PEP8 requirement for 2 spaces before an inline comment

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Cleanup

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Fix `imgsz` bug (ultralytics#5948)

* fix imgsz bug

* Update detect.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* `pretrained=False` fix (ultralytics#5966)

* `pretriained=False` fix

Fix for ultralytics#5964

* CI speed improvement

* make parameter ignore epochs (ultralytics#5972)

* make parameter ignore epochs

ignore epochs functionality add to prevent spikes at the beginning when fitness spikes and decreases after.
Discussed at ultralytics#5971

* Update train.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* YOLOv5s6 params and FLOPs fix (ultralytics#5977)

* Update callbacks.py with `__init__()` (ultralytics#5979)

Add __init__() function.

* Increase `ar_thr` from 20 to 100 for better detection on slender (high aspect ratio) objects (ultralytics#5556)

* Making `ar_thr` available as a hyperparameter

* Disabling ar_thr as hyperparameter and computing from the dataset instead

* Fixing bug in ar_thr computation

* Fix `ar_thr` to 100

* Allow `--weights URL` (ultralytics#5991)

* Recommend `jar xf file.zip` for zips (ultralytics#5993)

* *.torchscript inference `self.jit` fix (ultralytics#6007)

* Check TensorRT>=8.0.0 version (ultralytics#6021)

* Check TensorRT>=8.0.0 version

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Multi-layer capable `--freeze` argument (ultralytics#6019)

* support specfiy multiple frozen layers

* fix bug

* Cleanup Freeze section

* Cleanup argument

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* train -> val comment fix (ultralytics#6024)

* Add dataset source citations (ultralytics#6032)

* Kaggle `LOGGER` fix (ultralytics#6041)

* Simplify `set_logging()` indexing (ultralytics#6042)

* `--freeze` fix (ultralytics#6044)

Fix for ultralytics#6038

* OpenVINO Export (ultralytics#6057)

* OpenVINO export

* Remove timeout

* Add 3 files

* str

* Constrain opset to 12

* Default ONNX opset to 12

* Make dir

* Make dir

* Cleanup

* Cleanup

* check_requirements(('openvino-dev',))

* Reduce G/D/CIoU logic operations (ultralytics#6074)

Consider that the default value is CIOU,adjust the order of judgment could reduce the number of judgments.
And “elif CIoU:” didn't need 'if'.

Co-authored-by: 李杰 <360751194@qq.comqq.com>

* Init tensor directly on device (ultralytics#6068)

Slightly more efficient than .to(device)

* W&B: track batch size after autobatch (ultralytics#6039)

* track batch size after autobatch

* remove redundant import

* Update __init__.py

* Update __init__.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* W&B: Log best results after training ends (ultralytics#6120)

* log best.pt metrics at train end

* update

* Update __init__.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Log best results (ultralytics#6085)

* log best result in summary

* comment added

* add space for `flake8`

* log `best/epoch`

* fix `dimension` for epoch

ValueError: all the input arrays must have same number of dimensions

* log `best/` in `utils.logger.__init__`

* fix pre-commit

1. missing whitespace around operator
2.  over-indented

* Refactor/reduce G/C/D/IoU `if: else` statements (ultralytics#6087)

* Refactor the code to reduece else

* Update metrics.py

* Cleanup

Co-authored-by: Cmos <gen.chen@ubisoft.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Add EdgeTPU support (ultralytics#3630)

* Add models/tf.py for TensorFlow and TFLite export

* Set auto=False for int8 calibration

* Update requirements.txt for TensorFlow and TFLite export

* Read anchors directly from PyTorch weights

* Add --tf-nms to append NMS in TensorFlow SavedModel and GraphDef export

* Remove check_anchor_order, check_file, set_logging from import

* Reformat code and optimize imports

* Autodownload model and check cfg

* update --source path, img-size to 320, single output

* Adjust representative_dataset

* Put representative dataset in tfl_int8 block

* detect.py TF inference

* weights to string

* weights to string

* cleanup tf.py

* Add --dynamic-batch-size

* Add xywh normalization to reduce calibration error

* Update requirements.txt

TensorFlow 2.3.1 -> 2.4.0 to avoid int8 quantization error

* Fix imports

Move C3 from models.experimental to models.common

* Add models/tf.py for TensorFlow and TFLite export

* Set auto=False for int8 calibration

* Update requirements.txt for TensorFlow and TFLite export

* Read anchors directly from PyTorch weights

* Add --tf-nms to append NMS in TensorFlow SavedModel and GraphDef export

* Remove check_anchor_order, check_file, set_logging from import

* Reformat code and optimize imports

* Autodownload model and check cfg

* update --source path, img-size to 320, single output

* Adjust representative_dataset

* detect.py TF inference

* Put representative dataset in tfl_int8 block

* weights to string

* weights to string

* cleanup tf.py

* Add --dynamic-batch-size

* Add xywh normalization to reduce calibration error

* Update requirements.txt

TensorFlow 2.3.1 -> 2.4.0 to avoid int8 quantization error

* Fix imports

Move C3 from models.experimental to models.common

* implement C3() and SiLU()

* Add TensorFlow and TFLite Detection

* Add --tfl-detect for TFLite Detection

* Add int8 quantized TFLite inference in detect.py

* Add --edgetpu for Edge TPU detection

* Fix --img-size to add rectangle TensorFlow and TFLite input

* Add --no-tf-nms to detect objects using models combined with TensorFlow NMS

* Fix --img-size list type input

* Update README.md

* Add Android project for TFLite inference

* Upgrade TensorFlow v2.3.1 -> v2.4.0

* Disable normalization of xywh

* Rewrite names init in detect.py

* Change input resolution 640 -> 320 on Android

* Disable NNAPI

* Update README.me --img 640 -> 320

* Update README.me for Edge TPU

* Update README.md

* Fix reshape dim to support dynamic batching

* Fix reshape dim to support dynamic batching

* Add epsilon argument in tf_BN, which is different between TF and PT

* Set stride to None if not using PyTorch, and do not warmup without PyTorch

* Add list support in check_img_size()

* Add list input support in detect.py

* sys.path.append('./') to run from yolov5/

* Add int8 quantization support for TensorFlow 2.5

* Add get_coco128.sh

* Remove --no-tfl-detect in models/tf.py (Use tf-android-tfl-detect branch for EdgeTPU)

* Update requirements.txt

* Replace torch.load() with attempt_load()

* Update requirements.txt

* Add --tf-raw-resize to set half_pixel_centers=False

* Remove android directory

* Update README.md

* Update README.md

* Add multiple OS support for EdgeTPU detection

* Fix export and detect

* Export 3 YOLO heads with Edge TPU models

* Remove xywh denormalization with Edge TPU models in detect.py

* Fix saved_model and pb detect error

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix pre-commit.ci failure

* Add edgetpu in export.py docstring

* Fix Edge TPU model detection exported by TF 2.7

* Add class names for TF/TFLite in DetectMultibackend

* Fix assignment with nl in TFLite Detection

* Add check when getting Edge TPU compiler version

* Add UTF-8 encoding in opening --data file for Windows

* Remove redundant TensorFlow import

* Add Edge TPU in export.py's docstring

* Add the detect layer in Edge TPU model conversion

* Default `dnn=False`

* Cleanup data.yaml loading

* Update detect.py

* Update val.py

* Comments and generalize data.yaml names

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: unknown <fangjiacong@ut.cn>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Enable AdamW optimizer (ultralytics#6152)

* Update export format docstrings (ultralytics#6151)

* Update export documentation

* Cleanup

* Update export.py

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update README.md

* Update README.md

* Update README.md

* Update train.py

* Update train.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Update greetings.yml (ultralytics#6165)

* [pre-commit.ci] pre-commit suggestions (ultralytics#6177)

updates:
- [github.com/pre-commit/pre-commit-hooks: v4.0.1 → v4.1.0](pre-commit/pre-commit-hooks@v4.0.1...v4.1.0)
- [github.com/asottile/pyupgrade: v2.23.1 → v2.31.0](asottile/pyupgrade@v2.23.1...v2.31.0)
- [github.com/PyCQA/isort: 5.9.3 → 5.10.1](PyCQA/isort@5.9.3...5.10.1)
- [github.com/PyCQA/flake8: 3.9.2 → 4.0.1](PyCQA/flake8@3.9.2...4.0.1)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Update NMS `max_wh=7680` for 8k images (ultralytics#6178)

* Add OpenVINO inference (ultralytics#6179)

* Ignore `*_openvino_model/` dir (ultralytics#6180)

* Global export format sort (ultralytics#6182)

* Global export sort

* Cleanup

* Fix TorchScript on mobile export (ultralytics#6183)

* fix export of TorchScript on mobile

* Cleanup

Co-authored-by: yinrong <yinrong@xiaomi.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* TensorRT 7 `anchor_grid` compatibility fix (ultralytics#6185)

* fix: TensorRT 7 incompatiable

* Add comment

* Add if: else and comment

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Add `tensorrt>=7.0.0` checks (ultralytics#6193)

* Add `tensorrt>=7.0.0` checks

* Update export.py

* Update common.py

* Update export.py

* Add CoreML inference (ultralytics#6195)

* Add Apple CoreML inference

* Cleanup

* Fix `nan`-robust stream FPS (ultralytics#6198)

Fix for Webcam stop working suddenly (Issue ultralytics#6197)

* Edge TPU compiler comment (ultralytics#6196)

* Edge TPU compiler comment

* 7 to 8 fix

* TFLite `--int8` 'flatbuffers==1.12' fix (ultralytics#6216)

* TFLite `--int8` 'flatbuffers==1.12' fix

Temporary workaround for TFLite INT8 export.

* Update export.py

* Update export.py

* TFLite `--int8` 'flatbuffers==1.12' fix 2 (ultralytics#6217)

* TFLite `--int8` 'flatbuffers==1.12' fix 2

Reorganizes ultralytics#6216 fix to update before `tensorflow` import so no restart required.

* Update export.py

* Add `edgetpu_compiler` checks (ultralytics#6218)

* Add `edgetpu_compiler` checks

* Update export.py

* Update export.py

* Update export.py

* Update export.py

* Update export.py

* Update export.py

* Attempt `edgetpu-compiler` autoinstall (ultralytics#6223)

* Attempt `edgetpu-compiler` autoinstall

Attempt to install edgetpu-compiler dependency if missing on Linux.

* Update export.py

* Update export.py

* Update README speed reproduction command (ultralytics#6228)

* Update P2-P7 `models/hub` variants (ultralytics#6230)

* Update p2-p7 `models/hub` variants

* Update common.py

* AutoAnchor camelcase corrections

* TensorRT 7 export fix (ultralytics#6235)

* Fix `cmd` string on `tfjs` export (ultralytics#6243)

* Fix cmd string on tfjs export

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* TensorRT pip install

* Enable ONNX `--half` FP16 inference (ultralytics#6268)

* Enable ONNX ``--half` FP16 inference

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Update `export.py` with Detect, Validate usages (ultralytics#6280)

* Add `is_kaggle()` function (ultralytics#6285)

* Add `is_kaggle()` function

Return True if environment is Kaggle Notebook.

* Remove root loggers only if is_kaggle() == True

* Update general.py

* Fix `device` count check (ultralytics#6290)

* Fix device count check()

* Update torch_utils.py

* Update torch_utils.py

* Update hubconf.py

* Fixing bug multi-gpu training (ultralytics#6299)

* Fixing bug multi-gpu training

This solves this issue: ultralytics#6297 (comment)

* Update torch_utils.py for pep8

* `select_device()` cleanup (ultralytics#6302)

* `select_device()` cleanup

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Fix `train.py` parameter groups desc error (ultralytics#6318)

* Fix `train.py` parameter groups desc error

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Remove `dataset_stats()` autodownload capability (ultralytics#6303)

* Remove `dataset_stats()` autodownload capability

@kalenmike security update per Slack convo

* Update datasets.py

* Console corrupted -> corrupt (ultralytics#6338)

* Console corrupted -> corrupt 

Minor style changes.

* Update export.py

* TensorRT `assert im.device.type != 'cpu'` on export (ultralytics#6340)

* TensorRT `assert im.device.type != 'cpu'` on export

* Update export.py

* `export.py` return exported files/dirs (ultralytics#6343)

* `export.py` return exported files/dirs

* Path to str

* Created using Colaboratory

* `export.py` automatic `forward_export` (ultralytics#6352)

* `export.py` automatic `forward_export`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* New environment variable `VERBOSE` (ultralytics#6353)

New environment variable `VERBOSE`

* Reuse `de_parallel()` rather than `is_parallel()` (ultralytics#6354)

* `DEVICE_COUNT` instead of `WORLD_SIZE` to calculate `nw` (ultralytics#6324)

* Flush callbacks when on `--evolve` (ultralytics#6374)

* log best.pt metrics at train end

* update

* Update __init__.py

* flush callbacks when using evolve

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* FROM nvcr.io/nvidia/pytorch:21.12-py3 (ultralytics#6377)

* FROM nvcr.io/nvidia/pytorch:21.10-py3 (ultralytics#6379)

21.12 generates dockerhub errors so rolling back to 21.10 with latest pytorch install. Not sure if this torch install will work on non-GPU dockerhub autobuild so this is an experiment.

* Add `albumentations` to Dockerfile (ultralytics#6392)

* Add `stop_training=False` flag to callbacks (ultralytics#6365)

* New flag 'stop_training' in util.callbacks.Callbacks class to prematurely stop training from callback handler

* Removed most of the new  checks, leaving only the one after calling 'on_train_batch_end'

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Add `detect.py` GIF video inference (ultralytics#6410)

* Add detect.py GIF video inference

* Cleanup

* Update `greetings.yaml` email address (ultralytics#6412)

* Update `greetings.yaml` email address

* Update greetings.yml

* Rename logger from 'utils.logger' to 'yolov5' (ultralytics#6421)

* Gave a more explicit name to the logger

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Prefer `tflite_runtime` for TFLite inference if installed (ultralytics#6406)

* import tflite_runtime if tensorflow not installed

* rename tflite to tfli

* Attempt tflite_runtime for all TFLite workflows

Also rename tfli to tfl

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update workflows (ultralytics#6427)

* Workflow updates

* quotes fix

* best to weights fix

* Namespace `VERBOSE` env variable to `YOLOv5_VERBOSE` (ultralytics#6428)

* Verbose updates

* Verbose updates

* Add `*.asf` video support (ultralytics#6436)

* Revert "Remove `dataset_stats()` autodownload capability (ultralytics#6303)" (ultralytics#6442)

This reverts commit 3119b2f.

* Fix `select_device()` for Multi-GPU (ultralytics#6434)

* Fix `select_device()` for Multi-GPU

Possible fix for ultralytics#6431

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Update torch_utils.py

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Fix2 `select_device()` for Multi-GPU (ultralytics#6461)

* Fix2 select_device() for Multi-GPU

* Cleanup

* Cleanup

* Simplify error message

* Improve assert

* Update torch_utils.py

* Add Product Hunt social media icon (ultralytics#6464)

* Social media icons update

* fix URL

* Update README.md

* Resolve dataset paths (ultralytics#6489)

* Simplify TF normalized to pixels (ultralytics#6494)

* Improved `export.py` usage examples (ultralytics#6495)

* Improved `export.py` usage examples

* Cleanup

* CoreML inference fix `list()` -> `sorted()` (ultralytics#6496)

* Suppress `torch.jit.TracerWarning` on export (ultralytics#6498)

* Suppress torch.jit.TracerWarning

TracerWarnings can be safely ignored.

* Cleanup

* Suppress export.run() TracerWarnings (ultralytics#6499)

Suppresses warnings when calling export.run() directly, not just CLI python export.py.

Also adds Requirements examples for CPU and GPU backends

* W&B: Remember batchsize on resuming (ultralytics#6512)

* log best.pt metrics at train end

* update

* Update __init__.py

* flush callbacks when using evolve

* remember batch size on resuming

* Update train.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update hyp.scratch-high.yaml (ultralytics#6525)

Update `lrf: 0.1`, tested on YOLOv5x6 to 55.0 mAP@0.5:0.95, slightly higher than current.

* TODO issues exempt from stale action (ultralytics#6530)

* Update val_batch*.jpg for Chinese fonts (ultralytics#6526)

* Update plots for Chinese fonts

* make is_chinese() non-str safe

* Add global FONT

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update general.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Social icons after text (ultralytics#6473)

* Social icons after text

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update README.md

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Edge TPU compiler `sudo` fix (ultralytics#6531)

* Edge TPU compiler sudo fix

Allows for auto-install of Edge TPU compiler on non-sudo systems like the YOLOv5 Docker image.

@kalenmike

* Update export.py

* Update export.py

* Update export.py

* Edge TPU export 'list index out of range' fix (ultralytics#6533)

* Edge TPU `tf.lite.experimental.load_delegate` fix (ultralytics#6536)

* Edge TPU `tf.lite.experimental.load_delegate` fix

Fix attempt for ultralytics#6535

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Fixing minor multi-streaming issues with TensoRT engine (ultralytics#6504)

* Update batch-size in model.warmup() + indentation for logging inference results

* These changes are in response to PR comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Load checkpoint on CPU instead of on GPU (ultralytics#6516)

* Load checkpoint on CPU instead of on GPU

* refactor: simplify code

* Cleanup

* Update train.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* flake8: code meanings (ultralytics#6481)

* Fix 6 Flake8 issues (ultralytics#6541)

* F541

* F821

* F841

* E741

* E302

* E722

* Apply suggestions from code review

* Update general.py

* Update datasets.py

* Update export.py

* Update plots.py

* Update plots.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Edge TPU TF imports fix (ultralytics#6542)

* Edge TPU TF imports fix

Fix for ultralytics#6535 (comment)

* Update common.py

* Move trainloader functions to class methods (ultralytics#6559)

* Move trainloader functions to class methods

* results = ThreadPool(NUM_THREADS).imap(self.load_image, range(n))

* Cleanup

* Improved AutoBatch DDP error message (ultralytics#6568)

* Improved AutoBatch DDP error message

* Cleanup

* Fix zero-export handling with `if any(f):` (ultralytics#6569)

* Fix zero-export handling with `if any(f):`

Partial fix for ultralytics#6563

* Cleanup

* Fix `plot_labels()` colored histogram bug (ultralytics#6574)

* Fix `plot_labels()` colored histogram bug

* Cleanup

* Allow custom` --evolve` project names (ultralytics#6567)

* Update train.py

As see in ultralytics#6463, modification on train in evolve process to allow custom save directory.

* fix val

* PEP8

whitespace around operator

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Add `DATASETS_DIR` global in general.py (ultralytics#6578)

* return `opt` from `train.run()` (ultralytics#6581)

* Fix YouTube dislike button bug in `pafy` package (ultralytics#6603)

Per ultralytics#6583 (comment) by @alicera

* Update train.py

* Fix `hyp_evolve.yaml` indexing bug (ultralytics#6604)

* Fix `hyp_evolve.yaml` indexing bug

Bug caused hyp_evolve.yaml to display latest generation result rather than best generation result.

* Update plots.py

* Update general.py

* Update general.py

* Update general.py

* Fix `ROOT / data` when running W&B `log_dataset()` (ultralytics#6606)

* Fix missing data folder when running log_dataset

* Use ROOT/'data'

* PEP8 whitespace

* YouTube dependency fix `youtube_dl==2020.12.2` (ultralytics#6612)

Per ultralytics#5860 (comment) by @hdnh2006

* Add YOLOv5n to Reproduce section (ultralytics#6619)

* W&B: Improve resume stability (ultralytics#6611)

* log best.pt metrics at train end

* update

* Update __init__.py

* flush callbacks when using evolve

* remember batch size on resuming

* Update train.py

* improve stability of resume

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* W&B: don't log media in evolve (ultralytics#6617)

* YOLOv5 Export Benchmarks (ultralytics#6613)

* Add benchmarks.py

* Update

* Add requirements

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* dataset autodownload from root

* Update

* Redirect to /dev/null

* sudo --help

* Cleanup

* Add exports pd df

* Updates

* Updates

* Updates

* Cleanup

* dir handling fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Cleanup

* Cleanup2

* Cleanup3

* Cleanup model_type

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Fix ConfusionMatrix scale `vmin=0.0` (ultralytics#6638)

Fix attempt for ultralytics#6626

* Fixed wandb logger KeyError (ultralytics#6637)

* Fix yolov3.yaml remove list (ultralytics#6655)

Per ultralytics/yolov3#1887 (comment)

* Validate with 2x `--workers` (ultralytics#6658)

* Validate with 2x `--workers` single-GPU/CPU fix (ultralytics#6659)

Fix for ultralytics#6658 for single-GPU and CPU training use cases

* Add `--cache val` (ultralytics#6663)

New `--cache val` argument will cache validation set only into RAM. Should help multi-GPU training speeds without consuming as much RAM as full `--cache ram`.

* Robust `scipy.cluster.vq.kmeans` too few points (ultralytics#6668)

* Handle `scipy.cluster.vq.kmeans` too few points

Resolves ultralytics#6664

* Update autoanchor.py

* Cleanup

* Update Dockerfile `torch==1.10.2+cu113` (ultralytics#6669)

* FROM nvcr.io/nvidia/pytorch:22.01-py3 (ultralytics#6670)

* FROM nvcr.io/nvidia/pytorch:21.10-py3 (ultralytics#6671)

22.10 returns 'no space left on device' error message.

Seems like a bug at docker. Raised issue in docker/hub-feedback#2209

* Update Dockerfile reorder installs (ultralytics#6672)

Also `nvidia-tensorboard-plugin-dlprof`, `nvidia-tensorboard` are no longer installed in NVCR base.

* FROM nvcr.io/nvidia/pytorch:21.10-py3 (ultralytics#6673)

Reordered installation may help reduce resource usage in autobuild

* FROM nvcr.io/nvidia/pytorch:21.10-py3 (ultralytics#6677)

Revert to 21.10 on autobuild fail

* Fix TF exports >= 2GB (ultralytics#6292)

* Fix exporting saved_model: pb exceeds 2GB

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Replace TF v1.x API with TF v2.x API for saved_model export

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Clean up

* Remove lambda in tf.function()

* Revert "Remove lambda in tf.function()" to be compatible with TF v2.4

This reverts commit 46c7931f11dfdea6ae340c77287c35c30b9e0779.

* Fix for pre-commit.ci

* Cleanup1

* Cleanup2

* Backwards compatibility update

* Update common.py

* Update common.py

* Cleanup3

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Fix `--evolve --bucket gs://...` (ultralytics#6698)

* Fix CoreML P6 inference (ultralytics#6700)

* Fix CoreML P6 inference

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Fix floating point in number of workers `nw` (ultralytics#6701)

Integer division by a float yields a (rounded) float. This causes
the dataloader to crash when creating a range.

* Edge TPU inference fix (ultralytics#6686)

* refactor: use edgetpu flag

* fix: remove bitwise and assignation to tflite

* Cleanup and fix tflite

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Use `export_formats()` in export.py (ultralytics#6705)

* Use `export_formats()` in export.py

* list fix

* Suppress `torch` AMP-CPU warnings (ultralytics#6706)

This is a torch bug, but they seem unable or unwilling to fix it so I'm creating a suppression in YOLOv5. 

Resolves ultralytics#6692

* Update `nw` to `max(nd, 1)` (ultralytics#6714)

* GH: add PR template (ultralytics#6482)

* GH: add PR template

* Update CONTRIBUTING.md

* Update PULL_REQUEST_TEMPLATE.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update PULL_REQUEST_TEMPLATE.md

* Update PULL_REQUEST_TEMPLATE.md

* Update PULL_REQUEST_TEMPLATE.md

* Update PULL_REQUEST_TEMPLATE.md

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Switch default LR scheduler from cos to linear (ultralytics#6729)

* Switch default LR scheduler from cos to linear

Based on empirical results of training both ways on all YOLOv5 models.

* linear bug fix

* Updated VOC hyperparameters (ultralytics#6732)

* Update hyps

* Update hyp.VOC.yaml

* Update pathlib

* Update hyps

* Update hyps

* Update hyps

* Update hyps

* YOLOv5 v6.1 release (ultralytics#6739)

* Pre-commit table fix (ultralytics#6744)

* Update tutorial.ipynb (2 CPUs, 12.7 GB RAM, 42.2/166.8 GB disk) (ultralytics#6767)

* Update min warmup iterations from 1k to 100 (ultralytics#6768)

* Default `OMP_NUM_THREADS=8` (ultralytics#6770)

* Update tutorial.ipynb (ultralytics#6771)

* Update hyp.VOC.yaml (ultralytics#6772)

* Fix export for 1-channel images (ultralytics#6780)

Export failed for 1-channel input shape, 1-liner fix

* Update EMA decay `tau` (ultralytics#6769)

* Update EMA

* Update EMA

* ratio invert

* fix ratio invert

* fix2 ratio invert

* warmup iterations to 100

* ema_k

* implement tau

* implement tau

* YOLOv5s6 params FLOPs fix (ultralytics#6782)

* Update PULL_REQUEST_TEMPLATE.md (ultralytics#6783)

* Update autoanchor.py (ultralytics#6794)

* Update autoanchor.py

* Update autoanchor.py

* Update sweep.yaml (ultralytics#6825)

* Update sweep.yaml

Changed focal loss gamma search range between 1 and 4

* Update sweep.yaml

lowered the min value to match default

* AutoAnchor improved initialization robustness (ultralytics#6854)

* Update AutoAnchor

* Update AutoAnchor

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Add `*.ts` to `VID_FORMATS` (ultralytics#6859)

* Update `--cache disk` deprecate `*_npy/` dirs (ultralytics#6876)

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Cleanup

* Cleanup

* Update yolov5s.yaml (ultralytics#6865)

* Update yolov5s.yaml

* Update yolov5s.yaml

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Default FP16 TensorRT export (ultralytics#6798)

* Assert engine precision ultralytics#6777

* Default to FP32 inputs for TensorRT engines

* Default to FP16 TensorRT exports ultralytics#6777

* Remove wrong line ultralytics#6777

* Automatically adjust detect.py input precision ultralytics#6777

* Automatically adjust val.py input precision ultralytics#6777

* Add missing colon

* Cleanup

* Cleanup

* Remove default trt_fp16_input definition

* Experiment

* Reorder detect.py if statement to after half checks

* Update common.py

* Update export.py

* Cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Bump actions/setup-python from 2 to 3 (ultralytics#6880)

Bumps [actions/setup-python](https://github.com/actions/setup-python) from 2 to 3.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](actions/setup-python@v2...v3)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/checkout from 2 to 3 (ultralytics#6881)

Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Fix TRT `max_workspace_size` deprecation notice (ultralytics#6856)

* Fix TRT `max_workspace_size` deprecation notice

* Update export.py

* Update export.py

* Update bytes to GB with bitshift (ultralytics#6886)

* Move `git_describe()` to general.py (ultralytics#6918)

* Move `git_describe()` to general.py

* Move `git_describe()` to general.py

* PyTorch 1.11.0 compatibility updates (ultralytics#6932)

Resolves `AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'` first raised in ultralytics#5499

* Optimize PyTorch 1.11.0 compatibility update (ultralytics#6933)

* Allow 3-point segments (ultralytics#6938)

May resolve ultralytics#6931

* Fix PyTorch Hub export inference shapes (ultralytics#6949)

May resolve ultralytics#6947

* DetectMultiBackend() `--half` handling (ultralytics#6945)

* DetectMultiBackend() `--half` handling

* CI fixes

* rename .half to .fp16 to avoid conflict

* warmup fix

* val update

* engine update

* engine update

* Update Dockerfile `torch==1.11.0+cu113` (ultralytics#6954)

* New val.py `cuda` variable (ultralytics#6957)

* New val.py `cuda` variable

Fix for ONNX GPU val.

* Update val.py

* DetectMultiBackend() return `device` update (ultralytics#6958)

Fixes ONNX validation that returns outputs on CPU.

* Tensor initialization on device improvements (ultralytics#6959)

* Update common.py speed improvements

Eliminate .to() ops where possible for reduced data transfer overhead. Primarily affects warmup and PyTorch Hub inference.

* Updates

* Updates

* Update detect.py

* Update val.py

* EdgeTPU optimizations (ultralytics#6808)

* removed transpose op for better edgetpu support

* fix for training case

* enabled experimental new quantizer flag

* precalculate add and mul ops at compile time

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Model `ema` key backward compatibility fix (ultralytics#6972)

Fix for older model loading issue in ultralytics@d3d9cbc#commitcomment-68622388

* pt model to cpu on TF export

* YOLOv5 Export Benchmarks for GPU (ultralytics#6963)

* Add benchmarks.py GPU support

* Updates

* Updates

* Updates

* Updates

* Add --half

* Add TRT requirements

* Cleanup

* Add TF to warmup types

* Update export.py

* Update export.py

* Update benchmarks.py

* Update TQDM bar format (ultralytics#6988)

* Conditional `Timeout()` by OS (disable on Windows) (ultralytics#7013)

* Conditional `Timeout()` by OS (disable on Windows)

* Update general.py

* fix: add default PIL font as fallback  (ultralytics#7010)

* fix: add default font as fallback

Add default font as fallback if the downloading of the Arial.ttf font
fails for some reason, e.g. no access to public internet.

* Update plots.py

Co-authored-by: Maximilian Strobel <Maximilian.Strobel@infineon.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Consistent saved_model output format (ultralytics#7032)

* `ComputeLoss()` indexing/speed improvements (ultralytics#7048)

* device as class attribute

* Update loss.py

* Update loss.py

* improve zeros

* tensor split

* Update Dockerfile to `git clone` instead of `COPY` (ultralytics#7053)

Resolves git command errors that currently happen in image, i.e.:

```bash
root@382ae64aeca2:/usr/src/app# git pull
Warning: Permanently added the ECDSA host key for IP address '140.82.113.3' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
```

* Create SECURITY.md (ultralytics#7054)

* Create SECURITY.md

Resolves ultralytics#7052

* Move into ./github

* Update SECURITY.md

* Fix incomplete URL substring sanitation (ultralytics#7056)

Resolves code scanning alert in ultralytics#7055

* Use PIL to eliminate chroma subsampling in crops (ultralytics#7008)

* use pillow to save higher-quality jpg (w/o color subsampling)

* Cleanup and doc issue

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Fix `check_anchor_order()` in pixel-space not grid-space (ultralytics#7060)

* Update `check_anchor_order()`

Use mean area per output layer for added stability.

* Check in pixel-space not grid-space fix

* Update detect.py non-inplace with `y.tensor_split()` (ultralytics#7062)

* Update common.py lists for tuples (ultralytics#7063)

Improved profiling.

* Update W&B message to `LOGGER.info()` (ultralytics#7064)

* Update __init__.py (ultralytics#7065)

* Add non-zero `da` `check_anchor_order()` condition (ultralytics#7066)

* Fix2 `check_anchor_order()` in pixel-space not grid-space (ultralytics#7067)

Follows ultralytics#7060 which provided only a partial solution to this issue. ultralytics#7060 resolved occurences in yolo.py, this applies the same fix in autoanchor.py.

* Revert "Update detect.py non-inplace with `y.tensor_split()` (ultralytics#7062)" (ultralytics#7074)

This reverts commit d5e363f.

* Update loss.py with `if self.gr < 1:` (ultralytics#7087)

* Update loss.py with `if self.gr < 1:`

* Update loss.py

* Update loss for FP16 `tobj` (ultralytics#7088)

* Update model summary to display model name (ultralytics#7101)

* `torch.split()` 1.7.0 compatibility fix (ultralytics#7102)

* Update loss.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update loss.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Update benchmarks significant digits (ultralytics#7103)

* Model summary `pathlib` fix (ultralytics#7104)

Stems not working correctly for YOLOv5l with current .rstrip() implementation. After fix:
```
YOLOv5l summary: 468 layers, 46563709 parameters, 46563709 gradients, 109.3 GFLOPs
```

* Remove named arguments where possible (ultralytics#7105)

* Remove named arguments where possible

Speed improvements.

* Update yolo.py

* Update yolo.py

* Update yolo.py

* Multi-threaded VisDrone and VOC downloads (ultralytics#7108)

* Multi-threaded VOC download

* Update VOC.yaml

* Update

* Update general.py

* Update general.py

* `np.fromfile()` Chinese image paths fix (ultralytics#6979)

* 🎉 🆕 now can read Chinese image path. 

use "cv2.imdecode(np.fromfile(f, np.uint8), cv2.IMREAD_COLOR)" instead of "cv2.imread(f)" for Chinese image path.

* Update datasets.py

* Update __init__.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Add PyTorch Hub `results.save(labels=False)` option (ultralytics#7129)

Resolves ultralytics#388 (comment)

* SparseML integration

* Add SparseML dependancy

* Update: add missing files

* Update requirements.txt

* Update: sparseml-nightly support

* Update: remove model versioning

* Partial update for multi-stage recipes

* Update: multi-stage recipe support

* Update: remove sparseml dep

* Fix: multi-stage recipe handeling

* Fix: multi stage support

* Fix: non-recipe runs

* Add: legacy hyperparam files

* Fix: add copy-paste to hyps

* Fix: nit

* apply structure fixes

* Squashed rebase to v6.1 upstream

* Update SparseML Integration to V6.1 (#26)

* SparseML integration

* Add SparseML dependancy

* Update: add missing files

* Update requirements.txt

* Update: sparseml-nightly support

* Update: remove model versioning

* Partial update for multi-stage recipes

* Update: multi-stage recipe support

* Update: remove sparseml dep

* Fix: multi-stage recipe handeling

* Fix: multi stage support

* Fix: non-recipe runs

* Add: legacy hyperparam files

* Fix: add copy-paste to hyps

* Fix: nit

* apply structure fixes

* manager fixes

* Update function name

Co-authored-by: Konstantin <konstantin@neuralmagic.com>
Co-authored-by: Konstantin Gulin <66528950+KSGulin@users.noreply.github.com>
@DrkPlne
Copy link

DrkPlne commented Jun 6, 2022

@milind-sonialgılama sonuçları burada mevcuttur:

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 artık çalışmıyor.

Hey,
Hello there
Did you find a solution to this problem?

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

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

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.