简体   繁体   English

根据 yaml 文件中的标签对 1 个文件夹中的图像进行排序

[英]sorting images in 1 folder according to its labels in yaml file

I have a yaml file which has filename and label and looks like below:我有一个包含文件名和标签的 yaml 文件,如下所示:

- 1003520587_a35cd70a-22ab-4f89-92f8-e44884d6225d_boolean: no-checkbox
- 1003522794_3f24dcc8-60f1-4959-b0f4-d00a9f723a46_boolean: filled
- 1003522794_3f24dcc8-60f1-4959-b0f4-d00a9f723a46_boolean: empty

And I have all images in 1 folder.我在 1 个文件夹中有所有图像。 I would like to separate each image according to its label respectively into its folder using python.我想使用 python 将每个图像根据其标签分别分离到其文件夹中。 How would I do it?我该怎么做?

I am getting file doesn't exists error using following code.我使用以下代码获取文件不存在错误。

import yaml
import pandas as pd
import shutil
# base folders / template destinations
from_folder =  r'/home/user/Downloads/by_field/abschluss/'
to_folder_base = r'home/user/Downloads/sorted_image/abschluss'

with open('/home/user/Downloads/Yaml/abschluss.yaml', 'r') as stream:
        parsed_yaml=yaml.safe_load(stream)
        for i in parsed_yaml:
            for key in i:
                img_name = key + ".png"
                keyword = i[key]
                to_folder = os.path.join(to_folder_base, keyword)
                os.makedirs(to_folder, exist_ok=True)
                old_img_path = os.path.join(from_folder, img_name)
                new_img_path = os.path.join(to_folder, img_name)
                shutil.move(old_img_path, new_img_path)

FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError: [Errno 2] 没有这样的文件或目录

But when I checked using same path the file do exists但是当我使用相同的路径检查时,文件确实存在

I tried by converting yaml to csv file as well using pandas which looks like below: attached image我尝试使用如下所示的熊猫将yaml转换为 csv 文件:

import pandas as pd
# base folders / template destinations
from_folder = "/home/user/Downloads/images/Leibteil/"
to_folder_base = "/home/user/Downloads/sorted_image/"

# read in CSV file with pandas
meta_ham = (pd.read_csv('/home/user/Downloads/csv/Leibteil.csv'))
print(meta_ham)
# iterate through each row of csv
for index in range(len(meta_ham)):
    print(index)
    # get image name and corresponding group
    img_name = meta_ham[index] + ".png"
    print(img_name)
    keyword = meta_ham[label]
    print(keyword)
    # make a folder for this group, if it doesn't already exist. 
    # as long as exist_ok is True, then makedirs will do nothing if it already exists
    to_folder = os.path.join(to_folder_base, 'label')
    print(to_folder)
    os.makedirs(to_folder, exist_ok=True)
    # move the image from its original location to this folder
    old_img_path = os.path.join(from_folder, img_name)
    new_img_path = os.path.join(to_folder, img_name)
    shutil.move(old_img_path, new_img_path)

But using csv or pandas it throws Keyerror: 0但是使用 csv 或 pandas 它会抛出Keyerror: 0

You will need to check if the directory is created, and if the file exists for the yaml code but for the csv code it is an access error:您将需要检查目录是否已创建,以及 yaml 代码的文件是否存在,但 csv 代码是否存在访问错误:

# will return row number=index, and column=0
img_name = meta_ham.iloc[index, 0] + ".png"
# will return row number=index, and column=1
keyword = meta_ham.iloc[index, 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有图像标签的 csv 文件从文件夹中获取特定图像 - Getting specific images from a folder using a csv file with the labels of the images 根据文件名以一定顺序循环浏览文件夹中的所有图像 - Loop through all the images in a folder in a certain order according to their file names 将 CSV 文件中带有标签的图像文件夹转换为 tensorflow 数据集 - Convert folder of images with labels in CSV file into a tensorflow Dataset 如何根据我的火车 csv 文件中的文件名将文件夹中的图像分离为训练文件夹 - how to separate images in a folder to train folder according to the filename in my train csv file 想要将文件夹及其子文件夹中的所有图像编译为带有名称的pdf文件 - Want to compile all the images in a folder and its subfolders to pdf file with names 如何使用熊猫根据标签分割文件? - How to split a file according to labels using pandas? 根据Python中的第一列对文本文件进行排序 - Sorting a text file according to first column in Python 根据列对文件排序并获取uniq元素 - sorting file according to a column and getting uniq elements Python:根据副本上的结果顺序对列表进行排序 - Python: sorting a list according to the resulting order on its copy 如何在 yaml 文件中提及 Mac 主文件夹 - How to mention Mac Home folder in yaml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM