;
Artificial intelligence March 23 ,2025

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:

  1. yolov3.weights – Pre-trained weights for YOLOv3.
  2. yolov3.cfg – YOLOv3 network configuration file.
  3. 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:

  1. Install Dependencies – Install OpenCV and NumPy.
  2. Load YOLO Model – Load the pre-trained YOLO model and class labels.
  3. Process Image – Convert the image into a blob and perform inference.
  4. Detect Objects – Extract bounding boxes, class labels, and confidence scores.
  5. Draw Bounding Boxes – Display detected objects on the image.

This implementation provides a real-time, efficient object detection system using YOLO and OpenCV.ete

Purnima
0

You must logged in to post comments.

Related Blogs

What is Ar...
Artificial intelligence March 03 ,2025

What is Artificial I...

History an...
Artificial intelligence March 03 ,2025

History and Evolutio...

Importance...
Artificial intelligence March 03 ,2025

Importance and Appli...

Narrow AI,...
Artificial intelligence March 03 ,2025

Narrow AI, General A...

AI vs Mach...
Artificial intelligence March 03 ,2025

AI vs Machine Learni...

Linear Alg...
Artificial intelligence March 03 ,2025

Linear Algebra Basic...

Calculus f...
Artificial intelligence March 03 ,2025

Calculus for AI

Probabilit...
Artificial intelligence March 03 ,2025

Probability and Stat...

Probabilit...
Artificial intelligence March 03 ,2025

Probability Distribu...

Graph Theo...
Artificial intelligence March 03 ,2025

Graph Theory and AI

What is NL...
Artificial intelligence March 03 ,2025

What is NLP

Preprocess...
Artificial intelligence March 03 ,2025

Preprocessing Text D...

Sentiment...
Artificial intelligence March 03 ,2025

Sentiment Analysis a...

Word Embed...
Artificial intelligence March 03 ,2025

Word Embeddings (Wor...

Transforme...
Artificial intelligence March 03 ,2025

Transformer-based Mo...

Building C...
Artificial intelligence March 03 ,2025

Building Chatbots wi...

Basics of...
Artificial intelligence March 03 ,2025

Basics of Computer V...

Image Prep...
Artificial intelligence March 03 ,2025

Image Preprocessing...

Object Det...
Artificial intelligence March 03 ,2025

Object Detection and...

Face Recog...
Artificial intelligence March 03 ,2025

Face Recognition and...

Applicatio...
Artificial intelligence March 03 ,2025

Applications of Comp...

AI-Powered...
Artificial intelligence March 03 ,2025

AI-Powered Chatbot U...

Implementi...
Artificial intelligence March 03 ,2025

Implementing a Basic...

Implementa...
Artificial intelligence March 03 ,2025

Implementation of Ob...

Implementa...
Artificial intelligence March 03 ,2025

Implementation of Fa...

Deep Reinf...
Artificial intelligence March 03 ,2025

Deep Reinforcement L...

Deep Reinf...
Artificial intelligence March 03 ,2025

Deep Reinforcement L...

Deep Reinf...
Artificial intelligence March 03 ,2025

Deep Reinforcement L...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Tools for...
Artificial intelligence March 03 ,2025

Tools for Data Handl...

Tool for D...
Artificial intelligence March 03 ,2025

Tool for Data Handli...

Cloud Plat...
Artificial intelligence April 04 ,2025

Cloud Platforms for...

Deep Dive...
Artificial intelligence April 04 ,2025

Deep Dive into AWS S...

Cloud Plat...
Artificial intelligence April 04 ,2025

Cloud Platforms for...

Cloud Plat...
Artificial intelligence April 04 ,2025

Cloud Platforms for...

Visualizat...
Artificial intelligence April 04 ,2025

Visualization Tools...

Data Clean...
Artificial intelligence April 04 ,2025

Data Cleaning and Pr...

Explorator...
Artificial intelligence April 04 ,2025

Exploratory Data Ana...

Explorator...
Artificial intelligence April 04 ,2025

Exploratory Data Ana...

Feature En...
Artificial intelligence April 04 ,2025

Feature Engineering...

Data Visua...
Artificial intelligence April 04 ,2025

Data Visualization w...

Working wi...
Artificial intelligence April 04 ,2025

Working with Large D...

Understand...
Artificial intelligence April 04 ,2025

Understanding Bias i...

Ethics in...
Artificial intelligence April 04 ,2025

Ethics in AI Develop...

Fairness i...
Artificial intelligence April 04 ,2025

Fairness in Machine...

The Role o...
Artificial intelligence April 04 ,2025

The Role of Regulati...

Responsibl...
Artificial intelligence April 04 ,2025

Responsible AI Pract...

Artificial...
Artificial intelligence April 04 ,2025

Artificial Intellige...

AI in Fina...
Artificial intelligence April 04 ,2025

AI in Finance and Ba...

AI in Auto...
Artificial intelligence April 04 ,2025

AI in Autonomous Veh...

AI in Gami...
Artificial intelligence April 04 ,2025

AI in Gaming and Ent...

AI in Soci...
Artificial intelligence April 04 ,2025

AI in Social Media a...

Building a...
Artificial intelligence April 04 ,2025

Building a Spam Emai...

Creating a...
Artificial intelligence April 04 ,2025

Creating an Image Cl...

Developing...
Artificial intelligence April 04 ,2025

Developing a Sentime...

Implementi...
Artificial intelligence April 04 ,2025

Implementing a Recom...

Generative...
Artificial intelligence April 04 ,2025

Generative AI: An In...

Explainabl...
Artificial intelligence April 04 ,2025

Explainable AI (XAI)

AI for Edg...
Artificial intelligence April 04 ,2025

AI for Edge Devices...

Quantum Co...
Artificial intelligence April 04 ,2025

Quantum Computing an...

AI for Tim...
Artificial intelligence April 04 ,2025

AI for Time Series F...

Emerging T...
Artificial intelligence May 05 ,2025

Emerging Trends in A...

AI and the...
Artificial intelligence May 05 ,2025

AI and the Job Marke...

The Role o...
Artificial intelligence May 05 ,2025

The Role of AI in Cl...

AI Researc...
Artificial intelligence May 05 ,2025

AI Research Frontier...

Preparing...
Artificial intelligence May 05 ,2025

Preparing for an AI-...

4 Popular...
Artificial intelligence May 05 ,2025

4 Popular AI Certifi...

Building a...
Artificial intelligence May 05 ,2025

Building an AI Portf...

How to Pre...
Artificial intelligence May 05 ,2025

How to Prepare for A...

AI Career...
Artificial intelligence May 05 ,2025

AI Career Opportunit...

Staying Up...
Artificial intelligence May 05 ,2025

Staying Updated in A...

Part 1-  T...
Artificial intelligence May 05 ,2025

Part 1- Tools for T...

Implementi...
Artificial intelligence May 05 ,2025

Implementing ChatGPT...

Part 2-  T...
Artificial intelligence May 05 ,2025

Part 2- Tools for T...

Part 1- To...
Artificial intelligence May 05 ,2025

Part 1- Tools for Te...

Technical...
Artificial intelligence May 05 ,2025

Technical Implementa...

Part 2- To...
Artificial intelligence May 05 ,2025

Part 2- Tools for Te...

Part 1- To...
Artificial intelligence May 05 ,2025

Part 1- Tools for Te...

Step-by-St...
Artificial intelligence May 05 ,2025

Step-by-Step Impleme...

Part 2 - T...
Artificial intelligence May 05 ,2025

Part 2 - Tools for T...

Part 4- To...
Artificial intelligence May 05 ,2025

Part 4- Tools for Te...

Part 1- To...
Artificial intelligence May 05 ,2025

Part 1- Tools for Te...

Part 2- To...
Artificial intelligence May 05 ,2025

Part 2- Tools for Te...

Part 3- To...
Artificial intelligence May 05 ,2025

Part 3- Tools for Te...

Step-by-St...
Artificial intelligence May 05 ,2025

Step-by-Step Impleme...

Part 1- To...
Artificial intelligence June 06 ,2025

Part 1- Tools for Im...

Implementa...
Artificial intelligence June 06 ,2025

Implementation of D...

Part 2- To...
Artificial intelligence June 06 ,2025

Part 2- Tools for Im...

Part 1- To...
Artificial intelligence June 06 ,2025

Part 1- Tools for Im...

Implementa...
Artificial intelligence June 06 ,2025

Implementation of Ru...

Part 1- To...
Artificial intelligence June 06 ,2025

Part 1- Tools for Im...

Part 2- To...
Artificial intelligence June 06 ,2025

Part 2- Tools for Im...

Step-by-St...
Artificial intelligence June 06 ,2025

Step-by-Step Impleme...

Part 1-Too...
Artificial intelligence June 06 ,2025

Part 1-Tools for Ima...

Part 2- To...
Artificial intelligence June 06 ,2025

Part 2- Tools for Im...

Implementa...
Artificial intelligence June 06 ,2025

Implementation of Pi...

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech