Real-Time Object Detection with YOLOv3 and OpenCV





Real-time object detection has a wide range of applications, from autonomous driving to security surveillance. YOLO (You Only Look Once) is a state-of-the-art, real-time object detection system. In this tutorial, we will guide you through implementing real-time object detection using YOLOv3 and OpenCV in Python.


1. Set Up Your Environment

Ensure you have Python installed. Additionally, you'll need the following libraries:
  • OpenCV
  • NumPy
You can install these libraries using pip:

bash

pip install opencv-python numpy


2. Download YOLOv3 Pre-trained Weights and Configuration Files

Download the YOLOv3 weights and configuration files from the official YOLO website:Place these files in your project directory.



3. Import Libraries and Load YOLO

Start by importing the necessary libraries and loading the YOLO model.

python

import cv2 import numpy as np # Load YOLO net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # Load class names with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()]


4. Load and Preprocess Input Image

Load an input image and preprocess it for YOLO.

python

# Load image image = cv2.imread("input.jpg") height, width, channels = image.shape # Prepare the image for YOLO blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob)


5. Run Object Detection

Run the object detection algorithm on the input image.

python

outs = net.forward(output_layers) # Extract information from the detection class_ids = [] confidences = [] boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id)


6. Apply Non-Maximum Suppression

Apply non-maximum suppression to reduce overlapping bounding boxes.

python

indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)


7. Draw Bounding Boxes and Labels

Draw the bounding boxes and labels on the image.

python

for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) color = (0, 255, 0) # Green color for bounding box cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # Display the image cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows()


8. Implement Real-Time Detection

For real-time detection, capture video from a webcam and process each frame.

python

# Capture video from webcam cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() height, width, channels = frame.shape blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) class_ids = [] confidences = [] boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) color = (0, 255, 0) # Green color for bounding box cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # Display the frame cv2.imshow("Frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()




Conclusion

Real-time object detection using YOLOv3 and OpenCV is a powerful technique for various applications. This tutorial covered setting up the environment, loading and preprocessing images, running object detection, and implementing real-time detection using a webcam. With further enhancements, you can create more advanced detection systems tailored to specific needs.

Share:

0 Comments:

New Post

Recent Posts

    Support Me