Now that you have a solid conceptual understanding of how YOLO works, it's time to move from theory to practice. In the last lesson, we discussed the grid system, bounding boxes, confidence scores, and non-maximum suppression. Today, you'll see these concepts in action as we fire up a state-of-the-art object detector and point it at some images.
This lesson is entirely hands-on. Our goal is to set up the powerful and user-friendly Ultralytics YOLOv8 library and use a pretrained model to detect objects in your meal images. You'll learn how to run predictions both from the command line for quick tests and within a Python script, giving you the programmatic control needed for your project pipeline.
Getting Started with Ultralytics YOLOv8
The team behind YOLOv8, Ultralytics, has made it incredibly simple to use their models. The entire framework is packaged into a single Python library, ultralytics. Since you already have a Conda environment with PyTorch and CUDA support from our first module, the only step needed is to install this new package.
First, activate your project's Conda environment. Then, run the following command in your terminal:
pip install ultralytics
This single command installs everything you need to run inference with pretrained YOLOv8 models.
Instant Results: The Command Line Interface
The fastest way to verify your installation and see YOLOv8's power is through its command-line interface (CLI). This allows you to run a prediction with a single command, without writing any Python code. It's perfect for quick experiments.
The video below from the Ultralytics team demonstrates how to install the library and run a prediction directly from the terminal.
Object Detection with Pre-trained Ultralytics YOLOv8 Model | Episode 1
This official tutorial from Ultralytics provides a concise overview of running a pretrained model.
Watch the section where the presenter installs the library and runs a prediction using the command line. Pay close attention to the structure of the yolo predict command.
As you saw, the basic command is:
yolo predict model=<model_file> source=<image_source>
model: Specifies which pretrained model to use. Ultralytics offers several versions, fromyolov8n.pt(nano, the fastest and smallest) toyolov8x.pt(extra-large, the most accurate but slowest). For your project, starting withyolov8n.ptoryolov8s.ptis a good balance.source: This can be a path to a local image file, a URL to an image on the web, a video file, or even0for your webcam.
When you run this command for the first time with a specific model like yolov8n.pt, the library will automatically download the required model weights. The results, including an annotated image, will be saved in a runs/detect/predict/ directory.
Go ahead and try it yourself. Find a URL of a meal image online and run the command:
yolo predict model=yolov8n.pt source="<URL_TO_YOUR_IMAGE>"
Check the output directory to see the result. You'll notice the model is pretrained on the COCO dataset, a large-scale object detection dataset. It can identify 80 common object classes like 'person', 'car', 'dog', and importantly for your project, items like 'bowl', 'pizza', 'orange', and 'apple'.
Programmatic Control: Using YOLOv8 in Python
While the CLI is great for quick tests, your project requires integrating object detection into a larger data processing pipeline. For this, you'll need to use the ultralytics library within a Python script. This is just as straightforward as the CLI.
The core of the library is the YOLO class. You create an instance of this class by pointing it to the model weights you want to use, and then you call its predict() method.
Object Detection with Pre-trained Ultralytics YOLOv8 Model | Episode 1
Let's return to the Ultralytics video to see how to perform predictions in a Python script.
Watch this segment which demonstrates how to import the YOLO class and run inference with just two lines of code. This is the fundamental pattern you will use in your project.
Here is a minimal script to run a prediction on a local image:
from ultralytics import YOLO
from PIL import Image
# Load a pretrained YOLOv8n model
model = YOLO('yolov8n.pt')
# Define the path to your image
image_path = 'path/to/your/meal_image.jpg'
# Run inference on the image
results = model.predict(image_path)
# The 'results' object is a list containing a single Results object.
# We can access it and show the annotated image.
result = results[0]
im_array = result.plot() # plot a BGR numpy array of predictions
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
im.show() # show image
im.save('results.jpg') # save image
Replace 'path/to/your/meal_image.jpg' with a path to one of the images you've scraped and run the script. It should display an image with bounding boxes drawn around any detected objects.
Understanding and Extracting the Output
The predict() method returns a list of Results objects, one for each image you provide. For your project, you won't just be looking at the plotted images; you'll need to programmatically access the data behind the detections: the bounding box coordinates, the class of the object, and the confidence score.
This is where the real power for your project lies. By extracting this data, you can identify food items and calculate their area, which is the first step toward portion estimation. The following resource explains the structure of the Results object in detail.
How to Perform YOLO Object Detection using OpenCV in Python - The Python Code
This article from The Python Code provides an excellent, practical code walkthrough of running YOLOv8 and processing its output.
Read the section on Making Predictions to understand what the results.boxes.data attribute contains. Then, review the code in Drawing Detected Objects, which shows how to loop through the detections and extract the coordinates, confidence, and class ID for each one.
The key takeaway is that you can iterate through the results[0].boxes attribute. Each item in this loop will give you access to:
box.xyxy: The bounding box coordinates as a tensor in the format[x_min, y_min, x_max, y_max].box.conf: The confidence score of the detection (from 0 to 1).box.cls: The class ID of the detected object (e.g., 49 for 'orange' in the COCO dataset).
The official Ultralytics channel also has a great video dedicated to this exact topic.
How to Extract the Outputs from Ultralytics YOLOv8 Model for Custom Projects | Episode 5
This video focuses specifically on extracting detection data from the results object for custom projects.
First, watch the introduction to the results object and its attributes. Next, see how to access different bounding box formats. Finally, follow the detailed code demonstration that shows how to iterate through detections and extract the bounding box coordinates, and then the confidence and class IDs.
Putting It All Together
Let's combine everything into a complete script that you can adapt for your project. This script will load the model, run a prediction on one of your meal images, and print the details for each detected object.

