简体   繁体   中英

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:

- 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. I would like to separate each image according to its label respectively into its folder using 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

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

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

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:

# 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM