What YOLOv8 Does

YOLO (You Only Look Once) detects objects in images in a single forward pass — no sliding windows, no region proposals. YOLOv8 by Ultralytics is the latest iteration: faster, more accurate, and dead simple to use in Python.

You give it an image, it returns bounding boxes with class labels and confidence scores. Works on images, video streams, and webcams out of the box.

Install and Run in 60 Seconds

1
pip install ultralytics
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from ultralytics import YOLO

# Load a pretrained model (downloads automatically on first run)
model = YOLO("yolov8n.pt")  # nano model — fast, good for testing

# Run detection on an image
results = model("image.jpg")

# Show results
results[0].show()  # Opens a window with bounding boxes drawn

That’s it. Three lines to go from nothing to object detection. The yolov8n.pt model is the smallest (6.3MB) and runs in real-time even on a CPU.

Understanding the Results

The results object contains everything you need — bounding boxes, class names, confidence scores.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from ultralytics import YOLO

model = YOLO("yolov8n.pt")
results = model("street.jpg")

# Iterate over detected objects
for box in results[0].boxes:
    # Bounding box coordinates (x1, y1, x2, y2)
    coords = box.xyxy[0].tolist()
    x1, y1, x2, y2 = coords

    # Class name and confidence
    class_id = int(box.cls[0])
    class_name = model.names[class_id]
    confidence = float(box.conf[0])

    print(f"{class_name}: {confidence:.2f} at [{x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f}]")

# Example output:
# car: 0.92 at [120, 340, 450, 520]
# person: 0.88 at [50, 100, 180, 480]
# traffic light: 0.76 at [300, 20, 340, 90]

Key properties on each box:

  • box.xyxy — bounding box as [x1, y1, x2, y2]
  • box.xywh — bounding box as [center_x, center_y, width, height]
  • box.conf — confidence score (0 to 1)
  • box.cls — class ID (maps to model.names)

Run on Video

YOLOv8 handles video natively. It processes frame by frame and can write the output to a new video file.

1
2
3
4
5
6
7
8
9
from ultralytics import YOLO

model = YOLO("yolov8n.pt")

# Process video — saves annotated output automatically
results = model("traffic.mp4", save=True, conf=0.5)

# Or process a webcam stream (0 = default camera)
results = model(source=0, show=True, conf=0.5)

The conf=0.5 parameter filters out detections below 50% confidence, which cuts down on false positives in noisy scenes.

Filter Specific Classes

You don’t always want every object. Filter by class ID to detect only what matters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from ultralytics import YOLO

model = YOLO("yolov8n.pt")

# COCO class IDs: 0=person, 2=car, 5=bus, 7=truck
results = model("highway.jpg", classes=[0, 2, 5, 7])

# Count vehicles
vehicle_count = 0
for box in results[0].boxes:
    class_name = model.names[int(box.cls[0])]
    if class_name in ["car", "bus", "truck"]:
        vehicle_count += 1

print(f"Vehicles detected: {vehicle_count}")

Model Sizes

YOLOv8 comes in five sizes. Pick based on your speed/accuracy tradeoff:

ModelSizemAPSpeed (CPU)Use Case
yolov8n6.3 MB37.380msEdge devices, real-time
yolov8s22.5 MB44.9130msBalanced
yolov8m52 MB50.2240msGeneral purpose
yolov8l87 MB52.9375msHigh accuracy
yolov8x136 MB53.9480msMaximum accuracy
1
2
3
# Switch models by changing the filename
model_nano = YOLO("yolov8n.pt")    # Fastest
model_large = YOLO("yolov8l.pt")   # Most accurate

Save Results Programmatically

Export detections to JSON, save annotated images, or draw custom bounding boxes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from ultralytics import YOLO
import json

model = YOLO("yolov8n.pt")
results = model("office.jpg")

# Save annotated image
results[0].save("output.jpg")

# Export to JSON-friendly format
detections = []
for box in results[0].boxes:
    detections.append({
        "class": model.names[int(box.cls[0])],
        "confidence": round(float(box.conf[0]), 3),
        "bbox": [round(c, 1) for c in box.xyxy[0].tolist()],
    })

with open("detections.json", "w") as f:
    json.dump(detections, f, indent=2)

Common Issues

CUDA out of memory. Use a smaller model (yolov8n instead of yolov8x) or reduce the input image size with imgsz=640.

Low confidence scores. If detections are weak, the objects might be too small or occluded. Try increasing imgsz to 1280 for better small-object detection.

Wrong classes detected. The pretrained model uses COCO’s 80 classes. If you need to detect custom objects (like specific products or defects), you’ll need to fine-tune on your own dataset.

1
2
# Fine-tune on custom data
yolo detect train data=my_dataset.yaml model=yolov8n.pt epochs=100 imgsz=640

When to Use What

  • Real-time surveillanceyolov8n on GPU, stream mode
  • Batch image processingyolov8m or yolov8l, process in batches
  • Custom objects → Fine-tune any model on your labeled dataset
  • Edge deployment → Export to ONNX or TensorRT for optimized inference