# 加载模型print("LOADING YOLO")
net = cv2.dnn.readNet("yolov3.weights","yolov3.cfg")# 加载类别
classes =[]withopen("coco.names","r")as f:
classes =[line.strip()for line in f.readlines()]# get layers of the network
layer_names = net.getLayerNames()# 确定 YOLO 模型的输出层名称
output_layers =[layer_names[i[0]-1]for i in net.getUnconnectedOutLayers()]print("YOLO LOADED")
3、读取视频流,检测
video_capture = cv2.VideoCapture(0)whileTrue:# Capture frame-by-frame
re, img = video_capture.read()
img = cv2.resize(img,None, fx=0.4, fy=0.4)
height, width, channels = img.shape
# 使用opencv的blob函数对图像进行预处理
blob = cv2.dnn.blobFromImage(img,1/255.0,(416,416),
swapRB=True, crop=False)# Detecting objects
net.setInput(blob)
outs = net.forward(output_layers)# 在屏幕上显示信息
class_ids =[]
confidences =[]
boxes =[]for out in outs:for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]if confidence >0.5:# Object detected
center_x =int(detection[0]* width)
center_y =int(detection[1]* height)
w =int(detection[2]* width)
h =int(detection[3]* height)# Rectangle coordinates
x =int(center_x - w /2)
y =int(center_y - h /2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)# 使用NMS功能来执行非最大值抑制# 我们给出 IT 分数阈值和 NMS 阈值作为参数。7
indexes = cv2.dnn.NMSBoxes(boxes, confidences,0.5,0.4)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0,255, size=(len(classes),3))for i inrange(len(boxes)):if i in indexes:
x, y, w, h = boxes[i]
label =str(classes[class_ids[i]])
color = colors[class_ids[i]]
cv2.rectangle(img,(x, y),(x + w, y + h), color,2)
cv2.putText(img, label,(x, y +30), font,2, color,3)
cv2.imshow("Image", cv2.resize(img,(800,600)))if cv2.waitKey(25)&0xFF==ord('q'):break