我正在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
如果我运行此代码,则相机会打开,但是如果我动手,它将不动手。这是我的代码问题还是我的PC问题?请帮助我
顺便说一句,在上传到堆栈溢出时,代码还有更多空间或其他东西,对此我表示歉意。
答案 0 :(得分:0)
尝试花更多时间使用waitKey()函数:
cv2.waitKey(10) & 0xFF == ord('q')
^^
在此处阅读文档以供参考waitKey():
函数waitKey无限等待键事件(当?????≤0时),或者等待正数时延迟毫秒。由于操作系统在切换线程之间的时间最短,因此该功能将不会等待确切的延迟毫秒,而是将至少等待延迟毫秒,具体取决于当时计算机上正在运行的其他东西。它返回按下的键的代码;如果在指定的时间过去之前没有按下任何键,则返回-1。
此外,请检查代码的缩进。 尝试运行此较小的版本,如果可行,请逐步添加对象检测以发现其他问题:
import cv2 as cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
cv2.imshow("Video", img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
或更可读:
keypressed = cv2.waitKey(10)
if keypressed == ord('q'):
break
尝试此循环,如果检测器正在工作,则应在终端上看到检测列表。
如果您将自己放在骆驼的前面,则应该在索引0(即coco.names
类 person )上看到高分。
while True:
_, img = cap.read()
img = cv2.resize(img, None, fx=0.5, fy=0.5) # just for more room on my screen
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()
print(output_layers_names) # <-- prints out the list of detections
layerOutputs = net.forward('yolo_82') # <-- try one layer at a time
for out in layerOutputs:
if not np.all(out[5:] == 0): print(out[5:])
print('-'*50)
cv2.imshow("Video", img)
keypressed = cv2.waitKey(10)
if keypressed == ord('q'):
break
一旦生效,请逐步添加其余代码,并检查结果。