YOLOv8 Real-Time Object Detection with Python
Ultralytics YOLOv8 is the latest YOLO version released in January 2023. 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. The main features of YOLOv8 include mosaic data augmentation, anchor-free detection, C2f module, decoupled head, and a modified loss function as compared to the previous YOLO versons.
YOLOv8 Variants for Object Detection
YOLOv8 detection models have no suffix and are the default YOLOv8 models, i.e. (yolov8n.pt, yolov8s.pt, yolov8m.pt, yolov8l.pt, yolov8x.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 comes in five variants based on the number of parameters — nano(n), small(s), medium(m), large(l), and extra large(x). You can use all the variants for object detection according to your requirement. 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
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
Image(filename='/content/output.jpg')
Output Predicted Image
Print Bounding Boxes Information
for r in results:
print(r.boxes)
Discover the secrets to mastering Video and Image Object Detection and Segmentation with Python, in the following featured courses.
YOLOv8: Video Object Detection with Python on Custom Dataset
Video Segmentation with Python using Deep Learning for Real-Time
If you like reading, Buy me a Cofee !
Follow to Stay Tuned and Never Miss a Story! Thanks!!