from ultralytics import YOLO
import cv2
# Load a pretrained YOLOv8n model
model = YOLO('yolov8n.pt')
# Path to one of your meal images
image_path = 'path/to/your/meal_image.jpg'
# Run prediction
results = model.predict(image_path)
# Load the original image using OpenCV
image = cv2.imread(image_path)
# The result for the first (and only) image
result = results[0]
# Get the class names from the model
class_names = model.names
print(f"Detected classes: {class_names}")
print("-" * 30)
print("Detected Objects:")
print("-" * 30)
# Iterate over each detected object in the image
for box in result.boxes:
# Get bounding box coordinates
# .tolist() converts the tensor to a list
# [0] gets the first (and only) set of coordinates
coords = box.xyxy.tolist()[0]
x_min, y_min, x_max, y_max = map(int, coords)
# Get confidence score
confidence = box.conf.item()
# Get class ID and name
class_id = int(box.cls.item())
class_name = class_names[class_id]
# Print the detection details
print(f"Object: {class_name}")
print(f"Coordinates: [x_min={x_min}, y_min={y_min}, x_max={x_max}, y_max={y_max}]")
print(f"Confidence: {confidence:.4f}")
print("-" * 10)
# Draw bounding box on the image
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
# Add label with class name and confidence
label = f"{class_name}: {confidence:.2f}"
cv2.putText(image, label, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display the image with detections
cv2.imshow("YOLOv8 Detections", image)
cv2.waitKey(0) # Wait for a key press to close the image window
cv2.destroyAllWindows()
# Optionally, save the annotated image
cv2.imwrite("annotated_meal.jpg", image)
Run this script on one of your meal photos. You should see a window pop up with the detected objects outlined and labeled, and the corresponding data printed to your console. This script is the foundation for the next step of your project.
Conclusion
In this lesson, you have successfully bridged the gap from theory to practice. You now have a working object detection setup that you can apply to your project's image data.
Here are the key skills you've acquired:
- You installed the
ultralyticslibrary to get access to YOLOv8 models. - You learned to run quick predictions from the command line, which is useful for rapid testing.
- You can now use YOLOv8 within a Python script, loading a model and running predictions programmatically.
- Crucially, you know how to access and interpret the model's output, extracting bounding boxes, confidence scores, and class labels for each detected object.
You have now generated the raw data needed for the next phase of your image analysis. In the upcoming lesson, we will focus on processing these YOLO outputs. You will write a script to systematically extract the bounding box coordinates and class labels for all detected items in an image, preparing this data for the subsequent portion estimation task.