Mastering Object Detection with YOLO and OpenCV

Mastering Object Detection with YOLO and OpenCV

STEP 1:

Before diving into the code, you must set up your environment. Make sure you have OpenCV installed:

pip install opencv-python opencv-python-headless        

STEP 2:

Download the necessary YOLO files:


STEP 3: Place these files in a directory where you will run your script.

STEP 4: CODE

import os
import cv2
import numpy as np

# Paths to the YOLO files
weights_path = 'yolov3.weights'
config_path = 'yolov3.cfg'
coco_names_path = 'coco.names'

# Load YOLO
net = cv2.dnn.readNet(weights_path, config_path)

# Load the COCO class labels
with open(coco_names_path, 'rt') as f:
    classes = f.read().strip().split('\n')

# Define the function to process images
def process_images(folder_path):
    for filename in os.listdir(folder_path):
        if filename.endswith(".jpg") or filename.endswith(".png"):
            image_path = os.path.join(folder_path, filename)
            image = cv2.imread(image_path)

            # Get the width and height of the image
            (H, W) = image.shape[:2]

            # Prepare the image for YOLO
            blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
            net.setInput(blob)
            layer_names = net.getLayerNames()
            output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
            outputs = net.forward(output_layers)

            boxes = []
            confidences = []
            class_ids = []

            # Loop over each of the layer outputs
            for output in outputs:
                for detection in output:
                    scores = detection[5:]
                    class_id = np.argmax(scores)
                    confidence = scores[class_id]

                    if confidence > 0.5:  # Filter out weak detections
                        box = detection[0:4] * np.array([W, H, W, H])
                        (centerX, centerY, width, height) = box.astype("int")

                        x = int(centerX - (width / 2))
                        y = int(centerY - (height / 2))

                        boxes.append([x, y, int(width), int(height)])
                        confidences.append(float(confidence))
                        class_ids.append(class_id)

            # Apply non-maxima suppression to suppress weak and overlapping boxes
            indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)

            if len(indices) > 0:
                for i in indices.flatten():
                    (x, y) = (boxes[i][0], boxes[i][1])
                    (w, h) = (boxes[i][2], boxes[i][3])
                    color = (0, 255, 0)  # Bounding box color
                    cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
                    text = f"{classes[class_ids[i]]}: {confidences[i]:.2f}"
                    cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color,2)

            # Save the processed image
            output_path = os.path.join(folder_path, "processed_" + filename)
            cv2.imwrite(output_path, image)

# Set the folder path containing images
folder_path = 'path/to/your/images/folder'

# Process the images
process_images(folder_path)
        


How It Works:

  • Loading YOLO: We start by loading the YOLO model and class labels. This includes specifying the paths to the YOLO configuration, weights, and class label files.
  • Processing Images: We define a function process_images that iterates through all images in the specified folder. For each image:
  • Saving Results: The processed images are saved in the same folder with the prefix "processed_ ".


NOTE: Feel free to reach me in incase of any issues while running the project.



Louis Albert Ayongo

Help Desk chez SGS Scanning Cameroon SA

3 周

ARNAB MUKHERJEE ???? Hello, do you know how to convert a pytorch file PT into a cfg weight file?

要查看或添加评论,请登录

社区洞察

其他会员也浏览了