【发布时间】:2020-04-19 14:28:57
【问题描述】:
我正在尝试使用我的网络摄像头捕捉实时视频。
我从互联网上学到的代码就像一个魅力。
但是在我将我的 opencv 更新到 4.2.0 后出现了一个问题,即 videoCapture 窗口无论我尝试多少次,都不会关闭。
源代码
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv.flip(frame,1)
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
【问题讨论】:
-
单击显示图像的窗口(不是运行脚本的终端),然后按
q。 -
是的 q 有效,但关闭选项无效。为什么?
-
不知何故opencv无法直接理解窗口关闭事件,总是使用键盘。