File size: 14,468 Bytes
55a92f5
061439f
55a92f5
061439f
 
 
 
55a92f5
061439f
 
 
 
 
 
55a92f5
061439f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55a92f5
 
061439f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55a92f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac532c9
55a92f5
 
061439f
 
 
 
 
 
55a92f5
 
061439f
55a92f5
061439f
55a92f5
 
 
 
 
 
 
 
 
 
061439f
55a92f5
061439f
55a92f5
 
061439f
 
55a92f5
 
 
 
 
 
061439f
 
 
 
 
 
 
 
 
55a92f5
 
 
 
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import os
import sys
import threading
import time
from pathlib import Path
from types import SimpleNamespace

import av
import cv2
import joblib
import mediapipe as mp
import numpy as np
import streamlit as st
import torch
from streamlit_webrtc import VideoProcessorBase, webrtc_streamer

PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from scripts.evaluate.rep_counting_methods import EXERCISE_CONFIGS, FixedThresholdFSMCounter, SmoothingBuffer, extract_primary_angle, normalize_exercise_name
from scripts.realtime_eval.evaluate_realtime_webcam import (
    MODEL_SPECS,
    build_landmark_indices,
    build_model_and_tools,
    extract_frame_features,
    get_angle_triplets,
    load_pose_module,
)


