我正在使用 OpenCV-Python 3.1 遵循以下示例代码:http : //opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html并使用 http 摄像头流而不是默认相机,当相机物理断开连接时,videocapture 中的 read 函数永远不会返回“False”(或任何与此相关的东西),从而完全挂起/冻结程序。有谁知道如何解决这一问题?
import numpy as np
import cv2
cap = cv2.VideoCapture('http://url')
ret = True
while(ret):
# Capture frame-by-frame
ret, frame = cap.read()
print(ret)
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)