OpenCV Window 尝试与神经网络图像分类结合时冻结

OpenCV Window Freezing when trying to combine with Neural Network Image Classification

我正在使用 NN 在我的网络摄像头的实时画面中检测 4 种类型的物体(底盘、前扰流板、轮毂盖、车轮)。当检测到一个时,我想显示一张包含有关它的信息的图像(chassis.png、front-spoiler.png、hubcap.png、wheel.png)。 当我 运行 我的 NN 并将其中一个项目放在网络摄像头前时,opencv windows 冻结并且不显示任何内容。这是什么原因?

def displayImg(path):
    img = cv2.imread(path)
    cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    cv2.imshow("window", img)

# ----------------LIVE DETECTIONS ---------------
imagePath = "picture.jpg"
frontSpoilerImageOpen = False
chassisImageOpen = False
hubcapImageOpen = False
wheelImageOpen = False

model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp5/weights/last.pt', force_reload=True)
cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    results = model(frame)

    try:
        detectedItem = results.pandas().xyxy[0].iloc[0, 6]
        if detectedItem == "front-spoiler" and not frontSpoilerImageOpen:

            frontSpoilerImageOpen = False
            chassisImageOpen = False
            hubcapImageOpen = False
            wheelImageOpen = False

            displayImg(os.path.join("imagesToDisplay", "front-spoiler.png"))
            frontSpoilerImageOpen = True

        elif detectedItem == "chassis" and not chassisImageOpen:

            frontSpoilerImageOpen = False
            chassisImageOpen = False
            hubcapImageOpen = False
            wheelImageOpen = False

            displayImg(os.path.join("imagesToDisplay", "chassis.png"))
            chassisImageOpen = True

        elif detectedItem == "hubcap" and not hubcapImageOpen:

            frontSpoilerImageOpen = False
            chassisImageOpen = False
            hubcapImageOpen = False
            wheelImageOpen = False

            displayImg(os.path.join("imagesToDisplay", "hubcap.png"))
            hubcapImageOpen = True


        elif detectedItem == "wheel" and not wheelImageOpen:

            frontSpoilerImageOpen = False
            chassisImageOpen = False
            hubcapImageOpen = False
            wheelImageOpen = False

            displayImg(os.path.join("imagesToDisplay", "wheel.png"))
            wheelImageOpen = True
    except Exception as e:
        print(e)

我遇到了类似的问题。在 cv2.imshow 之后添加 cv2.waitKey 对我的情况有所帮助:

cv2.imshow("window", img)
cv2.waitKey(1)  # perform GUI housekeeping tasks

您的代码根本不包含 waitKey

OpenCV GUI (imshow) 需要 waitKey 才能工作。

这在所有 OpenCV 文档和教程中都有描述。

waitKey 与延迟或休息无关。它运行所有 GUI 处理所需的事件循环。

你可以使用waitKey(1)来获得最短的non-zero一毫秒的延迟(实践中多一点),或者你可以使用pollKey(),它甚至不会等待那个毫秒.