Python ImageIO:打开的文件过多

时间:2016-04-22 00:29:44

标签: python image ffmpeg python-imageio

我在python中使用imageio来打开目录中的所有视频文件并将它们转换为numpy数组。

这是我正在使用的脚本:

flavor1

最终此脚本因以下错误输出而崩溃:

  1 from __future__ import print_function
  2 from avi_to_numpy import *
  3 from os import listdir
  4 import numpy as np
  5 import imageio 
  6       
  7 class_path = '../Diving/'
  8 max_frames = 16
  9 stride = 8
 10 videos = [vid for vid in listdir(class_path)]
 11 train = []
 12 
 13 for vid in videos:
 14     print(str.format('Loading {}...', vid), end="")
 15     filename = class_path + vid
 16     reader = imageio.get_reader(filename, 'ffmpeg')
 17     frames = []
 18     
 19     for i, im in enumerate(reader):
 20         if len(frames) == max_frames:
 21             break
 22         
 23         if i % stride == 0:
 24             frames.append(im)
 25     
 26     reader.close()
 27     train.append(np.array(frames))
 28     print('done')        
 29 
 30 
 31 print(len(train))

我正在关闭来自imageio的Reader对象。似乎ffmpeg打开的文件没有正确关闭。

我在这里缺少明显的一步吗?我正确关闭文件吗?

编辑:找到临时解决方案。在github上打开了一个新问题。

我能够通过取消注释' imageio / plugins / ffmpeg.py'中的以下代码行来解决问题:

Traceback (most recent call last):
  File "load_class_test.py", line 16, in <module>
    reader = imageio.get_reader(filename, 'ffmpeg')
  File "/usr/local/lib/python2.7/site-packages/imageio/core/functions.py", line 111, in get_reader
    return format.get_reader(request)
  File "/usr/local/lib/python2.7/site-packages/imageio/core/format.py", line 158, in get_reader
    return self.Reader(self, request)
  File "/usr/local/lib/python2.7/site-packages/imageio/core/format.py", line 207, in __init__
    self._open(**self.request.kwargs.copy())
  File "/usr/local/lib/python2.7/site-packages/imageio/plugins/ffmpeg.py", line 260, in _open
    self._initialize()
  File "/usr/local/lib/python2.7/site-packages/imageio/plugins/ffmpeg.py", line 326, in _initialize
    stdout=sp.PIPE, stderr=sp.PIPE)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1223, in _execute_child
    errpipe_read, errpipe_write = self.pipe_cloexec()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1175, in pipe_cloexec
    r, w = os.pipe()
OSError: [Errno 24] Too many open files

然后我在381 def _close_streams(self): 382 for std in (self._proc.stdin, 383 self._proc.stdout, 384 self._proc.stderr): 385 try: 386 std.close() 387 except Exception: # pragma: no cover 388 pass 中添加了对上述函数的调用:

_close(self)

我不确定这样做的副作用是什么,但它为我提供了解决方案。

以下是问题的链接:https://github.com/imageio/imageio/issues/145

0 个答案:

没有答案