我正在用Python Opencv进行YOLO对象检测。但出于某种原因,它没有反应。如果我只是在没有目标检测代码的情况下打开相机,效果会很好。但是如果我加上目标检测代码,它根本没有反应。这是我的代码:
import cv2
import numpy as np
net = cv2.dnn.readNet('yolov3.cfg', 'yolov3.weights')
classes = []
cap = cv2.VideoCapture(0)
with open('coco.names', 'r') as f:
classes = f.read().splitlines()
while True:
_, img = cap.read()
height, width, _ = img.shape
blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0, 0, 0), swapRB=True, crop=False)
net.setInput(blob)
output_layers_names = net.getUnconnectedOutLayersNames()
layerOutputs = net.forward(output_layers_names)
boxes = []
confidence = []
class_ids = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
if confidence > [0.5]:
center_x = int(detection[0]*width)
center_y = int(detection[1]*height)
w = int(detection[2]*width)
h = int(detection[3]*height)
x = int(center_x - w/2)
y = int(center_y - h/2)
boxes.append([x, y, w, h])
confidence.append((float(confidence)))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidence, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(len(boxes), 3))
if len(indexes)>0:
for i in indexes.flatten():
x,y,w,h = boxes[i]
label = str(classes[class_ids[i]])
confidence = str(round(confidence[i], 2))
color = colors[i]
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
cv2.putText(img, label + '' + confidence, (x, y+20), font, 2, (255, 255, 255), 2)
cv2.imshow("Video", img)
if cv2.waitKey(0) & 0xFF == ord('q'):
break
如果我运行此代码,相机会打开,但如果我移动我的手,它不会移动我的手。这是我的代码问题还是我的电脑问题?请帮帮我
顺便说一句,代码在上传到堆栈溢出时得到了更多的空间,对此我深表歉意
请尝试给waitKey()函数更多时间:
请阅读此处的文档以供参考waitKey():
另外,检查代码的缩进。 尝试运行这个较小的版本,如果可以,请逐步添加对象检测以发现任何其他问题:
或更具可读性:
尝试此操作以测试探测器
试试这个循环,如果探测器正在工作,你应该在终端上看到一个探测列表。 如果你站在摄像机前面,你会看到索引0处的高分,这是
coco.names
类人工作完成后,逐步添加其余代码,检查结果
相关问题 更多 >
编程相关推荐