Complete Ultralytics Tutorial: Object Detection with YOLO
Ultralytics is a Python framework that provides state-of-the-art implementations for YOLO (You Only Look Once), one of the most popular and efficient object detection algorithms. In this tutorial, we will learn how to use Ultralytics from installation to custom model deployment.
What is Ultralytics and YOLO?
YOLO is a real-time object detection algorithm that can detect multiple objects in a single forward pass of a neural network. Ultralytics provides implementations of YOLOv8, YOLOv9, YOLOv10, and YOLO11 that are easy to use with high performance.
Ultralytics Advantages:- Very simple and intuitive API
- Support for multiple tasks: detection, segmentation, classification, pose estimation
- Pretrained models for various use cases
- Easy training and finetuning
- Export to various formats (ONNX, TensorRT, CoreML, etc.)
Installation
1. Setup Environment
# Create virtual environment
python -m venv ultralyticsenv
source ultralyticsenv/bin/activate # Linux/Mac
ultralyticsenv\Scripts\activate # Windows
Install Ultralytics
pip install ultralytics
For GPU support (CUDA)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
2. Verify Installation
import ultralytics
from ultralytics import YOLO
Check version
print(f"Ultralytics version: {ultralytics.version}")
Check CUDA availability
import torch
print(f"CUDA available: {torch.cuda.isavailable()}")
if torch.cuda.isavailable():
print(f"GPU: {torch.cuda.getdevicename(0)}")
Quick Start: Object Detection
1. Load Pretrained Model
from ultralytics import YOLO
Load YOLOv8 model (available: yolov8n, yolov8s, yolov8m, yolov8l, yolov8x)
n=nano, s=small, m=medium, l=large, x=extra large
model = YOLO('yolov8n.pt') # Model is automatically downloaded
Or load YOLO11 (latest)
model = YOLO('yolo11n.pt')
Model Options:
| Model | Size | mAP | Speed (ms) | Use Case |
|-------|------|-----|------------|----------|
| yolov8n | 3.2M | 37.3 | 1.2 | Edge devices, real-time |
| yolov8s | 11.2M | 44.9 | 1.9 | Balance speed/accuracy |
| yolov8m | 25.9M | 50.2 | 4.1 | General purpose |
| yolov8l | 43.7M | 52.9 | 6.8 | High accuracy |
| yolov8x | 68.2M | 53.9 | 10.8 | Maximum accuracy |
2. Inference on Images
from ultralytics import YOLO
Load model
model = YOLO('yolov8n.pt')
Inference on single image
results = model('path/to/image.jpg')
Inference on multiple images
results = model(['image1.jpg', 'image2.jpg', 'image3.jpg'])
Inference from URL
results = model('https://example.com/image.jpg')
Process results
for result in results:
# Bounding boxes
boxes = result.boxes
print(f"Detected {len(boxes)} objects")
for box in boxes:
# Box coordinates (x1, y1, x2, y2)
xyxy = box.xyxy[0].tolist()
# Confidence score
confidence = box.conf[0].item()
# Class ID and name
classid = int(box.cls[0].item())
classname = model.names[classid]
print(f" {classname}: {confidence:.2f} at {xyxy}")
3. Inference on Video
from ultralytics import YOLO
import cv2
model = YOLO('yolov8n.pt')
Inference on video file
results = model('path/to/video.mp4', stream=True)
for result in results:
# Process each frame
annotatedframe = result.plot()
# Display (optional)
cv2.imshow('Detection', annotatedframe)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
4. Real-time Inference from Webcam
from ultralytics import YOLO
import cv2
model = YOLO('yolov8n.pt')
Open webcam
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Run inference
results = model(frame, verbose=False)
# Visualize results