【问题标题】:Getting specific images from a folder using a csv file with the labels of the images使用带有图像标签的 csv 文件从文件夹中获取特定图像
【发布时间】:2021-02-12 10:39:46
【问题描述】:

我有一个文件夹,其中包含 4000 张胸部 X 光片的图像,它们被归类为是否有病理。有一个 csv 文件,其中包含每个图像的标签和文件的名称。我已经用一个小的 python 代码选择了前 100 个“正常”图像,但我知道我想获取这些图像并将它们复制到一个新文件夹中。我怎么做?我尝试了很多我在网上看到的建议,但没有一个适用于我想做的事情。感谢您的帮助。

dataset = pd.read_csv("Data_Entry_2017.csv")

No_Finding = dataset.loc[dataset['Finding Labels'] == 'No Finding']
No_Finding = No_Finding.iloc[:100,0]
print(No_Finding)
No_Finding.to_csv(path_or_buf='NormalImages.csv')
df = pd.read_csv("NormalImages.csv")
df = df.iloc[:,1].values

def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
    img = cv2.imread(os.path.join(folder,filename))
    if img is not None:
        images.append(img)
return images

我尝试加载图像,但即使这样也开始变得困难。

【问题讨论】:

  • 能否添加您目前拥有的代码,以及 CSV 文件中的一些文件名和行示例?
  • @james 希望对您有所帮助。我认为这是非常基本的东西,但我对此的经验很少,我仍在努力解决。
  • 为什么要将所有图像加载到图像列表中?一次 4000 张这样的图像似乎过多。如果您将图像输入 keras cnn,您可以逐步进行。
  • @GoldenLion 我想通了。重点是从数据集中仅选择图像子集并将它们复制到文件夹中。稍后,这些图像用于训练生成对抗网络。因此,我必须控制文件夹中有多少图像,因为我一直在尝试不同的数量。原始数据集有一个文件夹和一个 csv 文件,其中包含每个图像的标签和其他信息,这就是我必须这样做的原因。谢谢
  • 您得到解决文件夹和文件问题的答案了吗

标签: python image


【解决方案1】:
 # Source path with original set of images
source = "C:/Users/.../images"
  
# Destination path for the first 100 normal images
destination = "C:/Users/.../images_normal"
  

# Copy normal images to destination folder
for file in df:
    shutil.copy(os.path.join(source, file), destination)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多