File size: 1,235 Bytes
a63811d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os

# Path to the folder containing the images and text files
folder_path = "kohya_finetune_data"

# List of valid image extensions
image_extensions = ('.png', '.jpg', '.jpeg')

# Get lists of image and text files in the folder
image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(image_extensions)]
text_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')]

# Create sets of base filenames without extensions
image_basenames = {os.path.splitext(f)[0] for f in image_files}
text_basenames = {os.path.splitext(f)[0] for f in text_files}

# Check for image files without corresponding text files
for image_file in image_files:
    base_name = os.path.splitext(image_file)[0]
    if base_name not in text_basenames:
        os.remove(os.path.join(folder_path, image_file))
        print(f'Removed image file without corresponding text file: {image_file}')

# Check for text files without corresponding image files
for text_file in text_files:
    base_name = os.path.splitext(text_file)[0]
    if base_name not in image_basenames:
        os.remove(os.path.join(folder_path, text_file))
        print(f'Removed text file without corresponding image file: {text_file}')