imagededup这个python库它自动的会执行多线程读取数据的过程,但是他的准备时间可能太长了,官方示例是假设你的图像都在同一个文件夹。但是我的使用场景是要处理多个文件夹,每一个文件夹都要走一遍准备线程-跑线程-结束线程的过程。导致大量的时间在线程的准备和结束上。所以我选择用vthread来实现多线程,所有的图像文件无论位置在哪,都会统一使用处理,线程仅准备一次,并且,在给图像编码的时候,使用的imagededup的encode_image方法。
thread_num = 16
@vthread.pool(thread_num)
def encoding_image_files(all_images_path_dict:dict,num:int,):
for idx,file in enumerate(tqdm(all_images_path_dict)):
if idx%thread_num!=num:
continue
src=all_images_path_dict[file][0]
image=np.array(Image.open(src))
code = phasher.encode_image(image_array=image)
all_images_path_dict[file][1]=code
for thread_idx in range(thread_num):
encoding_image_files(all_images_path_dict,thread_idx)
vthread.pool.waitall()