Implementation of Object Detection using YOLO in OpenCV
Let's implement a basic object detection model using YOLO and OpenCV.
Step 1: Install Dependencies
pip install opencv-python numpy
Step 2: Load Pre-trained YOLO Model
import cv2
import numpy as np
def 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()]
return net, output_layers
Step 3: Detect Objects in an Image
def detect_objects(image_path):
net, output_layers = load_yolo()
image = cv2.imread(image_path)
height, width, _ = image.shape
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outputs = net.forward(output_layers)
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
print(f"Detected object: {class_id} with confidence {confidence}")
Step 4: Run Object DImplementation of Object Detection using YOLO in OpenCV
Object detection using YOLO (You Only Look Once) is one of the most efficient methods for detecting objects in real-time. YOLO processes an entire image in a single pass and detects multiple objects with high speed and accuracy. This implementation guide covers how to use YOLO with OpenCV for object detection in images.
Step 1: Install Dependencies
To use YOLO with OpenCV, install the necessary Python libraries:
pip install opencv-python numpy
Why These Libraries?
- OpenCV: Provides tools for image processing and loading the YOLO model.
- NumPy: Used for handling numerical computations, such as working with bounding boxes and confidence scores.
Step 2: Load the Pre-trained YOLO Model
Before detecting objects, we need to load the pre-trained YOLOv3 model and configuration files. These files contain the network structure and pre-trained weights for object detection.
Files Required:
- yolov3.weights – Pre-trained weights for YOLOv3.
- yolov3.cfg – YOLOv3 network configuration file.
- coco.names – A file containing class labels (e.g., person, car, dog).
Code to Load YOLO Model:
import cv2
import numpy as np
def load_yolo():
# Load the YOLO network with pre-trained weights and configuration
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# Extract layer names to get the output layer indexes
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Load the class names from coco.names
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
return net, output_layers, classes
Explanation:
- cv2.dnn.readNet(): Loads the YOLO network with the provided weights and configuration.
- net.getLayerNames(): Retrieves all layer names in the model.
- net.getUnconnectedOutLayers(): Gets the indexes of output layers responsible for detecting objects.
- COCO classes: Loaded from coco.names, containing 80 object classes YOLO can detect.
Step 3: Object Detection in an Image
Once the model is loaded, we pass an image through the network to detect objects.
Code for Object Detection:
def detect_objects(image_path):
net, output_layers, classes = load_yolo()
# Load and preprocess the image
image = cv2.imread(image_path)
height, width, _ = image.shape
# Convert image into a format suitable for YOLO
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
# Perform forward pass through the YOLO network
outputs = net.forward(output_layers)
boxes, confidences, class_ids = [], [], []
# Process the output layers
for output in outputs:
for detection in output:
scores = detection[5:] # Confidence scores for each class
class_id = np.argmax(scores) # Get class with highest probability
confidence = scores[class_id]
# Filter weak detections
if confidence > 0.5:
# Get bounding box coordinates
center_x, center_y, w, h = (detection[0:4] * np.array([width, height, width, height])).astype("int")
# Convert to top-left corner format
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)
return boxes, confidences, class_ids, classes
Explanation:
- cv2.imread(image_path): Reads the input image.
- cv2.dnn.blobFromImage(): Converts the image into a blob, normalizing pixel values and resizing to (416, 416).
- net.forward(output_layers): Runs the YOLO model on the image.
- Detection filtering: Extracts bounding box coordinates, confidence scores, and class labels for detected objects.
Step 4: Drawing Bounding Boxes on Detected Objects
Now that we have detected objects, we need to draw bounding boxes on the original image and display the results.
Code for Drawing Bounding Boxes:
def draw_labels(image_path):
boxes, confidences, class_ids, classes = detect_objects(image_path)
image = cv2.imread(image_path)
# Apply Non-Maximum Suppression to remove redundant boxes
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Define colors for bounding boxes
colors = np.random.uniform(0, 255, size=(len(classes), 3))
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}"
color = colors[class_ids[i]]
# Draw rectangle around detected object
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 output image
cv2.imshow("YOLO Object Detection", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
- cv2.dnn.NMSBoxes(): Removes overlapping bounding boxes with lower confidence scores.
- cv2.rectangle(): Draws a bounding box around detected objects.
- cv2.putText(): Displays the class name and confidence score.
- cv2.imshow(): Shows the final output image with detections.
Step 5: Running the Model
To run the YOLO object detection model, call:
draw_labels("sample_image.jpg")
Summary of the Implementation Steps:
- Install Dependencies – Install OpenCV and NumPy.
- Load YOLO Model – Load the pre-trained YOLO model and class labels.
- Process Image – Convert the image into a blob and perform inference.
- Detect Objects – Extract bounding boxes, class labels, and confidence scores.
- Draw Bounding Boxes – Display detected objects on the image.
This implementation provides a real-time, efficient object detection system using YOLO and OpenCV.ete