Certainly! Here's an updated code snippet to process the outputs and visualize the bounding boxes with class labels and confidence scores:
import cv2
import numpy as np
# Load the pre-trained YOLO model
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# Use the webcam as input
cap = cv2.VideoCapture("http://192.168.8.165:8080/video")
# Specify the classes you trained the model for
classes = ["class1", "class2", "class3", ...]
# Set the minimum confidence threshold for detection
confidence_threshold = 0.5
while True:
# Read the current frame
ret, frame = cap.read()
# Prepare the frame to be fed into the YOLO network
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
# Pass the frame through the network and obtain detections
net.setInput(blob)
outputs = net.forward()
# Process the outputs to obtain bounding boxes and confidence scores
boxes = []
confidences = []
class_ids = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > confidence_threshold:
# Scale the bounding box coordinates to the original image size
width, height = frame.shape[1], frame.shape[0]
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
bbox_width = int(detection[2] * width)
bbox_height = int(detection[3] * height)
# Calculate the top-left corner coordinates of the bounding box
x = int(center_x - bbox_width / 2)
y = int(center_y - bbox_height / 2)
boxes.append([x, y, bbox_width, bbox_height])
confidences.append(float(confidence))
class_ids.append(class_id)
# Apply non-maxima suppression to remove redundant overlapping boxes
indices = cv2.dnn.NMSBoxes(boxes, confidences, confidence_threshold, 0.4)
# Visualize the results on the frame
font = cv2.FONT_HERSHEY_SIMPLEX
colors = np.random.uniform(0, 255, size=(len(class_ids), 3))
if len(indices) > 0:
for i in indices.flatten():
x, y, w, h = boxes[i]
label = classes[class_ids[i]]
confidence = confidences[i]
color = colors[i]
# Draw the bounding box
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
# Write class label and confidence score
text = f"{label}: {confidence:.2f}"
text_width, text_height = cv2.getTextSize(text, font, 0.5, 1)[0]
cv2.rectangle(frame, (x, y), (x + text_width, y - text_height - 5), color, -1)
cv2.putText(frame, text, (x, y - 3), font, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
# Display the frame with the detections
cv2.imshow("Object Detection", frame)
if cv2.waitKey(1) == 27:
break
# Release resources
cap.release()
cv2.destroyAllWindows()
In this updated code, the YOLO model's outputs are processed to extract the bounding box coordinates, class labels, and confidence scores. Then, non-maxima suppression is applied to remove redundant overlapping boxes. Finally, the bounding boxes, class labels, and confidence scores are visualized on the frame using OpenCV functions.
Feel free to modify the visualization part according to your specific requirements or preferences, such as adjusting font sizes or colors.
Remember to replace "class1", "class2", "class3" with your actual class labels.