Я написал фрагмент кода для получения значения RGB в заданном месте на изображениях и был удивлен тем, как много времени это заняло.Вот код, который я использовал:
path ="Images/"
listFiles = [f for f in listdir(path) if isfile(join(path, f))]
listFiles.sort()
rgbValue = np.zeros((len(listFiles), 3))
start = timeit.default_timer()
for i in range(50):
for index, file in enumerate(listFiles):
im = Image.open(path+file)
pix = im.load()
rgbValue[index, :] = pix[50, 50] # Get the RGBA Value of the a pixel of an image in the position 50, 50
im.close()
end = timeit.default_timer()
print("Done in {} s. Average time is {} s.".format(end-start, (end-start)/50))
Я работал с набором изображений (собак) из Google Images.Есть 68 изображений на общую сумму ~ 500 кб.Учитывая, что я 50 раз перебираю набор данных, я работаю с 4300 изображениями и около 2,5 Мб.И это заняло от 3 до 4 секунд!
Следовательно, я использовал cProfile, чтобы увидеть, где может быть узкое место в моем коде.Я поместил основную часть в функцию с именем pxValues, затем вызвал ее из cProfile:
def pxValues():
for i in range(50):
for index, file in enumerate(listFiles):
im = Image.open(path+file)
pix = im.load()
rgbValue[index, :] = pix[50, 50]
im.close()
return True
cProfile.run('pxValues()')
615404 function calls in 3.655 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.075 0.075 3.655 3.655 <ipython-input-5-ccab4473739f>:1(pxValues)
1 0.000 0.000 3.655 3.655 <string>:1(<module>)
3400 0.003 0.000 0.003 0.000 BmpImagePlugin.py:49(_accept)
3400 0.002 0.000 0.002 0.000 GifImagePlugin.py:38(_accept)
3400 0.003 0.000 0.003 0.000 Image.py:2492(_decompression_bomb_check)
3400 0.024 0.000 0.702 0.000 Image.py:2512(open)
3400 0.019 0.000 0.350 0.000 Image.py:2556(_open_core)
3400 0.002 0.000 0.002 0.000 Image.py:358(preinit)
3400 0.010 0.000 0.019 0.000 Image.py:418(_getdecoder)
3400 0.006 0.000 0.006 0.000 Image.py:519(__init__)
3400 0.022 0.000 0.036 0.000 Image.py:560(close)
3400 0.003 0.000 0.005 0.000 Image.py:586(__del__)
6800 0.006 0.000 0.009 0.000 Image.py:794(load)
3400 0.059 0.000 2.837 0.001 ImageFile.py:130(load)
3400 0.005 0.000 0.062 0.000 ImageFile.py:259(load_prepare)
3400 0.001 0.000 0.001 0.000 ImageFile.py:268(load_end)
27200 0.010 0.000 0.016 0.000 ImageFile.py:522(_safe_read)
3400 0.001 0.000 0.001 0.000 ImageFile.py:66(_tilesort)
3400 0.013 0.000 0.298 0.000 ImageFile.py:78(__init__)
3400 0.033 0.000 0.047 0.000 JpegImagePlugin.py:151(SOF)
3400 0.025 0.000 0.033 0.000 JpegImagePlugin.py:199(DQT)
3400 0.002 0.000 0.002 0.000 JpegImagePlugin.py:292(_accept)
3400 0.068 0.000 0.275 0.000 JpegImagePlugin.py:304(_open)
3400 0.002 0.000 0.007 0.000 JpegImagePlugin.py:418(_getmp)
3400 0.005 0.000 0.005 0.000 JpegImagePlugin.py:481(_getmp)
17000 0.016 0.000 0.037 0.000 JpegImagePlugin.py:52(Skip)
3400 0.026 0.000 0.041 0.000 JpegImagePlugin.py:57(APP)
3400 0.013 0.000 0.317 0.000 JpegImagePlugin.py:763(jpeg_factory)
3400 0.002 0.000 0.002 0.000 TiffImagePlugin.py:252(_accept)
3400 0.003 0.000 0.011 0.000 __init__.py:1284(debug)
3400 0.003 0.000 0.003 0.000 __init__.py:1528(getEffectiveLevel)
3400 0.004 0.000 0.008 0.000 __init__.py:1542(isEnabledFor)
81600 0.015 0.000 0.015 0.000 _binary.py:23(i8)
74800 0.029 0.000 0.054 0.000 _binary.py:71(i16be)
6800 0.005 0.000 0.009 0.000 _util.py:13(isPath)
3400 0.002 0.000 0.002 0.000 _util.py:23(__init__)
3400 0.006 0.000 0.006 0.000 {built-in method PIL._imaging.jpeg_decoder}
3400 0.057 0.000 0.057 0.000 {built-in method PIL._imaging.new}
74800 0.025 0.000 0.025 0.000 {built-in method _struct.unpack}
3400 0.002 0.000 0.002 0.000 {built-in method builtins.divmod}
1 0.000 0.000 3.655 3.655 {built-in method builtins.exec}
6800 0.004 0.000 0.004 0.000 {built-in method builtins.getattr}
10200 0.007 0.000 0.007 0.000 {built-in method builtins.hasattr}
10200 0.006 0.000 0.006 0.000 {built-in method builtins.isinstance}
23800 0.003 0.000 0.003 0.000 {built-in method builtins.len}
3400 0.268 0.000 0.268 0.000 {built-in method io.open}
13600 0.002 0.000 0.002 0.000 {method 'append' of 'list' objects}
3400 0.002 0.000 0.002 0.000 {method 'cleanup' of 'ImagingDecoder' objects}
3400 0.150 0.000 0.150 0.000 {method 'close' of '_io.BufferedReader' objects}
3400 2.486 0.001 2.486 0.001 {method 'decode' of 'ImagingDecoder' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
3400 0.003 0.000 0.003 0.000 {method 'pixel_access' of 'ImagingCore' objects}
122400 0.095 0.000 0.095 0.000 {method 'read' of '_io.BufferedReader' objects}
10200 0.016 0.000 0.016 0.000 {method 'seek' of '_io.BufferedReader' objects}
3400 0.004 0.000 0.004 0.000 {method 'setimage' of 'ImagingDecoder' objects}
3400 0.006 0.000 0.007 0.000 {method 'sort' of 'list' objects}
Проблема заключается в том, что эта строка (ближе к концу отчета):
3400 2.486 0.001 2.486 0.001 {method 'decode' of 'ImagingDecoder' objects}
У меня следующие вопросы:
1) Использую ли я нужные инструменты?Мои знания ограничены.
2) Почему этот шаг является узким местом?Какие операции наносят такой ущерб процессору?Я ожидаю, что преобразование изображений в массив RGB будет довольно «быстрым».
3) Есть ли способ улучшить скорость моего кода?У меня есть наборы данных изображений размером в пару Гб, и для работы с ними требуется вечность.