Skip to content

Instantly share code, notes, and snippets.

@alekssamos
Forked from alexeygrigorev/tqdm_pool.py
Created November 10, 2023 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alekssamos/5899a0a65ba61d1916e3483e6d2ec21d to your computer and use it in GitHub Desktop.
Save alekssamos/5899a0a65ba61d1916e3483e6d2ec21d to your computer and use it in GitHub Desktop.
Track progress of ProcessPoolExecutor with tqdm
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