0
0
Computer-visionHow-ToBeginner ยท 3 min read

How to Use YOLOv5 Python for Computer Vision Tasks

Use YOLOv5 in Python by installing the repository, loading a pre-trained model with torch.hub.load, and running model(image) to detect objects. The model returns predictions with bounding boxes and labels for computer vision tasks.
๐Ÿ“

Syntax

To use YOLOv5 in Python, first load the model using torch.hub.load. Then pass an image or image path to the model to get predictions. The main parts are:

  • torch.hub.load('ultralytics/yolov5', 'yolov5s'): loads the small YOLOv5 model.
  • model(image): runs detection on the image.
  • results.xyxy[0]: gets bounding boxes and confidence scores.
python
import torch

# Load YOLOv5 small model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

# Run inference on an image
results = model('path/to/image.jpg')

# Get predictions
predictions = results.xyxy[0]
๐Ÿ’ป

Example

This example shows how to load YOLOv5, run detection on an image, and print detected objects with confidence scores.

python
import torch

# Load the YOLOv5 small model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

# Image URL or local path
img = 'https://ultralytics.com/images/zidane.jpg'

# Run inference
results = model(img)

# Print detected objects
for *box, conf, cls in results.xyxy[0]:
    print(f'Class: {model.names[int(cls)]}, Confidence: {conf:.2f}')
Output
Class: person, Confidence: 0.99 Class: person, Confidence: 0.98 Class: tie, Confidence: 0.85
โš ๏ธ

Common Pitfalls

Common mistakes when using YOLOv5 in Python include:

  • Not installing dependencies like torch and opencv-python.
  • Using incorrect image paths or unsupported image formats.
  • Forgetting to set pretrained=True when loading the model to get weights.
  • Misinterpreting the output format; results.xyxy[0] contains bounding boxes, confidence, and class indices.
python
import torch

# Wrong: missing pretrained weights
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=False)  # This loads model without weights

# Right: load with pretrained weights
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
๐Ÿ“Š

Quick Reference

Summary tips for using YOLOv5 in Python:

  • Install dependencies: pip install torch torchvision opencv-python
  • Load model with pretrained weights: torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
  • Input can be image path, URL, or numpy array.
  • Access results with results.xyxy[0] for bounding boxes and classes.
  • Use results.show() to display detections visually.
โœ…

Key Takeaways

Load YOLOv5 in Python using torch.hub.load with pretrained weights for easy setup.
Run model on images by passing file paths, URLs, or arrays to get detection results.
Interpret results.xyxy[0] to extract bounding boxes, confidence scores, and class labels.
Ensure dependencies like torch and opencv-python are installed to avoid errors.
Use results.show() to quickly visualize detected objects on images.