File size: 4,913 Bytes
3828719
 
 
 
 
 
 
 
45b4835
 
 
 
3828719
45b4835
 
 
 
 
 
 
 
 
 
 
 
3828719
 
 
 
 
 
 
 
 
 
 
 
45b4835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3828719
45b4835
 
 
 
 
 
3828719
45b4835
3828719
 
 
 
 
 
 
 
45b4835
 
 
3828719
 
 
 
 
 
 
 
 
45b4835
3828719
45b4835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3828719
 
45b4835
3828719
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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()