前提
動画の全てのフレームのハッシュ値を取得し,hash_dict ={}にhash_dict[i] = ハッシュ値(iはフレーム番号)を入れるプログラムを書きました.
実現したいこと
この処理を並列化したいです.
発生している問題・エラーメッセージ
エラーは出てきませんが,全フレームの処理が終わってないのにプログラムが完了してしまいます.
例:8415フレームのうち3000フレームしか処理されない,5430フレームのうち1079フレームしか処理されない.
該当のソースコード
python
1import cv2 2import numpy as np 3import imagehash 4from PIL import Image 5from concurrent.futures import ProcessPoolExecutor 6 7def cv2pil(image): 8 ''' OpenCV型 -> PIL型 ''' 9 new_image = image.copy() 10 if new_image.ndim == 2: # モノクロ 11 pass 12 elif new_image.shape[2] == 3: # カラー 13 new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB) 14 elif new_image.shape[2] == 4: # 透過 15 new_image = cv2.cvtColor(new_image, cv2.COLOR_BGRA2RGBA) 16 new_image = Image.fromarray(new_image) 17 return new_image 18 19def hash_image(image): 20 return imagehash.phash(cv2pil(image)) 21 # compute the hash of the image 22 23def main(): 24 # open the video 25 VIDEO_PATH = *** 26 video = cv2.VideoCapture(VIDEO_PATH) 27 28 # get the number of frames 29 num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) 30 hash_dict = {} 31 32 # create a pool of processes 33 with ProcessPoolExecutor() as executor: 34 # process the frames in parallel 35 for i, frame_hash in enumerate(executor.map(hash_image, video.read()[1])): 36 print('Frame {} of {} has hash {}'.format(i, num_frames, frame_hash)) 37 hash_dict[i] = frame_hash 38 39 40 41if __name__ == '__main__': 42 main()
追加質問(答えていただけたら幸いです)
このコードを
def main():
if name == 'main':
main()
なしで実行するとなぜか実行できません.なぜですか
> 追加質問
関数定義のみになるから、です。
def main():も消していますので,それは違うと思います.すみません
はい、cv2pil() と hash_image() の定義だけになるから、です。つまり、def main(): 以降のインデントがそのままになっているからではないでしょうか。
私の質問が悪いみたいです.
正しくは以下のようなエラーがでます
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 263, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\username\Desktop\Projects\V2PC\tests\test.py", line 40, in <module>
Traceback (most recent call last):
File "<string>", line 1, in <module>
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
for i, frame_hash in enumerate(executor.map(hash_image, video.read()[1])):
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py", line 674, in map
Traceback (most recent call last):
results = super().map(partial(_process_chunk, fn),
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 600, in map
File "<string>", line 1, in <module>
fs = [self.submit(fn, *args) for args in zip(*iterables)]
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 600, in <listcomp>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
Traceback (most recent call last):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 263, in run_path
return _run_module_code(code, init_globals, run_name,
fs = [self.submit(fn, *args) for args in zip(*iterables)]
Traceback (most recent call last):
File "<string>", line 1, in <module>
Traceback (most recent call last):
exitcode = _main(fd, parent_sentinel)
File "<string>", line 1, in <module>
main_content = runpy.run_path(main_path,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 263, in run_path
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py", line 645, in submit
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
File "<string>", line 1, in <module>
exitcode = _main(fd, parent_sentinel)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
Traceback (most recent call last):
Traceback (most recent call last):
return _run_module_code(code, init_globals, run_name,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code
self._start_queue_management_thread()
exitcode = _main(fd, parent_sentinel)
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code
exitcode = _main(fd, parent_sentinel)
prepare(preparation_data)
File "<string>", line 1, in <module>
File "<string>", line 1, in <module>
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
_run_code(code, mod_globals, init_globals,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
exitcode = _main(fd, parent_sentinel)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
_run_code(code, mod_globals, init_globals,
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
exec(code, run_globals)
exitcode = _main(fd, parent_sentinel)
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py", line 584, in _start_queue_management_thread
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
exitcode = _main(fd, parent_sentinel)
File "<string>", line 1, in <module>
exec(code, run_globals)
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
File "c:\Users\username\Desktop\Projects\V2PC\tests\test.py", line 40, in <module>
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
Traceback (most recent call last):
_fixup_main_from_path(data['init_main_from_path'])
self._adjust_process_count()
File "<string>", line 1, in <module>
prepare(preparation_data)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
File "c:\Users\username\Desktop\Projects\V2PC\tests\test.py", line 40, in <module>
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
for i, frame_hash in enumerate(executor.map(hash_image, video.read()[1])):
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py", line 608, in _adjust_process_count
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 236, in prepare
exitcode = _main(fd, parent_sentinel)
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 125, in _main
main_content = runpy.run_path(main_path,
_fixup_main_from_path(data['init_main_from_path'])
File "<string>", line 1, in <module>
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
_fixup_main_from_path(data['init_main_fr
def main():
if name == 'main':
main()
を消すとともにインデントも正したうえで実行しています
※ 解決した様なので削除
このコメント欄は、整形もされないし折畳みもできないので、そういう情報は質問を編集して追記したほうがいいですよ。