Object Detection Program in Python with Code and Implementation

Object Detection Program in Python


You are working for a company that develops security software. Your team has been tasked with developing an algorithm that can detect objects in images captured by surveillance cameras. Write a Python program that uses the OpenCV library to detect objects in an image, draw bounding boxes around the objects and save the processed image to a file.

To accomplish Object Detection Program in Python we will be using YOLOv5 algorithm for object detection. Here are the steps that we will follow:

  1. Install the necessary dependencies and libraries.
  2. Download YOLOv5 model and load it into our program.
  3. Load an image into our program.
  4. Use YOLOv5 model to detect objects in the image and draw bounding boxes around them.
  5. Save processed image to a file.

Let’s get started!

Step 1: Install the necessary dependencies and libraries.

We will be using the following libraries in our program:

  • torch: For loading YOLOv5 model.
  • opencv-python: For loading and processing image.
  • matplotlib: For displaying and saving processed image.

To install these libraries run following commands:

!pip install torch
!pip install opencv-python
!pip install matplotlib
Importing Libraries

Step 2: Download the YOLOv5 model and load it into our program.

We will be using YOLOv5s model for object detection. To download the model run the following command:

!git clone https://github.com/ultralytics/yolov5.git

Next, we need to load the model into our program. Here’s the code to do that:

import torch

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

Step 3: Load an image into our program.

For this example, let’s use the following image:

object Detection
object Detection

To load the image into our program, run the following code:

import cv2
image = cv2.imread('surveillance_image.jpg')

Step 4: Use the YOLOv5 model to detect objects in the image and draw bounding boxes around them.

To detect objects in the image, we need to pass the image through the YOLOv5 model. Here’s the code to do that:

results = model(image)

The results variable now contains detected objects along with their bounding boxes. We can use OpenCV to draw bounding boxes on the image. Here’s code to do that:

for result in results.xyxy[0]:
    x1, y1, x2, y2, confidence, class_id = result.tolist()
    if confidence > 0.5:
        cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)

This code loops through all detected objects and draws a green bounding box around each object that has a confidence score greater than 0.5.

Step 5: Save processed image to a file.

To save processed image to a file, we can use the imwrite function from OpenCV. Here’s the code to do that:

cv2.imwrite('processed_image.jpg', image)

This will save the processed image to a file called processed_image.jpg.

Here’s the complete code:

# Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# Load image
image = cv2.imread('test.png')
# Perform object detection
results = model(image)
# Draw bounding boxes around detected objects
for result in results.xyxy[0]:
    x1, y1, x2, y2, confidence, class_id = result.tolist()
    if confidence > 0.5:
        cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
# Save processed image
cv2.imwrite('processed_image.jpg', image)
# Display processed image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()
Complete Code

Output:

Processed Image
Processed Image

Colab File Link:

https://colab.research.google.com/drive/1xn5Zfblbzlx7keHOqt5xLZb7R-6vZgoA?usp=sharing

Here are the links to the resources used in the code:

In addition, here are some useful links for learning more about object detection and deep learning: