OpenCV在关闭窗口时遇到问题

时间:2018-09-07 15:17:36

标签: python opencv opencv-python

我正在使用OpenCV从帧流中读取图像,大约每半分钟显示一次。代码是:

import cv2
import zmq
import base64
import numpy as np

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://An IP')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))

cv2.startWindowThread()
cv2.namedWindow("Stream")

while True:
    try:

        frame= footage_socket.recv_string()
        img= base64.b64decode(frame)
        npimg= np.fromstring(img, dtype=np.uint8)
        decoded= cv2.imdecode(npimg, 1) 
        cv2.imshow("Stream", decoded)      
    except KeyboardInterrupt:
        break

cv2.destroyAllWindows()

Ctrl + C 似乎无济于事。
我尝试添加:

if cv2.waitKey(1)& 0xFF == ord('q'):
        break

但是我收到错误Attempt to unlock mutex that was not locked Aborted并且我的程序根本没有运行。

1 个答案:

答案 0 :(得分:0)

您可以重新启动内核,并在键盘交互触发时添加cv2.destroyAllWindows()

while True:
try:

    frame= footage_socket.recv_string()
    img= base64.b64decode(frame)
    npimg= np.fromstring(img, dtype=np.uint8)
    decoded= cv2.imdecode(npimg, 1) 
    cv2.imshow("Stream", decoded)
    if cv2.waitKey(1)& 0xFF == ord('q'):
        break      
except KeyboardInterrupt:
    cv2.destroyAllWindows()
    break