Datasets:
Tasks:
Text Classification
Modalities:
Image
Formats:
imagefolder
Languages:
English
Size:
1K - 10K
Tags:
art
License:
| import json | |
| import os | |
| def load_metadata(): | |
| """Load metadata from metadata.jsonl""" | |
| metadata_path = "metadata.jsonl" | |
| metadata = {} | |
| if not os.path.exists(metadata_path): | |
| print(f"Error: '{metadata_path}' file not found!") | |
| return metadata | |
| # Parse JSONL file and map descriptions by sprite ID | |
| try: | |
| with open(metadata_path, 'r') as f: | |
| for line in f: | |
| if line.strip(): | |
| entry = json.loads(line) | |
| # Extract sprite ID from file_name (e.g., "23.png" -> "23") | |
| sprite_id = entry["file_name"].split(".")[0] | |
| metadata[sprite_id] = entry["text"] | |
| except Exception as e: | |
| print(f"Error reading metadata file: {str(e)}") | |
| print(f"Loaded {len(metadata)} entries from metadata file") | |
| return metadata | |
| def extract_info_safely(description): | |
| """Extract information from description with error handling""" | |
| info = { | |
| "frames": "unknown", | |
| "action": "unknown", | |
| "direction": "unknown", | |
| "character": "unknown" | |
| } | |
| try: | |
| # Standard format: "X-frame sprite animation of: CHARACTER, that: ACTION, facing: DIRECTION" | |
| if "-frame sprite animation of:" in description: | |
| # Extract frame count | |
| info["frames"] = description.split("-frame")[0].strip() | |
| # Extract character | |
| if "of:" in description and ", that:" in description: | |
| character_part = description.split("of:")[1].split(", that:")[0].strip() | |
| info["character"] = character_part | |
| # Extract action | |
| if ", that:" in description and ", facing:" in description: | |
| action_part = description.split(", that:")[1].split(", facing:")[0].strip() | |
| info["action"] = action_part | |
| elif ", that:" in description: | |
| # Handle case where facing might not be present | |
| action_part = description.split(", that:")[1].strip() | |
| info["action"] = action_part | |
| # Extract direction | |
| if ", facing:" in description: | |
| direction_part = description.split(", facing:")[1].strip() | |
| info["direction"] = direction_part | |
| print(f"Processed: '{description[:30]}...' -> frames: {info['frames']}") | |
| except Exception as e: | |
| print(f"Warning: Error parsing description: '{description}'") | |
| print(f"Error details: {str(e)}") | |
| return info | |
| def create_sprite_dataset(): | |
| """Create sprite dataset with metadata""" | |
| # Load metadata | |
| metadata = load_metadata() | |
| if not metadata: | |
| print("No metadata entries found. Check if metadata.jsonl exists and has content.") | |
| return {} | |
| # Create output directory structure | |
| base_dir = "sprite_dataset" | |
| if not os.path.exists(base_dir): | |
| os.makedirs(base_dir) | |
| # Create metadata mapping | |
| sprite_info = {} | |
| # Check if train directory exists | |
| train_dir = "train" | |
| if not os.path.exists(train_dir): | |
| print(f"Error: '{train_dir}' directory not found!") | |
| return sprite_info | |
| # Process each folder in the train directory - looking for spritesheet_X folders | |
| frame_folders = [f for f in os.listdir(train_dir) if f.startswith("spritesheet_") and os.path.isdir(os.path.join(train_dir, f))] | |
| if not frame_folders: | |
| print(f"Warning: No folders starting with 'spritesheet_' found in '{train_dir}'") | |
| else: | |
| print(f"Found {len(frame_folders)} spritesheet folders to process") | |
| for folder_name in frame_folders: | |
| # Extract sprite ID from folder name (e.g., "spritesheet_23" -> "23") | |
| sprite_id = folder_name.split("_")[1] | |
| # Get description from metadata | |
| description = metadata.get(sprite_id, "No description available") | |
| # Extract information safely | |
| info = extract_info_safely(description) | |
| # Create organized structure with folder name | |
| sprite_info[sprite_id] = { | |
| "frames": info["frames"], | |
| "action": info["action"], | |
| "direction": info["direction"], | |
| "character": info["character"], | |
| "folder_name": folder_name, | |
| "full_description": description | |
| } | |
| if not sprite_info: | |
| print("Warning: No sprite information was extracted!") | |
| return {} | |
| # Save metadata to JSON file | |
| metadata_path = "sprite_metadata.json" # Save in current directory for easier debugging | |
| with open(metadata_path, "w") as f: | |
| json.dump(sprite_info, f, indent=4) | |
| print(f"Created sprite dataset with metadata for {len(sprite_info)} sprites") | |
| return sprite_info | |
| if __name__ == "__main__": | |
| sprite_info = create_sprite_dataset() |