0Pricing
Learn AI with Python · Lesson

Object Detection with YOLOv8

ultralytics YOLO, model.predict(), bounding boxes, confidence filtering, custom training.

Object Detection with YOLOv8 is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Classification vs Detection

Image classification says what is in an image. Object detection goes further: it finds where each object is, drawing bounding boxes and labels. YOLO is the most popular real-time detector.

What is YOLO?

YOLO (You Only Look Once) detects all objects in a single forward pass, making it fast enough for video. YOLOv8 by Ultralytics offers a clean Python API for inference and training.

pip install ultralytics

Loading a Pretrained Model

Import YOLO and load a checkpoint. yolov8n.pt is the nano model: tiny and fast. Larger variants (s, m, l, x) trade speed for accuracy. The weights download automatically on first use.

from ultralytics import YOLO

model = YOLO("yolov8n.pt")

Running Inference

Call model.predict (or just model(image)) on an image path, URL, or array. It returns a list of Results, one per input image.

results = model.predict("street.jpg", conf=0.5)

The conf Threshold

The conf parameter sets the minimum confidence to keep a detection. Raise it to drop uncertain boxes (fewer false positives); lower it to catch more objects at the risk of noise.

results = model.predict("street.jpg", conf=0.25)  # more detections
results = model.predict("street.jpg", conf=0.7)   # only confident ones

Reading the Boxes

Each result has a boxes object. boxes.xyxy gives bounding-box corners as [x1, y1, x2, y2] (top-left and bottom-right pixel coordinates).

r = results[0]
print(r.boxes.xyxy)  # tensor of [x1, y1, x2, y2] rows

Classes and Confidence

Alongside boxes you get boxes.cls (class indices) and boxes.conf (confidence scores). Map indices to names with model.names.

for box in r.boxes:
    cls_id = int(box.cls)
    conf = float(box.conf)
    name = model.names[cls_id]
    print(name, round(conf, 2))

Visualizing Results

results[0].plot() returns an annotated image array with boxes and labels drawn on it, ready to display or save. Great for quickly checking detections.

annotated = results[0].plot()

import cv2
cv2.imwrite("output.jpg", annotated)

Detecting in Video

Pass a video file or webcam index and YOLO processes every frame. With stream=True it yields results frame by frame, keeping memory low for long videos.

for result in model.predict("clip.mp4", stream=True):
    boxes = result.boxes
    # process each frame

Custom Training

To detect your own object classes, train on a labeled dataset described by a YAML file. Call model.train with the data config, epochs, and image size. YOLO fine-tunes the pretrained weights.

model = YOLO("yolov8n.pt")
model.train(
    data="my_dataset.yaml",
    epochs=50,
    imgsz=640
)

The data YAML

The dataset YAML lists the train/val image folders and the class names. YOLO expects each image to have a matching label file with normalized box coordinates per object.

# my_dataset.yaml
# train: images/train
# val: images/val
# names:
#   0: helmet
#   1: vest

Quick Check

Test your YOLOv8 knowledge.

Recap: Object Detection with YOLOv8

You loaded a pretrained detector with YOLO("yolov8n.pt"), ran model.predict(img, conf=0.5), and read detections from results[0].boxes.xyxy, .cls, and .conf. You visualized with results[0].plot() and fine-tuned on custom classes via model.train with a data YAML.

Frequently asked questions

Is the “Object Detection with YOLOv8” lesson free?

Yes — the full text of “Object Detection with YOLOv8” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Object Detection with YOLOv8”?

ultralytics YOLO, model.predict(), bounding boxes, confidence filtering, custom training. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Object Detection with YOLOv8” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. PyTorch Tensors and Autograd
  2. Custom Datasets and DataLoaders
  3. Building and Training CNNs in PyTorch
  4. Object Detection with YOLOv8
← Back to Learn AI with Python