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

How to Use YOLOv8 Python for Computer Vision Tasks

Use the ultralytics Python package to load a YOLOv8 model with YOLO('yolov8n.pt'), then run detection on images or videos using model.predict(). This lets you quickly detect objects in computer vision tasks with minimal code.
๐Ÿ“

Syntax

The basic syntax to use YOLOv8 in Python involves importing the YOLO class from the ultralytics package, loading a pre-trained model, and running predictions on images or videos.

  • YOLO('model_path'): Loads the YOLOv8 model from a file or pretrained weights.
  • model.predict(source='image_or_video_path'): Runs object detection on the given source.
  • results: Contains detected objects, bounding boxes, and confidence scores.
python
from ultralytics import YOLO

# Load a pretrained YOLOv8 model
model = YOLO('yolov8n.pt')

# Run detection on an image
results = model.predict(source='image.jpg')

# Access detected boxes and classes
boxes = results[0].boxes
classes = results[0].boxes.cls
๐Ÿ’ป

Example

This example shows how to load the YOLOv8 nano model, run detection on a sample image, and print detected object classes and confidence scores.

python
from ultralytics import YOLO

# Load YOLOv8 nano model
model = YOLO('yolov8n.pt')

# Run prediction on an example image
results = model.predict(source='https://ultralytics.com/images/bus.jpg')

# Print detected classes and confidence scores
for result in results:
    for box in result.boxes:
        cls = model.names[int(box.cls)]
        conf = box.conf.item()
        print(f'Detected {cls} with confidence {conf:.2f}')
Output
Detected bus with confidence 0.98 Detected person with confidence 0.85 Detected car with confidence 0.76
โš ๏ธ

Common Pitfalls

  • Not installing the ultralytics package first with pip install ultralytics.
  • Using incorrect model file names or paths causes loading errors.
  • Passing invalid source paths to model.predict() leads to file not found errors.
  • Forgetting to convert detected class indices to names using model.names.
  • Not handling results as a list of detections; always index the first element for single image input.
python
from ultralytics import YOLO

# Wrong: missing model file or wrong name
# model = YOLO('wrong_model.pt')  # This will raise an error

# Correct:
model = YOLO('yolov8n.pt')

# Wrong: invalid source path
# results = model.predict(source='nonexistent.jpg')  # FileNotFoundError

# Correct:
results = model.predict(source='image.jpg')
๐Ÿ“Š

Quick Reference

Here is a quick summary of key YOLOv8 Python usage tips:

  • Install with pip install ultralytics.
  • Load model: model = YOLO('yolov8n.pt').
  • Run detection: results = model.predict(source='image.jpg').
  • Access detections: results[0].boxes for bounding boxes.
  • Get class names: model.names[int(box.cls)].
  • Supports images, videos, webcam streams as source.
โœ…

Key Takeaways

Install the ultralytics package to use YOLOv8 in Python easily.
Load pretrained models with YOLO('model_name.pt') and run detection with model.predict().
Always check that source paths and model files exist to avoid errors.
Use results[0].boxes and model.names to interpret detection outputs.
YOLOv8 supports images, videos, and live streams for flexible computer vision tasks.