我在 Ubuntu 上的 OpenCV 4.4.0.46 中捕获和处理 IP 摄像机 RTSP 流。
不幸的是,处理需要相当长的时间,大约每帧 0.2 秒,并且流很快就会延迟。
视频文件必须保存 5 分钟,但通过此延迟视频文件只能保存 3-4 分钟。
我们可以更快地处理以克服延迟吗?
我有两个 IP 摄像机,它们有两个不同的 fps_rate(摄像机 1 有 18000,摄像机 2 有 20 fps)
我正在不同的 Ubuntu PC 中实现此代码
input_stream = 'rtsp://'+username+':'+password+'@'+ip+'/user='+username+'_password='+password+'_channel=0channel_number_stream=0.sdp'
input_stream---> rtsp://admin:Admin123@192.168.1.208/user=admin_password=Admin123_channel=0channel_number_stream=0.sdp
input_stream---> rtsp://Admin:@192.168.1.209/user=Admin_password=_channel=0channel_number_stream=0.sdp
vs = cv2.VideoCapture(input_stream)
fps_rate = int(vs.get(cv2.CAP_PROP_FPS))
I have two IP camera which have two diffrent fps_rate(Camera 1 have 18000 and camera 2 have 20 fps)
video_file_name = 0
start_time = time.time()
while(True):
ret, frame = vs.read()
time.sleep(0.2) # <= Simulate processing time (mask detection, face detection and many detection is hapning)
### Start of writing a video to disk
minute = 5 ## saving a file for 5 minute only then saving another file for 5 min
second = 60
minite_to_save_video = int(minute) * int(second)
# if we are supposed to be writing a video to disk, initialize
if time.time() - start_time >= minite_to_save_video or video_file_name == 0 :
## where H = heigth, W = width, C = channel
H, W, C = frame.shape
print('time.time()-->',time.time(),'video_file_name-->', video_file_name, ' #####')
start_time = time.time()
video_file_name = str(time.mktime(datetime.datetime.now().timetuple())).replace('.0', '')
output_save_directory = output_stream+str(int(video_file_name))+'.mp4'
fourcc = cv2.VideoWriter_fourcc(*'avc1')
writer = cv2.VideoWriter(output_save_directory, fourcc,20.0,(W, H), True)
# check to see if we should write the frame to disk
if writer is not None:
try:
writer.write(frame)
except Exception as e:
print('Error in writing video output---> ', e)
最佳答案
我看到了两种处理这个问题的选择。
关于python - 在opencv + python中处理帧时实时流被延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66687813/