CV2 VideoCapture的多重初始化
我需要在摄像头正在录制的时候使用它 --> 也就是要同时进行视频流传输
我听说不能对同一个摄像头进行多次初始化
video = cv2.VideoCapture(0)
所以我调用了另一个Python程序,在里面也调用了这个方法,但还是不行。
[ WARN:4@7.428] global cap_v4l.cpp:997 open VIDEOIO(V4L2:/dev/video0): 无法通过索引打开摄像头 [ERROR:4@7.429] global obsensor_uvc_stream_channel.cpp:159 getStreamChannelGroup 摄像头索引超出范围
我真的需要对我的使用情况进行多次初始化,不想完全改变一切。有解决办法吗?(线程不行,imageio也说设备正忙)如果多次初始化不可能,我会尝试直接从录制的帧进行视频流传输,但那样会很麻烦。
1 个回答
0
这个答案并不是说要多次初始化相机,而是要在录制和视频流之间共享画面。我们需要看看相机通常能处理多少帧,这样才能更好地进行平衡。
比如,可以这样声明:
FPS = 1/40
FPSSTREAM = 1/20
录制的方法:
....while(int(time.time() - start_time) < capture_duration ):
ret, frame = video.read()
if ret==True:
out.write(frame)
time.sleep(FPS) <---------
else: .....
视频流的方法:
def video_stream():
while camera_on:
ret, frame = video.read()
if not ret:
print("will adjust video stream speed, slight stuttering might occur")
time.sleep(ADJUST) <---- ignore this
else:
ret, buffer = cv2.imencode('.jpeg',frame)
frame = buffer.tobytes()
yield (b' --frame\r\n' b'Content-type: imgae/jpeg\r\n\r\n' + frame +b'\r\n')
time.sleep(FPSSTREAM) <--- here
如果还是有延迟,那就调整一下
out = cv2.VideoWriter(result,fourcc,15.0, (640,480)) <--
调整帧率,出于某种原因,15.0对我来说效果很好
如果还是有延迟,可以尝试使用多进程:
capture_process = mp.Process(target=recording, args=())
capture_process.start()