YOLOv8 can run object detection on images with a pre-trained model. YOLO (You Only Look Once) is a well-known object-detection algorithm, and YOLOv8 is designed for fast inference and accurate results. The ultralytics library provides the Python interface used below.
Requirements
Before you begin, make sure you have:
- Python
- The
ultralyticslibrary
If it is not installed yet, install it with pip:
pip install ultralyticsRun YOLOv8 on an image
This example loads a pre-trained YOLOv8 model, runs it on one image, and exposes the detection results:
from ultralytics import YOLO
# Load the default pre-trained YOLOv8 model
model = YOLO("yolov8n.pt")
# Run detection on an image
results = model(["image/car.jpg"])
# Iterate over the detection results
for result in results:
boxes = result.boxes # bounding boxes
masks = result.masks # segmentation masks, when supported by the model
keypoints = result.keypoints # pose keypoints
probs = result.probs # classification probabilities
obb = result.obb # oriented bounding boxes
# Display the result
result.show()
# Save the result to a file
result.save(filename="result.jpg")What the example returns
- Load the model →
YOLO("yolov8n.pt")loads the pre-trained YOLOv8 Nano model. If it is not available locally, Ultralytics downloads it automatically. - Detect objects →
model(["image/car.jpg"])runs inference on the image. Replace the path with any image you want to analyze. - Read the results → the result can contain bounding boxes, segmentation masks, pose keypoints, classification probabilities, and other task-specific output.
- Output → display the annotated result with
result.show()or save it withresult.save().
Next steps
The same model can process a batch of image paths or serve as the starting point for real-time detection from a camera. Replace image/car.jpg with an image available in your environment and inspect the returned result fields for the task you need.