导入CV2 将 numpy 导入为 np 导入数学 导入操作系统 从设置导入配置,load_model 导入日期时间
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp' video_name =“rtsp://sfkhikvision:[电子邮件受保护]:554/Streaming/Channels/1” 视频 = cv2.VideoCapture(video_name, cv2.CAP_FFMPEG)
ret,frame = video.read()
if ret:
frame_resized = cv2.resize(frame, (416,416)) # resize frame for prediction
else:
continue
此代码生成视频(4秒)
生成后,video.read() 返回 None。
所以我很长时间无法获取视频流
解决办法是什么?
您需要在循环内连续从流中读取帧。修改您的代码如下:
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
video_name = "rtsp://sfkhikvision:[email protected]:554/Streaming/Channels/1"
video = cv2.VideoCapture(video_name, cv2.CAP_FFMPEG)
# use while-loop here to continuously capture frames
while True:
ret, frame = video.read()
if not ret:
print("Error: Could not read frame")
break
frame_resized = cv2.resize(frame, (416,416))