【问题标题】:cv2 reading video frames causes python generator to delay output till the endcv2读取视频帧导致python生成器延迟输出直到结束
【发布时间】:2020-07-22 19:05:33
【问题描述】:

我有一个 python 生成器,它从视频中输出 cv2 图像(帧),并使用concurrent.futures.ThreadPoolExecutor 对其进行处理。我看到 tqdm 挂起,直到发电机用完为止。但是,如果我从 gen_frames_dummy 提供执行程序,则 tqdm 会随着每个任务的完成而更新。

任何帮助将不胜感激。

def gen_frames(s):
    for frame_num in frames_2_get:
        # skip to frame in video
        s.set(cv2.CAP_PROP_POS_FRAMES, frame_num-1)
        res, frame = s.read()
        if res:
            yield frame
def gen_frames_dummy(s):
    for frame_num in frames_2_get:
        yield np.zeros((16,16,3))
def frame_op(f):
    # process the frame
    pass
with ThreadPoolExecutor() as executor:
    frames_g = gen_frames(vid_capture)
    list(tqdm(executor.map(frame_op, frames_g), total=len(frames2get))

【问题讨论】:

    标签: python python-3.x multithreading opencv tqdm


    【解决方案1】:

    map 等待所有工作人员完成,然后将结果传递给代码中的tqdm

    我想你可以这样重写以使 tqdm 按预期工作。

        for args in tqdm(frames_g, total=len(frames_2_get)):
            executor.submit(frame_op, args)
    

    【讨论】:

      猜你喜欢
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2021-11-25
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多