Learn to Detect Objects in Images using YOLO in Python

YOLO Object Detection in Python

Introduction

Object detection has revolutionized the way we interpret and interact with visual data. With advancements in deep learning, frameworks like YOLO (You Only Look Once) have made it possible to detect objects in real time with remarkable accuracy.

In this article, we will guide you through the process of setting up and using YOLO to detect objects in an image using Python. Whether youā€™re new to computer vision or an experienced developer, this tutorial will equip you with the knowledge to implement object detection in your projects. Weā€™ll cover everything from the basics of YOLO to practical implementation, ensuring you gain a solid understanding of this powerful tool.

Requirements and Installations

Before we start coding, letā€™s ensure Python (3.6 or later) is installed on your computer. You can check your Python version by typingĀ python --versionĀ orĀ python3 --versionĀ in your terminal. If you donā€™t have Python, you can download it for free fromĀ https://www.python.org/downloads/.

Now download all the dependencies we require using the following commands:

pip install gitpython>=3.1.30
pip install matplotlib>=3.3
pip install numpy>=1.23.5
pip install opencv-python>=4.1.1
pip install pillow>=10.3.0
pip install psutil 
pip install PyYAML>=5.3.1
pip install requests>=2.32.0
pip install scipy>=1.4.1
pip install thop>=0.1.1
pip install torch>=1.8.0
pip install torchvision>=0.9.0
pip install tqdm>=4.64.0
pip install ultralytics>=8.2.34
pip install pandas>=1.1.4
pip install seaborn>=0.11.0
pip install setuptools>=65.5.1
pip install filterpy
pip install scikit-image
pip install lap

Alternative Installation

Installing the above utilities one by one might be a boring task. Instead, you can download the ā€˜requirements.txtā€˜ file containing all the dependencies above. Simply run the following command. It will automate the whole task in one go.

pip install -r requirements.txt

Setting Up the Environment

Create a separate folder named ā€œObject_Detections.ā€ Now under this folder create two more folders named ā€˜Weightsā€˜ and ā€˜Mediaā€˜ to store pre-trained YOLO models and images respectively.

Download a YOLO Model

Download the YOLO model ā€˜yolov8l.ptā€˜ from GitHub and place it into the ā€˜Weightsā€˜ folder. Here is the download link: Ā https://github.com/ultralytics/assets/releases/download/v8.1.0/yolov8l.pt.

Media Files

I have collected some suitable images from the internet for our object detection program. You can get those using the ā€˜Downloadā€˜ button below. All you have to do is, download the zip file, unzip it, and place those images inside the ā€˜Mediaā€˜ folder.

Create Your Python Script

Weā€™re almost at the end of setting up the environment. Now choose your favorite text editor and open the directory ā€˜Object_Detections.ā€™ Inside this directory, create a Python program file named ā€˜object_detections.pyā€˜. This is where youā€™ll write the code for your object detection program.

The Source Code

Letā€™s break down the entire source code into multiple sections and explain what each part does:

Import Libraries

First, we need to import the necessary libraries. ā€˜OpenCVā€˜ is used for image processing, ā€˜cvzoneā€˜ helps draw bounding boxes, and ā€˜YOLOā€™ from the ā€˜ultralyticsā€˜ library is used for object detection.

import cv2
import math
import cvzone
from ultralytics import YOLO

Load YOLO Model and Define Class Names

Next, we load the YOLO model with the custom weights and define the class names that YOLO can detect. Make sure you have downloaded the YOLOv8 weights and placed them in the correct directory.

# Load YOLO model with custom weights
yolo_model = YOLO("Weights/yolov8l.pt")

# Define class names
class_labels = [
    "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
    "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
    "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", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", 
    "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", 
    "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"
]

Load the Image

We then load the image we want to process using OpenCVā€™s ā€˜imreadā€˜ method.

# Load the image
image_path = "Media/public.jpg"
img = cv2.imread(image_path)

Perform Object Detection

The YOLO model is used to detect objects in the loaded image.

# Perform object detection
results = yolo_model(img)

Draw Bounding Boxes and Labels

We loop through the detected objects and draw bounding boxes around them. The confidence score and class label are also displayed.

# Loop through the detections and draw bounding boxes
for r in results:
    boxes = r.boxes
    for box in boxes:
        x1, y1, x2, y2 = box.xyxy[0]
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)

        w, h = x2 - x1, y2 - y1
        cvzone.cornerRect(img, (x1, y1, w, h))
        conf = math.ceil((box.conf[0] * 100)) / 100
        cls = int(box.cls[0])

        if conf > 0.3:
            cvzone.putTextRect(img, f'{class_labels[cls]} {conf}', (x1, y1 - 10), scale=0.8, thickness=1, colorR=(255, 0, 0))

Display the Image

Finally, we display the processed image using OpenCVā€™s ā€˜imshowā€˜ method. The window will close when the ā€˜qā€˜ button is pressed.

# Display the image with detections
cv2.imshow("Image", img)

# Close window when 'q' button is pressed
while True:
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
cv2.waitKey(1)

Output

YOLO detection of 12 persons in a park scene, with people walking

Summary

In this article, we learned how to use the YOLO model to detect objects in an image using Python. We used the pre-trained ā€˜yolov8lā€˜ model to identify objects in an image. But thatā€™s not all! This tutorial will help you build custom object detection models using Python.

Recommended Article: Create a Car Counter in Python using YOLO and OpenCV

For any query, reach out to me at contact@pyseek.com.

Happy Coding!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! Iā€™m Subhankar Rakshit, the brains behind PySeek. Iā€™m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles:Ā 194