繁体   English   中英

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)来获得一毫秒的最短非零延迟(在实践中多一点),或者您可以使用pollKey() ,它甚至不会等待那毫秒。

暂无
暂无

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

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