File size: 13,090 Bytes
061439f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import argparse
from pathlib import Path

import cv2
import mediapipe as mp
import numpy as np
import pandas as pd
from tqdm import tqdm


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataset-root", default="data/raw/real-time-exercise-recognition-dataset")
    parser.add_argument("--input-datasets", nargs="+", default=["final_kaggle_with_additional_video", "synthetic_dataset/synthetic_dataset", "similar_dataset"])
    parser.add_argument("--output-dir", default="data/interim")
    parser.add_argument("--min-visibility", type=float, default=0.5)
    return parser.parse_args()

# Build a mapping of landmark names to their corresponding MediaPipe indices
def build_landmark_indices(mp_pose):
    return {
        "LEFT_SHOULDER": mp_pose.PoseLandmark.LEFT_SHOULDER.value,
        "RIGHT_SHOULDER": mp_pose.PoseLandmark.RIGHT_SHOULDER.value,
        "LEFT_HIP": mp_pose.PoseLandmark.LEFT_HIP.value,
        "RIGHT_HIP": mp_pose.PoseLandmark.RIGHT_HIP.value,
        "LEFT_KNEE": mp_pose.PoseLandmark.LEFT_KNEE.value,
        "RIGHT_KNEE": mp_pose.PoseLandmark.RIGHT_KNEE.value,
        "LEFT_ELBOW": mp_pose.PoseLandmark.LEFT_ELBOW.value,
        "RIGHT_ELBOW": mp_pose.PoseLandmark.RIGHT_ELBOW.value,
        "LEFT_WRIST": mp_pose.PoseLandmark.LEFT_WRIST.value,
        "RIGHT_WRIST": mp_pose.PoseLandmark.RIGHT_WRIST.value,
        "LEFT_ANKLE": mp_pose.PoseLandmark.LEFT_ANKLE.value,
        "RIGHT_ANKLE": mp_pose.PoseLandmark.RIGHT_ANKLE.value,
        "LEFT_HEEL": mp_pose.PoseLandmark.LEFT_HEEL.value,
        "RIGHT_HEEL": mp_pose.PoseLandmark.RIGHT_HEEL.value,
        "LEFT_FOOT_INDEX": mp_pose.PoseLandmark.LEFT_FOOT_INDEX.value,
        "RIGHT_FOOT_INDEX": mp_pose.PoseLandmark.RIGHT_FOOT_INDEX.value,
        "LEFT_PINKY": mp_pose.PoseLandmark.LEFT_PINKY.value,
        "RIGHT_PINKY": mp_pose.PoseLandmark.RIGHT_PINKY.value,
        "LEFT_INDEX": mp_pose.PoseLandmark.LEFT_INDEX.value,
        "RIGHT_INDEX": mp_pose.PoseLandmark.RIGHT_INDEX.value,
        "LEFT_THUMB": mp_pose.PoseLandmark.LEFT_THUMB.value,
        "RIGHT_THUMB": mp_pose.PoseLandmark.RIGHT_THUMB.value,
    }

# Define the list of landmark names we want to extract coordinates for
def get_coordinate_landmark_names():
    return [
        "LEFT_SHOULDER",
        "RIGHT_SHOULDER",
        "LEFT_HIP",
        "RIGHT_HIP",
        "LEFT_KNEE",
        "RIGHT_KNEE",
        "LEFT_ELBOW",
        "RIGHT_ELBOW",
        "LEFT_WRIST",
        "RIGHT_WRIST",
        "LEFT_ANKLE",
        "RIGHT_ANKLE",
        "LEFT_HEEL",
        "RIGHT_HEEL",
        "LEFT_FOOT_INDEX",
        "RIGHT_FOOT_INDEX",
        "LEFT_PINKY",
        "RIGHT_PINKY",
        "LEFT_INDEX",
        "RIGHT_INDEX",
        "LEFT_THUMB",
        "RIGHT_THUMB",
    ]

# Define the triplets of landmarks for which we want to calculate joint angles
def get_angle_triplets():
    return [
        ("LEFT_HIP", "LEFT_SHOULDER", "LEFT_ELBOW"),
        ("RIGHT_HIP", "RIGHT_SHOULDER", "RIGHT_ELBOW"),
        ("LEFT_SHOULDER", "LEFT_ELBOW", "LEFT_WRIST"),
        ("RIGHT_SHOULDER", "RIGHT_ELBOW", "RIGHT_WRIST"),
        ("LEFT_HIP", "LEFT_KNEE", "LEFT_ANKLE"),
        ("RIGHT_HIP", "RIGHT_KNEE", "RIGHT_ANKLE"),
        ("LEFT_SHOULDER", "LEFT_HIP", "LEFT_KNEE"),
        ("RIGHT_SHOULDER", "RIGHT_HIP", "RIGHT_KNEE"),
        ("LEFT_KNEE", "LEFT_ANKLE", "LEFT_HEEL"),
        ("RIGHT_KNEE", "RIGHT_ANKLE", "RIGHT_HEEL"),
        ("LEFT_ANKLE", "LEFT_HEEL", "LEFT_FOOT_INDEX"),
        ("RIGHT_ANKLE", "RIGHT_HEEL", "RIGHT_FOOT_INDEX"),
    ]

# function to recursively list video files in a dataset folder, filtering by video file extensions
def list_video_files(dataset_path):
    allowed_suffixes = {".mp4", ".avi", ".mov", ".m4v", ".asf", ".MOV"}
    video_paths = []
    for file_path in dataset_path.rglob("*"):
        if file_path.is_file() and file_path.suffix in allowed_suffixes:
            video_paths.append(file_path)
    return sorted(video_paths)

# normalise exercise labels by mapping known variations to a standard label, and lowercasing/stripping whitespace for consistency
def normalize_exercise_label(raw_label):
    lower_label = raw_label.strip().lower()
    label_mapping = {
        "hammer curl": "barbell biceps curl",
        "bicept curl": "barbell biceps curl",
    }
    return label_mapping.get(lower_label, lower_label)

# Check if a MediaPipe landmark is valid based on its visibility score compared to a minimum threshold
def is_landmark_valid(landmark, min_visibility):
    return landmark.visibility >= min_visibility

# Return a placeholder point (0, 0, 0) for missing or invalid landmarks to maintain consistent feature dimensions
def get_placeholder_point():
    return np.array([0.0, 0.0, 0.0], dtype=np.float32)

# Calculate the angle in degrees between three points (A, B, C) where B is the vertex point. If any point is invalid (all zeros), return 0 degrees.
def calculate_angle_degrees(point_a, point_b, point_c):
    if np.allclose(point_a, 0.0) or np.allclose(point_b, 0.0) or np.allclose(point_c, 0.0):
        return 0.0

    vector_ab = point_a[:2] - point_b[:2]
    vector_cb = point_c[:2] - point_b[:2]
    denominator = np.linalg.norm(vector_ab) * np.linalg.norm(vector_cb)

    if denominator == 0.0:
        return 0.0

    cosine_value = np.dot(vector_ab, vector_cb) / denominator 
    cosine_value = np.clip(cosine_value, -1.0, 1.0) # Clip cosine value to the valid range to avoid numerical issues with arccos
    angle_radians = np.arccos(cosine_value) # Calculate angle in radians and convert to degrees
    return float(np.degrees(angle_radians))

# Extract the specified landmarks from a MediaPipe pose estimation result for a single frame, checking visibility and using placeholders for missing landmarks. Returns a dictionary of landmark names to their (x, y, z) coordinates.
def extract_frame_landmarks(media_pipe_results, landmark_indices, coordinate_landmark_names, min_visibility):
    extracted_landmarks = {}
    if not media_pipe_results.pose_landmarks:
        return extracted_landmarks

    for landmark_name in coordinate_landmark_names:
        landmark_index = landmark_indices[landmark_name]
        detected_landmark = media_pipe_results.pose_landmarks.landmark[landmark_index]

        if is_landmark_valid(detected_landmark, min_visibility):
            extracted_landmarks[landmark_name] = np.array(
                [detected_landmark.x, detected_landmark.y, detected_landmark.z],
                dtype=np.float32,
            )
        else:
            extracted_landmarks[landmark_name] = get_placeholder_point()

    return extracted_landmarks

# Check if all essential landmarks for a given body side (LEFT or RIGHT) are valid (not all zeros) to determine if we can trust the pose estimation for that side. This helps filter out frames where the pose estimation failed for one side of the body.
def has_valid_body_side(extracted_landmarks, side_prefix):
    essential_points = ["SHOULDER", "ELBOW", "WRIST", "HIP", "KNEE", "ANKLE"]
    for point_name in essential_points:
        full_landmark_name = f"{side_prefix}_{point_name}"
        if np.allclose(extracted_landmarks[full_landmark_name], 0.0):
            return False
    return True

# Build a feature row dictionary for a single frame, including the video identifier, exercise label, frame index, landmark coordinates, and calculated angles based on the specified triplets. This function combines all the extracted information into a structured format for later saving to CSV.
def build_feature_row(
    extracted_landmarks,
    coordinate_landmark_names,
    angle_triplets,
    frame_index,
    video_identifier,
    exercise_label,
):
    feature_row = {
        "video_id": video_identifier,
        "exercise_label": exercise_label,
        "frame_index": frame_index,
    }

    for landmark_name in coordinate_landmark_names:
        landmark_value = extracted_landmarks[landmark_name]
        feature_row[f"{landmark_name.lower()}_x"] = landmark_value[0]
        feature_row[f"{landmark_name.lower()}_y"] = landmark_value[1]
        feature_row[f"{landmark_name.lower()}_z"] = landmark_value[2]

    for point_a, point_b, point_c in angle_triplets:
        angle_name = f"angle_{point_a.lower()}_{point_b.lower()}_{point_c.lower()}"
        feature_row[angle_name] = calculate_angle_degrees(
            extracted_landmarks[point_a],
            extracted_landmarks[point_b],
            extracted_landmarks[point_c],
        )

    return feature_row

# Process a single video file to extract per-frame features. For each frame, it runs pose estimation, extracts landmarks, checks validity, and builds feature rows for valid frames. It returns a list of feature rows for the entire video.
def extract_features_from_video(
    video_path,
    exercise_label,
    dataset_name,
    pose_estimator,
    landmark_indices,
    coordinate_landmark_names,
    angle_triplets,
    min_visibility,
):
    video_capture = cv2.VideoCapture(str(video_path))
    frame_index = 0
    extracted_rows = []
    video_identifier = f"{dataset_name}/{exercise_label}/{video_path.stem}"

    while video_capture.isOpened():
        frame_read_success, frame_bgr = video_capture.read()
        if not frame_read_success:
            break

        frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
        pose_results = pose_estimator.process(frame_rgb)
        extracted_landmarks = extract_frame_landmarks(
            pose_results,
            landmark_indices,
            coordinate_landmark_names,
            min_visibility,
        )

        if extracted_landmarks:
            left_side_is_valid = has_valid_body_side(extracted_landmarks, "LEFT")
            right_side_is_valid = has_valid_body_side(extracted_landmarks, "RIGHT")

            if left_side_is_valid or right_side_is_valid:
                extracted_rows.append(
                    build_feature_row(
                        extracted_landmarks=extracted_landmarks,
                        coordinate_landmark_names=coordinate_landmark_names,
                        angle_triplets=angle_triplets,
                        frame_index=frame_index,
                        video_identifier=video_identifier,
                        exercise_label=exercise_label,
                    )
                )

        frame_index += 1

    video_capture.release()
    return extracted_rows

# Save the list of feature row dictionaries to a CSV file using pandas, ensuring the output directory exists. Each row in the CSV corresponds to a single frame's extracted features.
def save_rows_to_csv(rows, output_file_path):
    output_file_path.parent.mkdir(parents=True, exist_ok=True)
    dataframe = pd.DataFrame(rows)
    dataframe.to_csv(output_file_path, index=False)

# Sets up the main execution flow: parses arguments, initializes MediaPipe pose estimator, iterates through specified datasets and videos, extracts features for each video, and saves the results to CSV files in the output directory.
def main():
    args = parse_args()

    dataset_root_path = Path(args.dataset_root)
    input_dataset_names = args.input_datasets
    output_directory_path = Path(args.output_dir)
    minimum_landmark_visibility = args.min_visibility

    media_pipe_pose = mp.solutions.pose
    landmark_indices = build_landmark_indices(media_pipe_pose)
    coordinate_landmark_names = get_coordinate_landmark_names()
    angle_triplets = get_angle_triplets()

    with media_pipe_pose.Pose(
        static_image_mode=False,
        model_complexity=1,
        enable_segmentation=False,
        min_detection_confidence=0.5,
        min_tracking_confidence=0.5,
    ) as pose_estimator:
        for dataset_name in input_dataset_names:
            dataset_path = dataset_root_path / dataset_name
            if not dataset_path.exists():
                print(f"Skipping missing dataset folder: {dataset_path}")
                continue

            print(f"Processing dataset: {dataset_name}")
            dataset_rows = []
            video_paths = list_video_files(dataset_path)

            for video_path in tqdm(video_paths, desc=f"Videos in {dataset_name}"):
                exercise_label = normalize_exercise_label(video_path.parent.name)
                video_rows = extract_features_from_video(
                    video_path=video_path,
                    exercise_label=exercise_label,
                    dataset_name=dataset_name,
                    pose_estimator=pose_estimator,
                    landmark_indices=landmark_indices,
                    coordinate_landmark_names=coordinate_landmark_names,
                    angle_triplets=angle_triplets,
                    min_visibility=minimum_landmark_visibility,
                )
                dataset_rows.extend(video_rows)

            output_file_path = output_directory_path / f"{dataset_name.replace('/', '_')}_frame_features.csv"
            save_rows_to_csv(dataset_rows, output_file_path)
            print(f"Saved: {output_file_path} ({len(dataset_rows)} rows)")


if __name__ == "__main__":
    main()