python - 在opencv + python中处理帧时实时流被延迟

标签 python opencv image-processing video ffmpeg

我在 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() 直接转到 write() 而不进行任何处理),然后当你完成可以从该视频文件中读取并在闲暇时处理这些帧,因为此处的延迟不会导致您跳过帧。
  • 关于python - 在opencv + python中处理帧时实时流被延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66687813/

    相关文章:

    模块中的 python 导入类抛出 ImportError

    python - 在python中生成1到6之间的6个随机数列表

    python - 如何替换 Python pathlib.Path 中的子字符串?

    c++ - 模板匹配亚像素精度

    c++ - 线程中 OpenCv 3.2.0 的内存泄漏

    Matlab:蒙版/创建一个知道其原点具有一定半径的圆形 roi

    javascript - 如何只计算有效的 jquery 文件上传?

    python - 在 Wagtail 管理中将编辑者限制为自己的内容时出现问题

    java - 在图像识别尝试中获取空指针

    objective-c - 如何在 Xcode 中使用图像过渡滤镜?