首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在opencv + python中处理帧时,活动流会被延迟。

在opencv + python中处理帧时,活动流会被延迟。
EN

Stack Overflow用户
提问于 2021-03-18 09:01:11
回答 1查看 904关注 0票数 0

我在Ubuntu上的OpenCV 4.4.0.46中捕获并处理了一个IP相机RTSP流。不幸的是,处理过程花费了相当长的时间,大约每帧0.2s,而且流很快就会延迟。视频文件必须保存5分钟,但通过此延迟视频文件仅保存3-4分钟。

我们能否更快地处理以克服延误?

我有两个IP相机,它有两个不同的fps_rate(照相机1有18000,照相机2有20 fps)。

我正在不同的Ubuntu个人电脑上实现这个代码。

linux

  • Django==3.1.2

  • Ubuntu = 18.04和20.04

  • opencv-contrib-python==4.4.0.46

  • opencv-python==4.4.0.46

上的

  • Python3.8.5(默认值:2020年7月28日,12:59:40) GCC 9.3.0

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-18 14:36:57

我看到了处理这件事的两种选择。

  1. 可以有一个单独的线程,用于在缓冲区中读取和存储来自RTSP流的帧。您的处理将在主线程中进行,并将从该缓冲区请求帧,该缓冲区将移交最老的帧。这将确保您不会错过任何框架。但是,由于您的处理步骤相对于相机的框架非常慢,您可能会在缓冲区中得到数千张图片,这可能导致“内存不足”错误。

  1. 由于您似乎主要关心使用处理步骤创建视频,而不是实时显示视频,所以可以先保存视频的5分钟(从read()直接保存到写(),无需任何处理),完成后您可以从该视频文件中读取,并在空闲时处理这些帧,因为这里的延迟不会导致您跳过帧。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66687813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档