sprite-animation / reorganize_sprite_dataset.py
Loacky's picture
change dataset names and create sprite_metadata.json
45b4835
raw
history blame
2.1 kB
import os
from datasets import Dataset, DatasetDict, Image, Features, Value
import glob
# Define the path to your dataset (where the folders like 0_frames, 1_frames, etc., are located)
dataset_path = "/Users/lorenzo/Documents/GitHub/sprite-animation/train" # Replace with the actual path
# Define the features for the dataset
features = Features({
"image": Image(),
"label": Value("string"), # The folder name (e.g., "12_frames")
"sprite_id": Value("string"), # The sprite ID (e.g., "12")
})
# Initialize lists to hold the consolidated data
images = []
labels = []
sprite_ids = []
# Iterate over each folder (0_frames, 1_frames, etc.)
for folder_name in os.listdir(dataset_path):
folder_path = os.path.join(dataset_path, folder_name)
# Skip non-directory files and hidden directories (e.g., .git)
if not os.path.isdir(folder_path) or folder_name.startswith("."):
continue
print(f"Processing folder: {folder_name}") # Debug: Print folder being processed
# Extract the sprite ID from the folder name (e.g., "12" from "12_frames")
sprite_id = folder_name.split("_")[0]
# Load all images in the folder
image_paths = glob.glob(os.path.join(folder_path, "sprite_*.png"))
print(f"Found {len(image_paths)} images in folder '{folder_name}'") # Debug: Print number of images found
for image_path in image_paths:
# Append data to the consolidated lists
images.append(image_path)
labels.append(folder_name) # Use the folder name as the label
sprite_ids.append(sprite_id) # Use the sprite ID as an additional field
# Create a single dataset with all the data
dataset = Dataset.from_dict(
{
"image": images,
"label": labels,
"sprite_id": sprite_ids,
},
features=features
)
# Create a DatasetDict with a single split (e.g., "train")
final_dataset = DatasetDict({"train": dataset})
# Push the dataset to Hugging Face
final_dataset.push_to_hub("Lod34/sprite-animation", private=False) # Set private=True if you want it private
print("Dataset successfully uploaded!")