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

青葱年少 pytorch 437

原文标题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 窗口冻结并且不显示任何内容。这是什么原因?

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)

原文链接:https://stackoverflow.com//questions/71478801/opencv-window-freezing-when-trying-to-combine-with-neural-network-image-classifi

回复

我来回复
  • Olli的头像
    Olli 评论

    我有一个类似的问题。在 cv2.imshow 之后添加 cv2.waitKey 对我的情况有帮助:

    cv2.imshow("window", img)
    cv2.waitKey(1)  # perform GUI housekeeping tasks
    
    2年前 0条评论
  • Christoph Rackwitz的头像
    Christoph Rackwitz 评论

    您的代码根本不包含waitKey

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

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

    • 一个基本教程:https://docs.opencv.org/4.x/db/deb/tutorial_display_image.html
    • imshow 的文档提到了要求:https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563

    waitKey不是关于延迟或中断。它运行所有 GUI 处理所需的事件循环。

    您可以使用waitKey(1)来获得最短的非零延迟一毫秒(实际上要多一点),或者您可以使用pollKey(),它甚至不会等待那毫秒。

    2年前 0条评论