File size: 3,055 Bytes
d2885a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from pathlib import Path


def parse_pose_data_filename(file_name: str, extension: str | None = None) -> dict:
    """
    Parse your posture dataset file naming convention.

    Expected style:
    20241114_1643_hyz_N_TC-Head_000_012.npy

    Extracted fields:
    - capture_date
    - capture_time
    - human_model_name
    - label
    - extensions
    - weight
    - frame_number
    """
    if extension is not None:
        file_name = file_name.replace(extension, "")

    parts = re.split(r"_", file_name)

    if len(parts) not in range(6, 8):
        raise ValueError(f"Invalid posture filename format: {file_name}")

    return {
        "capture_date": int(parts[0]),
        "capture_time": int(parts[1]),
        "human_model_name": str(parts[2]),
        "label": str(parts[3]),
        "extensions": str(parts[4]),
        "weight": float(parts[5][:1] + "." + parts[5][1:]),
        "frame_number": int(parts[6]) if len(parts) > 6 else None,
    }


def get_video_properties(video_name: str, item: str | None = None) -> dict | int | str:
    """
    Parse your phone-data video naming convention.

    Expected style without extension:
    20250512_1427_model_position_greeninfo_Lphone

    Your current project uses:
    - date
    - time
    - model name
    - position
    - color info
    - hand info

    Returns full dict or one selected item.
    """
    if video_name.endswith(".mp4"):
        raise ValueError("video_name must not include file extension")

    values = tuple(video_name.split("_"))
    keys = ["date", "time", "model name", "position", "color info", "hand info"]

    if len(values) < len(keys):
        raise ValueError(f"Invalid video filename format: {video_name}")

    properties = {key: value for key, value in zip(keys, values)}

    hand_prefix = properties["hand info"][0]
    if hand_prefix not in {"L", "R", "B"}:
        raise ValueError(f"Invalid hand code in video name: {video_name}")

    properties["hand index"] = {"L": 0, "R": 1, "B": 2}[hand_prefix]
    properties["hand item"] = properties["hand info"][1:]
    del properties["hand info"]

    if item is None:
        return properties

    valid_items = list(properties.keys()) + ["hex id"]
    if item not in valid_items:
        raise KeyError(f"Invalid item '{item}'. Supported: {valid_items}")

    if item == "hex id":
        return f"{properties['date'][:8]}_{properties['time']}"

    return properties[item]


def video_name_to_hand_index(video_name: str) -> int:
    """
    Small helper used in your current phone dataset gathering logic.

    Returns:
    - 0 for left hand
    - 1 for right hand
    - 2 for both hands
    """
    properties = tuple(video_name.split("_"))
    hand_prefix = properties[-1][0]

    if hand_prefix not in {"L", "R", "B"}:
        raise ValueError(f"Invalid hand code in video name: {video_name}")

    return {"L": 0, "R": 1, "B": 2}[hand_prefix]


def get_file_stem(path_like: str | Path) -> str:
    """
    Return file name without extension.
    """
    return Path(path_like).stem