| """ |
| Auto-extract L1 action labels from filenames/metadata for datasets without text. |
| |
| Outputs: data/processed/{dataset}/labels.json |
| Format: { "motion_id": {"L1_action": "walk", "L1_style": "happy", "source_file": "..."}, ... } |
| """ |
|
|
| import sys |
| import os |
| import re |
| import json |
| from pathlib import Path |
| import numpy as np |
|
|
| project_root = Path(__file__).parent.parent |
| sys.path.insert(0, str(project_root)) |
|
|
|
|
| def extract_lafan1(): |
| """LAFAN1: filename = {theme}{take}_{subject}.bvh → L1 = theme""" |
| labels = {} |
| mdir = project_root / 'data' / 'processed' / 'lafan1' / 'motions' |
| for f in sorted(os.listdir(mdir)): |
| d = dict(np.load(mdir / f, allow_pickle=True)) |
| src = str(d.get('source_file', f)) |
| |
| match = re.match(r'([a-zA-Z]+)\d*_', src) |
| action = match.group(1).lower() if match else 'unknown' |
| labels[f.replace('.npz', '')] = { |
| 'L1_action': action, |
| 'source_file': src, |
| } |
| return labels |
|
|
|
|
| def extract_100style(): |
| """100Style: filename = {StyleName}_{MovementType}.bvh""" |
| movement_map = { |
| 'FW': 'walk forward', 'BW': 'walk backward', |
| 'FR': 'run forward', 'BR': 'run backward', |
| 'SW': 'sidestep walk', 'SR': 'sidestep run', |
| 'ID': 'idle', |
| 'TR1': 'transition 1', 'TR2': 'transition 2', |
| 'TR3': 'transition 3', 'TR4': 'transition 4', |
| } |
| labels = {} |
| mdir = project_root / 'data' / 'processed' / '100style' / 'motions' |
| for f in sorted(os.listdir(mdir)): |
| d = dict(np.load(mdir / f, allow_pickle=True)) |
| src = str(d.get('source_file', f)) |
| |
| parts = src.replace('.bvh', '').split('_') |
| style = parts[0].lower() if parts else 'unknown' |
| movement_code = parts[1] if len(parts) > 1 else '' |
| movement = movement_map.get(movement_code, movement_code.lower()) |
| action = movement.split()[0] if movement else 'unknown' |
| labels[f.replace('.npz', '')] = { |
| 'L1_action': action, |
| 'L1_movement': movement, |
| 'L1_style': style, |
| 'source_file': src, |
| } |
| return labels |
|
|
|
|
| def extract_bandai_namco(): |
| """Bandai Namco: has JSON metadata in original repo.""" |
| labels = {} |
| mdir = project_root / 'data' / 'processed' / 'bandai_namco' / 'motions' |
| |
| bn_meta = {} |
| meta_dirs = [ |
| project_root / 'data' / 'raw' / 'BandaiNamco' / 'dataset', |
| ] |
| for meta_dir in meta_dirs: |
| for json_file in meta_dir.rglob('*.json'): |
| try: |
| with open(json_file) as jf: |
| data = json.load(jf) |
| if isinstance(data, list): |
| for item in data: |
| if 'file' in item and 'content' in item: |
| bn_meta[item['file']] = item |
| except Exception: |
| continue |
|
|
| for f in sorted(os.listdir(mdir)): |
| d = dict(np.load(mdir / f, allow_pickle=True)) |
| src = str(d.get('source_file', f)) |
| |
| action = 'unknown' |
| style = '' |
| if src in bn_meta: |
| meta = bn_meta[src] |
| action = meta.get('content', 'unknown').lower() |
| style = meta.get('style', '').lower() |
| else: |
| |
| name = src.replace('.bvh', '').lower() |
| |
| for keyword in ['walk', 'run', 'kick', 'punch', 'jump', 'dance', 'idle', 'turn', |
| 'throw', 'catch', 'wave', 'bow', 'sit', 'stand', 'crouch', 'crawl']: |
| if keyword in name: |
| action = keyword |
| break |
| labels[f.replace('.npz', '')] = { |
| 'L1_action': action, |
| 'L1_style': style, |
| 'source_file': src, |
| } |
| return labels |
|
|
|
|
| def extract_cmu_mocap(): |
| """CMU MoCap: directory structure = subject/action.""" |
| labels = {} |
| mdir = project_root / 'data' / 'processed' / 'cmu_mocap' / 'motions' |
| for f in sorted(os.listdir(mdir)): |
| d = dict(np.load(mdir / f, allow_pickle=True)) |
| src = str(d.get('source_file', f)) |
| |
| parts = src.replace('.bvh', '').split('_') |
| subject = parts[0] if parts else '?' |
| seq = parts[1] if len(parts) > 1 else '?' |
| labels[f.replace('.npz', '')] = { |
| 'L1_action': 'motion', |
| 'L1_subject': subject, |
| 'L1_sequence': seq, |
| 'source_file': src, |
| } |
| return labels |
|
|
|
|
| def extract_mixamo(): |
| """Mixamo: hash filenames → no meaningful labels from filename alone.""" |
| labels = {} |
| mdir = project_root / 'data' / 'processed' / 'mixamo' / 'motions' |
|
|
| |
| anim_map = {} |
| anim_json = project_root / 'data' / 'raw' / 'Mixamo' / 'animation_frames.json' |
| if anim_json.exists(): |
| try: |
| with open(anim_json) as jf: |
| data = json.load(jf) |
| for name, info in data.items(): |
| |
| anim_map[name.lower()] = name |
| except Exception: |
| pass |
|
|
| for f in sorted(os.listdir(mdir)): |
| d = dict(np.load(mdir / f, allow_pickle=True)) |
| src = str(d.get('source_file', f)).replace('.bvh', '').replace('.fbx', '') |
|
|
| |
| action = 'motion' |
| labels[f.replace('.npz', '')] = { |
| 'L1_action': action, |
| 'source_file': src, |
| } |
| return labels |
|
|
|
|
| def extract_truebones_zoo(): |
| """Truebones Zoo: already has some text; extract L1 from species + filename.""" |
| labels = {} |
| mdir = project_root / 'data' / 'processed' / 'truebones_zoo' / 'motions' |
| for f in sorted(os.listdir(mdir)): |
| d = dict(np.load(mdir / f, allow_pickle=True)) |
| species = str(d.get('species', '')) |
| src = str(d.get('source_file', f)) |
| texts = str(d.get('texts', '')) |
|
|
| |
| name = src.replace('.bvh', '').strip('_').lower() |
| action = 'motion' |
| for keyword in ['attack', 'walk', 'run', 'idle', 'die', 'death', 'eat', 'bite', |
| 'jump', 'fly', 'swim', 'crawl', 'sleep', 'sit', 'stand', 'turn', |
| 'howl', 'bark', 'roar', 'hit', 'charge', 'gallop', 'trot', 'strike', |
| 'breath', 'wing', 'tail', 'shake', 'scratch', 'pounce', 'retreat']: |
| if keyword in name: |
| action = keyword |
| break |
|
|
| labels[f.replace('.npz', '')] = { |
| 'L1_action': action, |
| 'L1_species': species, |
| 'L1_species_category': _species_category(species), |
| 'has_L2': bool(texts and texts not in ('', "b''")), |
| 'source_file': src, |
| } |
| return labels |
|
|
|
|
| def _species_category(species): |
| """Map species to category.""" |
| quadrupeds = {'Dog', 'Dog-2', 'Cat', 'Horse', 'Bear', 'BrownBear', 'PolarBear', 'PolarBearB', |
| 'Buffalo', 'Camel', 'Coyote', 'Deer', 'Elephant', 'Fox', 'Gazelle', 'Goat', |
| 'Hamster', 'Hippopotamus', 'Hound', 'Jaguar', 'Leapord', 'Lion', 'Lynx', |
| 'Mammoth', 'Monkey', 'Puppy', 'Raindeer', 'Rat', 'Rhino', 'SabreToothTiger', |
| 'SandMouse', 'Skunk'} |
| flying = {'Bat', 'Bird', 'Buzzard', 'Chicken', 'Crow', 'Eagle', 'Flamingo', 'Giantbee', |
| 'Ostrich', 'Parrot', 'Parrot2', 'Pigeon', 'Pteranodon', 'Tukan'} |
| reptile = {'Alligator', 'Comodoa', 'Crocodile', 'Stego', 'Trex', 'Tricera', 'Tyranno'} |
| insect = {'Ant', 'Centipede', 'Cricket', 'FireAnt', 'Isopetra', 'Roach', 'Scorpion', |
| 'Scorpion-2', 'Spider', 'SpiderG'} |
| snake = {'Anaconda', 'KingCobra'} |
| aquatic = {'Crab', 'HermitCrab', 'Jaws', 'Pirrana', 'Turtle'} |
| fantasy = {'Dragon', 'Raptor', 'Raptor2', 'Raptor3'} |
|
|
| if species in quadrupeds: return 'quadruped' |
| if species in flying: return 'flying' |
| if species in reptile: return 'reptile' |
| if species in insect: return 'insect' |
| if species in snake: return 'snake' |
| if species in aquatic: return 'aquatic' |
| if species in fantasy: return 'fantasy' |
| return 'other' |
|
|
|
|
| def main(): |
| extractors = { |
| 'lafan1': extract_lafan1, |
| '100style': extract_100style, |
| 'bandai_namco': extract_bandai_namco, |
| 'cmu_mocap': extract_cmu_mocap, |
| 'mixamo': extract_mixamo, |
| 'truebones_zoo': extract_truebones_zoo, |
| } |
|
|
| for ds, extractor in extractors.items(): |
| labels = extractor() |
| out_path = project_root / 'data' / 'processed' / ds / 'labels.json' |
| with open(out_path, 'w') as f: |
| json.dump(labels, f, indent=2, ensure_ascii=False) |
|
|
| |
| actions = [v.get('L1_action', '?') for v in labels.values()] |
| from collections import Counter |
| top = Counter(actions).most_common(5) |
| print(f'{ds:15s}: {len(labels)} labels, top actions: {top}') |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|