0

我正在使用 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,我的程序根本没有运行。

4

1 回答 1

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
于 2018-09-07T15:43:58.030 回答