【问题标题】:CPU is 100% for multiprocessing queue to get frames from multiple camerasCPU 100% 用于多处理队列从多个摄像头获取帧
【发布时间】:2020-04-06 03:07:15
【问题描述】:

我正在从事一个图像处理项目,我希望 3 个摄像头的图片在按下按钮时获取最新帧,为此,我使用了 multiprocessing.process 和 multiprocessing.queue,如下面的代码所示。完成了要求的任务,但现在有两个问题:

任务管理器中的 1.cpu-100%(这会减慢程序速度)

2.代码中有cam.set(cv2.CAP_PROP_BUFFERSIZE, 0) 的解决方法,这是导致50% cpu 使用率的主要原因

实际上我想要一种快速获取帧的方法,所以我也尝试了 multiprocessing.pipe 而不是队列,但由于第二次按下按钮时它没有获得最新的帧,所以我不得不使用队列通信方法。任何有关代码的帮助将不胜感激

def camera_func(queue,cam_indx):
    cam = cv2.VideoCapture(cam_indx,cv2.CAP_DSHOW)   #current camera
    cam.set(cv2.CAP_PROP_BUFFERSIZE, 0)
    if cam.isOpened(): # Check success if the object is created and opened                    
        while True:
            try:
                flag, frame=cam.read()
                if flag==0:
                    break 
                # used to remove buffer size problem because it gets next frame 
                # rather latest frame   
                if not queue.empty():
                    try:
                        queue.get_nowait()   # discard previous (unprocessed) frame
                    except Queue.Empty:
                        pass

                queue.put(frame,False)

            except:
                continue      
    else:               
        cam.open()        
        raise Exception("Could not open camera")

queues=[]
for pip in range(0,a_cams):
    queu = Queue(maxsize=1)
    queues.append(queu)

processes = [Process(target=camera_func, args=(queues[x],x)) for x in range(a_cams)]   # Setup a list of processes that we want to run
for p in processes:     
    p.start()   # Run processes

更新:

我已经按照 nathancy 的建议使用了线程,现在由于来自here 的 fps 同步,它正在执行相同的任务而没有解决方法,CPU 使用率仍然是 80%,但这次启动时有 40 秒的延迟程序太多了。

【问题讨论】:

  • 你做过任何基准测试/分析吗?
  • 它的行为是线性的吗?例如1 个摄像头占用 40% 的 vcpu,2 个摄像头占用 80%,3 个摄像头占用 100%?你有一个多核系统,如果使用多线程可以超过 100%?
  • 如何进行基准分析?
  • 不,当没有解决方法时,它不会呈现线性 50% 的使用率,即按下按钮时我得到的连续帧不是最新的。当我做解决方法时,50% 会跳进去。我有i7 8gen 12核系统
  • 您是否尝试过线程而不是多处理?看看thisthis

标签: python multithreading opencv image-processing multiprocessing


【解决方案1】:

我遇到了类似的问题。我发现这与我检查队列的方式直接相关。

我也有

if not queue.empty():
    try:
        r = queue.get()
        ...

将 cpu 负载降低到接近零的原因是删除了 .empty() 检查。 .get() 调用会在继续之前自动等待队列中出现某些内容。

【讨论】:

    【解决方案2】:

    愿有人觉得它有用!

    由于没有人回答我的问题,所以尽我所能尝试一切,我发现仅将相机输入的线程版本中的 capture = cv2.VideoCapture(src) 行更改为 capture = cv2.VideoCapture(src,cv2.CAP_DSHOW) 会显着降低 CPU 使用率,而且开始时也没有延迟.

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 2014-09-07
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多