我有一个pandas
数据帧,看起来像这样:
image_id category sub image_path
1000 A HH images/ID_1000.png
512 A HH images/ID_512.png
1002 C CC images/ID_1002.png
236 B XX images/ID_236.png
... ... ... .....
最终目标是什么?
category
对图像进行分组。阅读类别中的所有图像。对其进行转换,然后使用skimage
将其保存到磁盘。
将与图像所在行对应的所有值复制到新的数据帧中,并相应地更改image_path
以指向转换后的图像。
简而言之,我想使用pool
并行化此函数:
def transform_and_save(df, to_generate=5000):
categories = {"A":0, "B":1, "C":2}
save_dir_path = "new_images/"
new_df = pd.DataFrame(columns=df.columns)
final_count=0
for cls in classes_to_aug.keys():
orig_images = df[df["category"]==cls].reset_index(drop=True)
orig_count = len(orig_images)
nb_images_to_gen = to_generate - orig_count
counter = 0
stop = False
while counter < nb_images_to_gen:
for i in range(len(orig_images)):
all_values = orig_images.loc[i]
img = Path(orig_images["image_path"][i])
img_name = img.name
save_name = save_dir_path + "newimg_" + str(counter) + img_name
img = imread(img)
img = resize(img, (200, 200))
imsave(fname=save_name, check_contrast=False, arr=img)
all_values["image_path"] = save_name
new_df.loc[final_count] = all_values
counter += 1
final_count += 1
if counter > nb_images_to_gen:
stop=True
break
if stop:
break
return new_df