Mastering Object Detection with YOLO and OpenCV
ARNAB MUKHERJEE ????
Senior Associate || Software Engineer || Master's in Data Science || PGDM (Product Management) || Six Sigma Yellow Belt Certified
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:
NOTE: Feel free to reach me in incase of any issues while running the project.
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?