forked from Nota-NetsPresso/BK-SDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_pokemon_text_dir.py
73 lines (56 loc) · 2.24 KB
/
create_pokemon_text_dir.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import pandas as pd
from shutil import copyfile
from sklearn.model_selection import train_test_split
from tqdm import tqdm
from PIL import Image
# Read the CSV file into a DataFrame
df = pd.read_csv("pokemon_text.csv")
# Split the data into training and testing datasets (10% for testing)
train_df, test_df = train_test_split(df, test_size = 0.1, random_state = 7)
# Directories for train and test datasets
train_dir = "train"
test_dir = "test"
# Create the directories if they don't exist
os.makedirs(train_dir, exist_ok=True)
os.makedirs(test_dir, exist_ok=True)
def process_data(data, directory):
file_names = []
texts = []
# Iterate through each row in the DataFrame
for index, row in tqdm(data.iterrows(), total=data.shape[0]):
image_path = row['Image Path']
description = row['Description']
try:
img = Image.open(image_path)
except Exception as e:
print(f"An error occurred: {str(e)}")
print(f"Image file error-prone: {image_path}")
continue
file_name = f"{index}.jpg"
file_names.append(file_name)
texts.append(description)
# Define paths
source_image_path = image_path
destination_image_path = os.path.join(directory, file_name)
description_path = os.path.join(directory, f"{index}.txt")
# Copy the image to the destination directory
try:
copyfile(source_image_path, destination_image_path)
except FileNotFoundError:
print(f"Image file not found: {source_image_path}")
# Write the description to a text file
with open(description_path, 'w') as f:
f.write(description)
return pd.DataFrame({
'file_name': file_names,
'text': texts
})
# Process train and test datasets
train_metadata = process_data(train_df, train_dir)
test_metadata = process_data(test_df, test_dir)
# Save the DataFrames to CSV files
train_metadata.to_csv(os.path.join(train_dir, 'metadata.csv'), index=False)
test_metadata.to_csv(os.path.join(test_dir, 'metadata.csv'), index=False)
print(f"Train data size: {len(train_metadata)}\nTest data size: {len(test_metadata)}")
print("Training and testing files have been processed and saved.")