YOLOv5 Object Detector (Pip Package)

7.0.14 · active · verified Thu Apr 16

YOLOv5 is a family of object detection architectures and models pretrained on the COCO dataset, known for its balance of speed and accuracy. This `yolov5` pip package by @fcakyon provides an easily installable and programmatic interface for the original Ultralytics YOLOv5 implementation. The current version is 7.0.14, with frequent minor updates aligning with upstream changes and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a pretrained YOLOv5 model, set inference parameters, and perform object detection on an image. The `YOLOv5` class manages model loading and inference, providing easy access to prediction results.

from yolov5 import YOLOv5
import os

# Load a pretrained YOLOv5 model
# Options: yolov5s, yolov5m, yolov5l, yolov5x
model = YOLOv5(model_path="yolov5s.pt", device="cpu") # Use device="cuda:0" for GPU

# Set model parameters
model.conf = 0.25  # NMS confidence threshold
model.iou = 0.45   # NMS IoU threshold
model.max_det = 1000 # Maximum number of detections per image

# Perform inference on an image (e.g., from a URL or local path)
# Example image from COCO dataset
img_path = "https://ultralytics.com/images/zidane.jpg"
results = model(img_path, size=640)

# Process and display results
predictions = results.pred[0]
boxes = predictions[:, :4]
scores = predictions[:, 4]
categories = predictions[:, 5]

print(f"Detected {len(boxes)} objects:")
for i in range(len(boxes)):
    print(f"  Box: {boxes[i].tolist()}, Score: {scores[i]:.2f}, Category: {int(categories[i])}")

# Optional: Save results to a directory
# results.save(save_dir="./results")

view raw JSON →