SEQUENCE_LENGTH = 30
FEATURE_COUNT = 78
DEFAULT_MODELS_ROOT = "models"
DEFAULT_PREDICTION_INTERVAL = 1.0
CAMERA_INDEX_CANDIDATES = [0, 1, 2]
RTC_CONFIGURATION = {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
BROWSER_MEDIA_CONSTRAINTS = {"video": {"width": {"ideal": 1280}, "height": {"ideal": 720}, "frameRate": {"ideal": 24, "max": 30}}, "audio": False}


def load_runtime(model_name: str, models_root: str, feature_count: int):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    args = SimpleNamespace(model_name=model_name, models_root=models_root, feature_count=feature_count)
    model, scaler, label_encoder = build_model_and_tools(args, device)
    return device, model, scaler, label_encoder


def load_similarity_asset(model_name: str, models_root: str):
    asset_path = Path(models_root) / model_name / "weights" / "similarity_centroids.pkl"
    if not asset_path.exists():
        return None
    return joblib.load(asset_path)


def cosine_similarity_percent(vector_a: np.ndarray, vector_b: np.ndarray) -> float:
    denom = float(np.linalg.norm(vector_a) * np.linalg.norm(vector_b))
    if denom <= 1e-8:
        return 0.0
    score = float(np.dot(vector_a, vector_b) / denom)
    score = max(-1.0, min(1.0, score))
    return ((score + 1.0) / 2.0) * 100.0


def read_valid_frame(capture: cv2.VideoCapture, max_reads: int = 20) -> np.ndarray | None:
    frame_bgr = None
    for _ in range(max_reads):
        ok, candidate = capture.read()
        if not ok:
            continue
        if float(np.mean(candidate)) > 5.0:
            return candidate
        frame_bgr = candidate
    if frame_bgr is not None and float(np.mean(frame_bgr)) > 5.0:
        return frame_bgr
    return None


def open_camera_with_fallback() -> cv2.VideoCapture | None:
    for camera_index in CAMERA_INDEX_CANDIDATES:
        capture = cv2.VideoCapture(camera_index)
        if not capture.isOpened():
            capture.release()
            continue
        frame_bgr = read_valid_frame(capture, max_reads=10)
        if frame_bgr is not None:
            return capture
        capture.release()
    return None


def create_runtime_state():
    return {
        "counter": None,
        "smoother": None,
        "active_exercise": None,
        "current_label": "none",
        "current_similarity": None,
        "current_reps": 0,
        "last_prediction_time": 0.0,
        "window": [],
    }


def process_single_frame(frame_bgr: np.ndarray, state: dict, model, scaler, label_encoder, device, pose_estimator, pose_module, landmark_indices, angle_triplets, similarity_asset, prediction_interval: float):
    frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
    pose = pose_estimator.process(frame_rgb)

    drawing_utils = mp.solutions.drawing_utils
    if drawing_utils is not None and pose.pose_landmarks:
        drawing_utils.draw_landmarks(frame_bgr, pose.pose_landmarks, pose_module.POSE_CONNECTIONS)

    frame_features = extract_frame_features(pose, landmark_indices, angle_triplets)
    if frame_features is not None:
        state["window"].append(frame_features)
        if len(state["window"]) > SEQUENCE_LENGTH:
            state["window"].pop(0)

    if len(state["window"]) == SEQUENCE_LENGTH and (time.time() - state["last_prediction_time"]) >= prediction_interval:
        now = time.time()
        sequence_flat = np.array(state["window"], dtype=np.float32).reshape(1, -1)
        scaled_flat = scaler.transform(sequence_flat)
        scaled = scaled_flat.reshape(1, SEQUENCE_LENGTH, FEATURE_COUNT)
        input_tensor = torch.tensor(scaled, dtype=torch.float32, device=device)
        with torch.inference_mode():
            logits = model(input_tensor)
            prediction_index = int(torch.argmax(logits, dim=1).item())
            predicted_label = label_encoder.classes_[prediction_index]
        state["current_label"] = predicted_label

        if similarity_asset is not None:
            scaled_vector = scaled_flat[0]
            centroids = similarity_asset.get("centroids", {})
            centroid_vector = centroids.get(state["current_label"])
            if centroid_vector is not None:
                state["current_similarity"] = cosine_similarity_percent(scaled_vector.astype(np.float32), np.asarray(centroid_vector, dtype=np.float32))
            else:
                state["current_similarity"] = None
        state["last_prediction_time"] = now

    normalized_label = normalize_exercise_name(state["current_label"])
    current_reps = 0
    if pose.pose_landmarks and normalized_label in EXERCISE_CONFIGS:
        if normalized_label != state["active_exercise"]:
            config = EXERCISE_CONFIGS[normalized_label]
            state["counter"] = FixedThresholdFSMCounter(config.fixed_low, config.fixed_high, config.min_state_frames)
            state["smoother"] = SmoothingBuffer(config.smoothing_window)
            state["active_exercise"] = normalized_label

        landmarks = {}
        for name, index in landmark_indices.items():
            lm = pose.pose_landmarks.landmark[index]
            landmarks[name] = np.array([lm.x, lm.y, lm.z], dtype=np.float32) if lm.visibility >= 0.5 else np.array([0.0, 0.0, 0.0], dtype=np.float32)

        config = EXERCISE_CONFIGS[normalized_label]
        raw_angle = extract_primary_angle(landmarks, config)
        smoothed_angle = state["smoother"].update(raw_angle)
        state["counter"].update(smoothed_angle)
        current_reps = state["counter"].reps
    else:
        state["active_exercise"] = None
        state["counter"] = None
        state["smoother"] = None

    state["current_reps"] = current_reps
    show_frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
    return show_frame, state


class MotionVideoProcessor(VideoProcessorBase):
    def __init__(self):
        self.lock = threading.Lock()
        self.initialized = False
        self.state = create_runtime_state()

    def configure(self, _model_name, model, scaler, label_encoder, device, pose_module, landmark_indices, angle_triplets, similarity_asset, prediction_interval):
        self.model = model
        self.scaler = scaler
        self.label_encoder = label_encoder
        self.device = device
        self.pose_module = pose_module
        self.landmark_indices = landmark_indices
        self.angle_triplets = angle_triplets
        self.similarity_asset = similarity_asset
        self.prediction_interval = prediction_interval
        self.pose_estimator = pose_module.Pose(static_image_mode=False, model_complexity=1, min_detection_confidence=0.5, min_tracking_confidence=0.5)
        self.initialized = True

    def recv(self, frame):
        frame_bgr = frame.to_ndarray(format="bgr24")
        with self.lock:
            if self.initialized:
                frame_rgb, self.state = process_single_frame(
                    frame_bgr=frame_bgr,
                    state=self.state,
                    model=self.model,
                    scaler=self.scaler,
                    label_encoder=self.label_encoder,
                    device=self.device,
                    pose_estimator=self.pose_estimator,
                    pose_module=self.pose_module,
                    landmark_indices=self.landmark_indices,
                    angle_triplets=self.angle_triplets,
                    similarity_asset=self.similarity_asset,
                    prediction_interval=self.prediction_interval,
                )
            else:
                frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
        return av.VideoFrame.from_ndarray(frame_rgb, format="rgb24")

    def __del__(self):
        if hasattr(self, "pose_estimator"):
            self.pose_estimator.close()


def render_metrics(model_name: str, state: dict, slot):
    similarity_text = f"{state['current_similarity']:0.1f}%" if state["current_similarity"] is not None else "N/A"
    slot.markdown(f"### Live Metrics\nModel: `{model_name}`\n\nExercise: `{state['current_label']}`\n\nReps: `{state['current_reps']}`\n\nSimilarity: `{similarity_text}`")


def run_local_session(model_name: str, model, scaler, label_encoder, device, pose_module, landmark_indices, angle_triplets, similarity_asset):
    capture = open_camera_with_fallback()
    if capture is None:
        st.error("No camera device found in runtime. If using Docker locally, run with --device=/dev/video0:/dev/video0. On Hugging Face Spaces, use Browser (HF/Cloud) mode.")
        st.session_state.session_active = False
        return

    left_col, right_col = st.columns([2, 1])
    with left_col:
        frame_slot = st.empty()
    with right_col:
        metrics_slot = st.empty()

    state = create_runtime_state()
    with pose_module.Pose(static_image_mode=False, model_complexity=1, min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose_estimator:
        while True:
            ok, frame_bgr = capture.read()
            if not ok:
                break
            show_frame, state = process_single_frame(
                frame_bgr=frame_bgr,
                state=state,
                model=model,
                scaler=scaler,
                label_encoder=label_encoder,
                device=device,
                pose_estimator=pose_estimator,
                pose_module=pose_module,
                landmark_indices=landmark_indices,
                angle_triplets=angle_triplets,
                similarity_asset=similarity_asset,
                prediction_interval=DEFAULT_PREDICTION_INTERVAL,
            )
            frame_slot.image(show_frame, channels="RGB", width="stretch")
            render_metrics(model_name, state, metrics_slot)
            if not st.session_state.session_active:
                break

    capture.release()
    if st.session_state.session_active:
        st.session_state.session_active = False
        st.session_state.session_notice = "finished"
        st.rerun()


def run_browser_session(model_name: str, model, scaler, label_encoder, device, pose_module, landmark_indices, angle_triplets, similarity_asset):
    left_col, right_col = st.columns([2, 1])
    with left_col:
        webrtc_ctx = webrtc_streamer(
            key="motionbench-webrtc",
            video_processor_factory=MotionVideoProcessor,
            rtc_configuration=RTC_CONFIGURATION,
            media_stream_constraints=BROWSER_MEDIA_CONSTRAINTS,
            async_processing=True,
        )
    with right_col:
        metrics_slot = st.empty()

    if webrtc_ctx.video_processor:
        video_processor = webrtc_ctx.video_processor
        if not video_processor.initialized:
            video_processor.configure(model_name, model, scaler, label_encoder, device, pose_module, landmark_indices, angle_triplets, similarity_asset, DEFAULT_PREDICTION_INTERVAL)
        with video_processor.lock:
            state_copy = {
                "current_label": video_processor.state["current_label"],
                "current_reps": video_processor.state["current_reps"],
                "current_similarity": video_processor.state["current_similarity"],
            }
        render_metrics(model_name, state_copy, metrics_slot)
    else:
        metrics_slot.info("Allow camera access and click START in the video panel. If needed, choose your camera under Video Input.")


def main():
    st.set_page_config(page_title="MotionBench", layout="wide")
    st.title("MotionBench Live")

    if "session_active" not in st.session_state:
        st.session_state.session_active = False
    if "session_notice" not in st.session_state:
        st.session_state.session_notice = None

    is_hf_space = bool(os.getenv("SPACE_ID"))
    model_name = st.selectbox("Select Model", options=list(MODEL_SPECS.keys()), index=0)
    camera_options = ["Browser (HF/Cloud)", "Local OpenCV (Desktop)"]
    default_camera_index = 0 if is_hf_space else 1
    camera_source = st.selectbox("Camera Source", options=camera_options, index=default_camera_index)
    session_button_label = "Stop Session" if st.session_state.session_active else "Start Session"
    session_button_clicked = st.button(session_button_label, width="stretch")

    if session_button_clicked and st.session_state.session_active:
        st.session_state.session_active = False
        st.session_state.session_notice = "stopped"
        st.rerun()

    if session_button_clicked and not st.session_state.session_active:
        st.session_state.session_active = True
        st.session_state.session_notice = None
        st.rerun()

    if not st.session_state.session_active:
        if st.session_state.session_notice == "stopped":
            st.info("Session stopped.")
            st.session_state.session_notice = None
        elif st.session_state.session_notice == "finished":
            st.success("Session finished.")
            st.session_state.session_notice = None
        st.info("Select a model, then start session.")
        return

    device, model, scaler, label_encoder = load_runtime(model_name, DEFAULT_MODELS_ROOT, feature_count=FEATURE_COUNT)
    similarity_asset = load_similarity_asset(model_name, DEFAULT_MODELS_ROOT)
    pose_module = load_pose_module()
    landmark_indices = build_landmark_indices(pose_module)
    angle_triplets = get_angle_triplets()

    if camera_source == "Local OpenCV (Desktop)":
        run_local_session(model_name, model, scaler, label_encoder, device, pose_module, landmark_indices, angle_triplets, similarity_asset)
    else:
        run_browser_session(model_name, model, scaler, label_encoder, device, pose_module, landmark_indices, angle_triplets, similarity_asset)


if __name__ == "__main__":
    main()