【发布时间】:2020-10-13 09:41:44
【问题描述】:
我正在 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
如果我运行此代码,相机会打开,但如果我移动我的手,它不会移动我的手。这是我的代码问题还是我的电脑问题?请帮帮我
顺便说一句,代码在上传到堆栈溢出时得到了更多的空间或其他东西,对此我深表歉意。
【问题讨论】:
-
当你按下一个键时,是否传递到新帧?
-
不是当我按下键时,而是当我按下顶部的 X 按钮时,你知道窗口是如何关闭并再次打开的。当它再次打开时,它会传递到一个新框架
标签: python opencv object-detection yolo