为什么我的Opencv没有响应或执行任何操作?

2024-04-25 04:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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

如果我运行此代码,相机会打开,但如果我移动我的手,它不会移动我的手。这是我的代码问题还是我的电脑问题?请帮帮我

顺便说一句,代码在上传到堆栈溢出时得到了更多的空间,对此我深表歉意


Tags: 代码imgoutputnetnamesnpcv2class
1条回答
网友
1楼 · 发布于 2024-04-25 04:22:12

请尝试给waitKey()函数更多时间:

cv2.waitKey(10) & 0xFF == ord('q')
            ^^

请阅读此处的文档以供参考waitKey()

The function waitKey waits for a key event infinitely (when 𝚍𝚎𝚕𝚊𝚢≤0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.

另外,检查代码的缩进。 尝试运行这个较小的版本,如果可以,请逐步添加对象检测以发现任何其他问题:

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

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

工作完成后,逐步添加其余代码,检查结果

相关问题 更多 >