简体   繁体   English

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

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

I am using a NN to detect 4 types of objects (chassis, front-spoiler, hubcap, wheel) in the live feed of my webcam.我正在使用 NN 在网络摄像头的实时画面中检测 4 种类型的物体(底盘、前扰流板、轮毂罩、车轮)。 When one is detected, I want to display an image with information about it (chassis.png, front-spoiler.png, hubcap.png, wheel.png).当检测到一个时,我想显示一张包含它相关信息的图像(chassis.png、front-spoiler.png、hubcap.png、wheel.png)。 When I run my NN and hold one of the items in front of the webcam, the opencv windows freezes and doesnt display anything.当我运行我的 NN 并将其中一个项目放在网络摄像头前时,opencv windows 冻结并且不显示任何内容。 What is the reason for that?这是什么原因?

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)

I had a similar issue.我有一个类似的问题。 Adding cv2.waitKey after cv2.imshow helped in my case:在 cv2.imshow 之后添加 cv2.waitKey 对我的情况有所帮助:

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

Your code contains no waitKey at all.您的代码根本不包含waitKey

OpenCV GUI ( imshow ) requires waitKey to work. OpenCV GUI ( imshow ) 需要waitKey才能工作。

This is described in all OpenCV documentation and tutorials.这在所有 OpenCV 文档和教程中都有描述。

waitKey isn't about delays or breaks. waitKey与延迟或中断无关。 It runs the event loop that all GUI processing requires.它运行所有 GUI 处理所需的事件循环。

You can use waitKey(1) for the shortest non-zero delay of one millisecond (a little more in practice), or you can use pollKey() , which will not wait even that millisecond.您可以使用waitKey(1)来获得一毫秒的最短非零延迟(在实践中多一点),或者您可以使用pollKey() ,它甚至不会等待那毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM