多处理和(数据帧+图像处理)

2024-04-27 00:58:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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

Tags: topathname图像imageiddfimg