how to to tackle OverflowError: cannot convert float infinity to integer

how to to tackle OverflowError: cannot convert float infinity to integer

我正在尝试使用 imageio API 读取帧。我有一个 reader 作为我使用 imageio.get_reader(video_path,"ffmpeg")

收到的对象
nframes =int(reader.get_length())     
for ii in range(nframes):
    while frame_q.qsize() > 500: # so that we dont use huge amounts of memory
        time.sleep(1)
    cur_img = reader.get_next_data()
    frame_q.put(cur_img)
    #shape = cur_img.shape
    #noisy_img = np.uint8(cur_img.astype(np.float) + np.random.randn(*shape) * 20)
    #frame_q.put(noisy_img)
    if ii % 100 == 0:
        print("%i / %i frames in queue" % (ii, nframes))
print("All %i frames in queue" % (nframes))

回溯:

 Traceback (most recent call last):
  File "/home/prashantb/anaconda3/envs/demo/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/home/prashantb/anaconda3/envs/demo/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "multiprocess_detect_actions.py", line 61, in read_frames
    nframes =int(reader.get_length())
OverflowError: cannot convert float infinity to integer

最初,nframe 是一个浮点值,然后我尝试将其转换为一个整数,但后来我得到一个 OverflowError.I 将感谢您对此的建议。谢谢你。

在Python中,float类型可以是infinity。要修复您的错误,请在转换为 int:

之前检查该值是否为 inf-inf
def float_to_int(x):
    if x == float('inf') or x == float('-inf'):
        return float('nan') # or a large value you choose
    return int(x)

正如@ShadowRanger 指出的那样,此解决方案只是防止 OverflowError 错误。您应该调查 reader 以解决无限浮动的问题。

根据 imageio 的新 2019 更新 2.5.0 版本: "The reader of the ffmpeg plugin now always reports inf as the number of frames. Use reader.count_frames() to get the actual number, or estimate it from the fps and duration in the meta data." imageio update