The steps for this recipe are as follows:
- Import the libraries:
import cv2
import numpy as np
- Set the variables:
with open("yolov3.txt", 'r') as f:
classes = [line.strip() for line in f.readlines()]
colors = np.random.uniform(0, 300, size=(len(classes), 3))
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
cap = cv2.VideoCapture(0)
scale = 0.00392
conf_threshold = 0.5
nms_threshold = 0.4
- Define our output layers:
def get_output_layers(net):
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in \
net.getUnconnectedOutLayers()]
return output_layers
- Create bounding boxes:
def create_bounding_boxes(outs,Width, Height):
boxes = []
class_ids = []
confidences = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold...