YOLO Object Detection with Python

YOLOv8 models are fast, accurate, and easy to use, making them ideal for real-time object detection task trained on large datasets and run on diverse hardware platforms, from CPUs to GPUs. Object detection involves identifying the location and class of objects in an image or video stream. The output is a set of bounding boxes that enclose the objects in the image, along with class labels and confidence scores for each box. Object detection is a perfect choice when you need to detect and identify objects of interest, but don’t need to know exactly where the object is or its exact shape.

YOLOv8 detection models have no suffix and are the default YOLOv8 models, i.e. (yolov8n.pt, yolov8s.pt, yolov8m.pt, yolov8l.pt, yolov8nx.pt) and are pretrained on COCO dataset with the following Classes.

['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']

YOLOv8 pretrained Detect models (nano, small, medium, large and extra large based on number of parameters) are shown in the table below:

Setup UltraLytics for YOLOv8

%pip install ultralytics
import ultralytics
ultralytics.checks()

Load YOLOv8 for Object Detection

from ultralytics import YOLO

# Load a model
# You can use different YOLOv8 variants (yolov8n, yolov8s, yolov8m, yolov8l, yolov8nx)
model = YOLO('yolov8n.pt')  # load a pretrained model

# Use the model
results = model('https://ultralytics.com/images/zidane.jpg')  # predict on an image
# Save the output image after detection in Google Colab
results[0].save('/content/output.jpg')
# Print the COCO dataset classes on which model is trained.
print(model.names.values())

Input Image

Display the Output Image after Detection

from IPython.display import Image

# Display the image in Google Colab
Image(filename='/content/output.jpg')

Output Predicted Image

Print Bounding Boxes Information

for r in results:
    print(r.boxes)