Created
December 6, 2018 15:36
-
-
Save alexeygrigorev/79c97c1e9dd854562df9bbeea76fc5de to your computer and use it in GitHub Desktop.
Track progress of ProcessPoolExecutor with tqdm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from glob import glob | |
import multiprocessing | |
from concurrent.futures import ProcessPoolExecutor | |
import cv2 | |
from PIL import Image | |
import imagehash | |
from tqdm import tqdm | |
num_cores = multiprocessing.cpu_count() | |
files = glob("images/*") | |
def process(img_file): | |
try: | |
cvimg = cv2.imread(img_file) | |
img = Image.fromarray(cvimg) | |
phash = str(imagehash.phash(img)) | |
dhash = str(imagehash.dhash(img)) | |
whash = str(imagehash.whash(img)) | |
return img_file, phash, dhash, whash | |
except: | |
return img_file, None, None, None | |
with ProcessPoolExecutor(max_workers=num_cores) as pool: | |
with tqdm(total=len(files)) as progress: | |
futures = [] | |
for file in files: | |
future = pool.submit(process, file) | |
future.add_done_callback(lambda p: progress.update()) | |
futures.append(future) | |
results = [] | |
for future in futures: | |
result = future.result() | |
results.append(result) | |
with open('results.txt', 'w') as f_out: | |
for img, h1, h2, h3 in results: | |
f_out.write(img) | |
f_out.write('\t') | |
f_out.write(str(h1)) | |
f_out.write('\t') | |
f_out.write(str(h2)) | |
f_out.write('\t') | |
f_out.write(str(h3)) | |
f_out.write('\n') | |
f_out.flush() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment