在 opencv + python 中处理帧时直播流被延迟
Live stream is gets delayed while processing frame in opencv + python
我在 Ubuntu 上的 OpenCV 4.4.0.46 中捕获并处理 IP 摄像机 RTSP 流。
不幸的是,处理需要相当多的时间,大约每帧 0.2 秒,并且流很快就会延迟。
视频文件必须保存 5 分钟,但通过这种延迟视频文件仅保存 3-4 分钟。
我们能否加快处理速度以克服延误?
我有两个 IP 摄像头,它们有两个不同 fps_rate(摄像头 1 有 18000,摄像头 2 有 20 fps)
我正在以不同的方式实现此代码 Ubuntu PC
- Python 3.8.5(默认,2020 年 7 月 28 日,12:59:40)[GCC 9.3.0] linux
- Django==3.1.2
- Ubuntu = 18.04 和 20.04
- opencv-contrib-python==4.4.0.46
- opencv-python==4.4.0.46
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)
我看到有两种处理方法。
您可以有一个单独的线程专用于从缓冲区中的 RTSP 流中读取和存储帧。您的处理发生在主线程中,并将从该缓冲区请求帧,该缓冲区将移交最旧的帧。这将确保您不会丢失任何帧。但是,由于您的处理步骤与相机的帧率相比非常慢,您最终可能会在缓冲区中拥有数千张图片,这可能会导致“内存不足”错误。
由于你似乎最关心的是用你的处理步骤创建视频而不是实时显示它,你可以先保存5分钟的视频(从 read() 直接写( ) 没有任何处理),完成后您可以从该视频文件中读取并随意处理这些帧,因为这里的延迟不会导致您跳过帧。
我在 Ubuntu 上的 OpenCV 4.4.0.46 中捕获并处理 IP 摄像机 RTSP 流。 不幸的是,处理需要相当多的时间,大约每帧 0.2 秒,并且流很快就会延迟。 视频文件必须保存 5 分钟,但通过这种延迟视频文件仅保存 3-4 分钟。
我们能否加快处理速度以克服延误?
我有两个 IP 摄像头,它们有两个不同 fps_rate(摄像头 1 有 18000,摄像头 2 有 20 fps)
我正在以不同的方式实现此代码 Ubuntu PC
- Python 3.8.5(默认,2020 年 7 月 28 日,12:59:40)[GCC 9.3.0] linux
- Django==3.1.2
- Ubuntu = 18.04 和 20.04
- opencv-contrib-python==4.4.0.46
- opencv-python==4.4.0.46
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)
我看到有两种处理方法。
您可以有一个单独的线程专用于从缓冲区中的 RTSP 流中读取和存储帧。您的处理发生在主线程中,并将从该缓冲区请求帧,该缓冲区将移交最旧的帧。这将确保您不会丢失任何帧。但是,由于您的处理步骤与相机的帧率相比非常慢,您最终可能会在缓冲区中拥有数千张图片,这可能会导致“内存不足”错误。
由于你似乎最关心的是用你的处理步骤创建视频而不是实时显示它,你可以先保存5分钟的视频(从 read() 直接写( ) 没有任何处理),完成后您可以从该视频文件中读取并随意处理这些帧,因为这里的延迟不会导致您跳过帧。