Skip to content

Instantly share code, notes, and snippets.

@leandrobmarinho
Last active December 2, 2023 20:18
Show Gist options
  • Save leandrobmarinho/a5c4b026cc392d849840e9c639ec2450 to your computer and use it in GitHub Desktop.
Save leandrobmarinho/a5c4b026cc392d849840e9c639ec2450 to your computer and use it in GitHub Desktop.
Darknet using OpenCV in python
import cv2
import numpy as np
# Load YOLO
net = cv2.dnn.readNet("./yolov2.weights", "./cfg/yolov2.cfg")
classes = []
with open("./data/coco.names", "r") as f:
classes = [line.strip() for line in f]
# Set up OpenCV video capture
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("http://<IP>:8080/video")
while True:
# Read a frame from the camera
ret, frame = cap.read()
# Create a blob from the frame
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
# Get the output layer names
output_layer_names = net.getUnconnectedOutLayersNames()
# Run forward pass to get output from output layer
outputs = net.forward(output_layer_names)
# Process each output
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * frame.shape[1])
center_y = int(detection[1] * frame.shape[0])
width = int(detection[2] * frame.shape[1])
height = int(detection[3] * frame.shape[0])
# Calculate top-left corner of bounding box
x = int(center_x - width / 2)
y = int(center_y - height / 2)
# Draw bounding box and label on the frame
cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 255, 0), 2)
label = f"{classes[class_id]}: {confidence:.2f}"
cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow("YOLO Object Detection", frame)
# Break the loop when the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close the window
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment