如何解决OverflowError:无法将浮点无穷大转换为整数

时间:2019-02-20 02:35:57

标签: python python-imageio

我正在尝试使用imageio API读取框架。我有一个使用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。感谢您的建议。谢谢。

2 个答案:

答案 0 :(得分:0)

在Python中,浮点类型可以为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进行调查,以解决无限浮动问题。

答案 1 :(得分:0)

根据imageio的新的2019年更新2.5.0版本: “ ffmpeg插件的阅读器现在总是报告inf作为帧数。使用reader.count_frames()获取实际数字,或根据fps和持续时间估算元数据。” imageio update