Spaces:
Paused
Paused
File size: 20,995 Bytes
4f1c726 4d5ee9e 4f1c726 4d5ee9e 4f1c726 f5d5899 4f1c726 f5d5899 4f1c726 f5d5899 4f1c726 f5d5899 4d5ee9e 4f1c726 f5d5899 4f1c726 f5d5899 4f1c726 f5d5899 4f1c726 4d5ee9e 4f1c726 4d5ee9e 4f1c726 4d5ee9e 4f1c726 504e17a 4f1c726 f5d5899 4f1c726 f5d5899 7b05fce 4f1c726 f5d5899 4f1c726 7b05fce 4f1c726 |
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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
"""
Copyright (c) 2025 MyoLab, Inc. All Rights Reserved.
This software and associated documentation files (the "Software") are the intellectual property of MyoLab, Inc. Unauthorized copying, modification, distribution, or use of this code, in whole or in part, without express written permission from the copyright owner is strictly prohibited.
MyoSDK Retargeting App
"""
import os
import tempfile
import time
import cv2
import gradio as gr
import numpy as np
import pandas as pd
import plotly.graph_objs as go
import spaces
import torch
from metrabs_pytorch.scripts.run_video import run_metrabs_video
from myo_tools.mjs.marker.marker_api import get_marker_names
from myo_tools.utils.file_ops.dataframe_utils import from_array_to_dataframe
from myosdk import Client
PLOT_CONFIG = {
"plot_bgcolor": "#0f172a",
"paper_bgcolor": "#0f172a",
"font": {"color": "#e2e8f0", "family": "Inter, system-ui, sans-serif"},
"xaxis": {"gridcolor": "#1e293b", "linecolor": "#334155"},
"yaxis": {"gridcolor": "#1e293b", "linecolor": "#334155"},
}
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
def draw_keypoints(frame, poses2d, radius=10):
"""
frame: HxWx3 uint8
poses2d: NxJx2 (N people, J joints)
"""
for person in poses2d:
for x, y in person:
cv2.circle(frame, (int(x), int(y)), radius, (0, 255, 0), -1)
return frame
def save_video_with_keypoints(results, output_video):
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(
output_video,
fourcc,
results[0]["fps"],
(results[0]["frame_bgr"].shape[1], results[0]["frame_bgr"].shape[0]),
)
for res in results:
frame = res["frame_bgr"]
fps = res["fps"]
poses2d = res["poses2d"] # NxJx2
frame = draw_keypoints(frame, poses2d)
out.write(frame)
out.release()
return output_video
def load_all_videos():
video_dir = os.path.join(os.path.dirname(__file__), "./data")
return [
os.path.abspath(os.path.join(video_dir, f))
for f in os.listdir(video_dir)
if f.lower().endswith((".mp4", ".avi", ".mov", ".mkv"))
]
# ------------------------------------------------------------
# Retargeting
# ------------------------------------------------------------
def run_retargeting_c3d(api_key, c3d_files, markerset_file):
status = []
output_files = []
# Initial validation
if not api_key:
api_key = os.getenv("MYOSDK_API_KEY")
if not api_key:
gr.Warning("β Error: API key is missing!", duration=5)
yield (
"β Error: API key is missing or invalid",
None,
None,
gr.update(value=[], visible=True),
gr.update(visible=False),
)
return
if markerset_file is None:
yield (
"β Error: Markerset XML file is required",
None,
None,
gr.update(value=[], visible=True),
gr.update(visible=False),
)
try:
# Initialize client
status.append("πΉ Initializing MyoSDK client...")
init_time = time.time()
yield "\n".join(status), None, None, gr.update(visible=False), gr.update(
visible=False
)
client = Client(api_key=api_key)
status.append(
f"πΉ MyoSDK client initialized in { time.time() - init_time:.2f} seconds"
)
init_time = time.time()
# Upload markerset
status.append("πΉ Uploading markerset file...")
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
mk_asset = client.assets.upload_file(markerset_file.name)
status.append(
f"πΉ Markerset file uploaded in {time.time() - init_time:.2f} seconds"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
mk_id = mk_asset["asset_id"]
# Process each C3D file
total_files = len(c3d_files)
for idx, f in enumerate(c3d_files):
status.append(
f"β‘ Processing file {idx + 1}/{total_files}: {os.path.basename(f)}"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
init_time = time.time()
c3d_asset = client.assets.upload_file(f)
status.append(
f"\tπΉ C3D file uploaded in {time.time() - init_time:.2f} seconds"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
init_time = time.time()
job = client.jobs.start_retarget(
c3d_asset_id=c3d_asset["asset_id"],
markerset_asset_id=mk_id,
)
status.append(
f"\tπΉ Retargeting job started in {time.time() - init_time:.2f} seconds"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
init_time = time.time()
result = client.jobs.wait(job["job_id"])
status.append(
f"\tπΉ Retargeting job completed in {time.time() - init_time:.2f} seconds"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
if result["status"] != "SUCCEEDED":
status.append(f"\tβ Failed retarget for {os.path.basename(f)}")
continue
status.append(f"\tβ
Retargeting completed for {os.path.basename(f)}")
base = os.path.splitext(os.path.basename(f))[0]
out_path = os.path.join(tempfile.gettempdir(), base + ".npy")
client.assets.download(
result["output"]["retarget_output_asset_id"], out_path
)
output_files.append(out_path)
if not output_files:
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
)
# Load angles from first output file
status.append("πΉ Loading angle data...")
yield "\n".join(status), None, None, gr.update(
interactive=True, visible=True
), gr.update(visible=True)
data = np.load(output_files[0])
joint_angles = data["joint_angles_degrees"].squeeze()
joint_names = data["joint_names"]
df = pd.DataFrame(joint_angles, columns=[jn for jn in joint_names])
df.insert(0, "frame", df.index)
angle_list = list(df.columns[1:])
initial_value = [angle_list[0]] if angle_list else []
status.append("β
Complete!")
yield (
"\n".join(status),
gr.update(value=output_files, visible=True),
df,
gr.update(choices=angle_list, value=initial_value, visible=True),
gr.update(visible=True),
)
except Exception as e:
yield (
f"β {e}",
None,
None,
gr.update(visible=False),
gr.update(visible=False),
)
@spaces.GPU
def run_retargeting_video(
api_key,
video_file="",
model="metrabs",
):
status = []
# Initial validation
if not api_key:
api_key = os.getenv("MYOSDK_API_KEY")
if not api_key: # covers None, "", or other falsy values
gr.Warning("β Error: API key is missing!", duration=5)
yield (
"β Error: API key is missing or invalid",
None,
None,
gr.update(visible=False),
gr.update(visible=False),
video_file,
)
return
# Extract path from list if it's a list, otherwise use directly
if isinstance(video_file, list):
video_path = video_file[0] if len(video_file) > 0 else None
else:
video_path = video_file
if (
video_file is None
or (isinstance(video_file, list) and len(video_file) == 0)
or video_path is None
):
yield (
"β Error: No video file selected",
None,
None,
gr.update(visible=False),
gr.update(visible=False),
video_file,
)
return
try:
print("πΉ Pose Extraction from Video Started")
status.append(
"πΉ Pose Extraction from Video Started ... this may take a while depending on the video length."
)
init_time = time.time()
yield "\n".join(status), None, None, gr.update(visible=False), gr.update(
visible=False
), video_path
results = list(
run_metrabs_video(video_path=video_path, device=DEVICE, visualize=False)
)
markers = np.array([res["poses3d"] for res in results]).squeeze()
fps = (
results[0]["fps"] if results else 25.0
) # Default to 25 fps if not available
video_with_keypoints = os.path.join(
tempfile.gettempdir(), "video_with_keypoints.mp4"
)
save_video_with_keypoints(results, video_with_keypoints)
yield "\n".join(status), None, None, gr.update(visible=False), gr.update(
visible=True
), video_with_keypoints,
status.append(
f"πΉ Pose Extraction from Video Completed in {time.time() - init_time:.2f} seconds with {len(markers)} frames extracted ({((time.time() - init_time)/len(markers)):.2f} seconds per frame)"
)
print("πΉ Pose Extraction from Video Completed")
yield "\n".join(status), None, None, gr.update(visible=False), gr.update(
visible=False
), video_with_keypoints
# Initialize client
status.append("πΉ Initializing MyoSDK client...")
init_time = time.time()
yield "\n".join(status), None, None, gr.update(visible=False), gr.update(
visible=False,
), video_with_keypoints
client = Client(api_key=api_key)
print(f"πΉ MyoSDK client initialized in { time.time() - init_time:.2f} seconds")
status.append(
f"πΉ MyoSDK client initialized in { time.time() - init_time:.2f} seconds"
)
init_time = time.time()
# Upload markerset
status.append("πΉ Uploading markerset file...")
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False,
), video_with_keypoints
markerset_file_name = "markersets/movi_metrabs_markerset.xml"
mk_asset = client.assets.upload_file(markerset_file_name)
status.append(
f"πΉ Markerset file uploaded in {time.time() - init_time:.2f} seconds"
)
print(f"πΉ Markerset file uploaded in {time.time() - init_time:.2f} seconds")
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
), video_with_keypoints
init_time = time.time()
marker_names = get_marker_names(markerset_file_name)
fn_parquet = os.path.join(tempfile.gettempdir(), "video_trackers.parquet")
from_array_to_dataframe(markers, marker_names, fps, fn_parquet)
markers_asset = client.assets.upload_file(fn_parquet, purpose="retarget")
print("fn_parquet: ", fn_parquet)
init_time = time.time()
job = client.jobs.start_retarget(
c3d_asset_id=markers_asset["asset_id"],
markerset_asset_id=mk_asset["asset_id"],
)
status.append(
f"\tπΉ Retargeting job started in {time.time() - init_time:.2f} seconds"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
), video_with_keypoints
init_time = time.time()
result = client.jobs.wait(job["job_id"])
status.append(
f"\tπΉ Retargeting job completed in {time.time() - init_time:.2f} seconds"
)
yield "\n".join(status), None, None, gr.update(value=[]), gr.update(
visible=False
), video_with_keypoints
print("STATUS: ", result["status"])
assert (
result["status"] == "SUCCEEDED"
), f"Failed retarget for {os.path.basename(video_path)}"
base = os.path.splitext(os.path.basename(video_path))[0]
out_path = os.path.join(tempfile.gettempdir(), base + ".npy")
client.assets.download(result["output"]["retarget_output_asset_id"], out_path)
assert os.path.exists(
out_path
), f"Failed to download retargeted data for {os.path.basename(video_path)}"
# Load angles from first output file
status.append("πΉ Loading angle data...")
yield "\n".join(status), None, None, gr.update(
interactive=True, visible=True
), gr.update(visible=True), video_with_keypoints
data = np.load(out_path)
joint_angles = data["joint_angles_degrees"].squeeze()
joint_names = data["joint_names"]
print(joint_angles)
print(joint_names)
df = pd.DataFrame(joint_angles, columns=[jn for jn in joint_names])
df.insert(0, "frame", df.index)
angle_list = list(df.columns[1:])
initial_value = [angle_list[0]] if angle_list else []
status.append("β
Complete!")
yield (
"\n".join(status),
gr.update(value=out_path, visible=True),
df,
gr.update(choices=angle_list, value=initial_value, visible=True),
gr.update(visible=True),
video_with_keypoints,
)
except Exception as e:
# Use video_path if defined, otherwise use video_file or None
error_video = (
video_path
if "video_path" in locals()
else (video_file if video_file else None)
)
yield (
"\n".join(status + ["\nβ Error: " + str(e)]),
None,
None,
gr.update(visible=False),
gr.update(visible=False),
error_video,
)
# ------------------------------------------------------------
# Plotting
# ------------------------------------------------------------
def update_plot(df, joints):
if df is None or df.empty:
return go.Figure()
if not joints:
return go.Figure()
if not isinstance(joints, list):
joints = [joints]
fig = go.Figure()
for j in joints:
if j in df.columns:
fig.add_trace(go.Scatter(x=df["frame"], y=df[j], mode="lines", name=j))
fig.update_layout(
title="Joint Angles",
xaxis_title="Frame",
yaxis_title="Angle Value",
plot_bgcolor="#1E1E1E",
paper_bgcolor="#1E1E1E",
font=dict(color="#F0F0F0", family="Arial"),
xaxis=dict(gridcolor="#444444", linecolor="#F0F0F0", tickcolor="#F0F0F0"),
yaxis=dict(gridcolor="#444444", linecolor="#F0F0F0", tickcolor="#F0F0F0"),
legend=dict(font=dict(color="#F0F0F0")),
)
return fig
with gr.Blocks() as app:
with gr.Row():
with gr.Column(scale=3):
gr.Markdown(
"""
## MyoSDK Retargeting
<span style="color:#6b7280">Joint visualization & motion retargeting pipelines</span>
This application allows you to retarget motion capture data to biomechanical models using MyoSDK's Kinesis engine.
Upload C3D files or videos to extract joint angles using [Kinesis](https://myolab.ai/blog/myokinesis) and visualize motion data.
"""
)
with gr.Column(scale=1):
api_key = gr.Textbox(
label="π API Key",
placeholder="Enter your MyoLab API key",
type="password",
info="Get your API key from https://dev.myolab.ai",
)
with gr.Tab("π Motion Capture Retargeting"):
gr.Markdown(
"""
Upload motion capture data in C3D format along with a markerset XML file to retarget the motion to a biomechanical model.
The process will extract joint angles and generate visualizations of the motion data.
"""
)
with gr.Row(equal_height=True):
with gr.Column(scale=2):
gr.Markdown(
"""
**1. Upload a Markerset File**
<br>
<span style="color:#6b7280; font-size: 0.9em">
Upload an XML file that defines the marker set configuration.
This file specifies which markers are used and their anatomical locations.
See [Markerset Editor](https://markerset-editor.myolab.ai) for more details.
</span>
"""
)
markerset = gr.File(
# label=None,
file_types=[".xml"],
elem_id="file-upload-markerset",
value=os.path.join(
os.path.dirname(__file__), "./markersets/cmu_markerset.xml"
),
)
with gr.Column(scale=2):
gr.Markdown(
"""
**2. Upload C3D Motion Capture File(s)**
<br>
<span style="color:#6b7280; font-size: 0.9em">
Upload one or more C3D files containing 3D marker trajectories from motion capture systems.
Multiple files can be processed in batch. Each file will be retargeted using the same markerset.
</span>
"""
)
c3d_files = gr.File(
label=None,
file_types=[".c3d"],
elem_id="file-upload-c3d",
file_count="multiple",
value=[os.path.join(os.path.dirname(__file__), "./data/35_30.c3d")],
)
run_btn_c3d = gr.Button("3. π Run Retargeting", variant="primary")
with gr.Tab("π₯ Video-Based Motion Retargeting"):
gr.Markdown(
"""
Extract 3D pose from video and retarget it to a biomechanical model using [Kinesis](https://myolab.ai/blog/myokinesis).
β οΈ **Important:** Using Metrabs for video-based motion retargeting which is **ONLY FOR RESEARCH/ACADEMIC USE**.
Please cite the [paper](https://arxiv.org/abs/2409.06042) if you use this feature.
For commercial applications, please contact MyoLab.
"""
)
video_file = gr.Video(
label="1. Upload a Video File (Supported formats: MP4, AVI, MOV, MKV)",
height=400,
value=os.path.join(
os.path.dirname(__file__), "./data/13710671_1080_1920_25fps.mp4"
),
)
run_v2m_btn_video = gr.Button(
"2. π Run Retargeting from Video", variant="primary"
)
output_file = gr.File(
label="π₯ Download Results - Download the retargeted motion data as a NumPy (.npy) file containing joint angles and metadata.",
visible=False,
)
df_state = gr.State()
joint_dropdown = gr.Dropdown(
label="Select Joint Angle(s) to Visualize",
interactive=False,
multiselect=True,
visible=True,
info="After processing completes, select one or more joint angles to plot. The dropdown will be populated with available joints from the retargeted data.",
)
plot_area = gr.Plot(
label="Joint Angle Visualization - Interactive plot showing the selected joint angles over time. Use the legend to toggle individual joints on/off.",
visible=False,
)
status_box = gr.Textbox(
label="Processing Status",
lines=12,
info="Real-time status updates showing the progress of file uploads, retargeting jobs, and data processing.",
)
joint_dropdown.change(
fn=update_plot,
inputs=[df_state, joint_dropdown],
outputs=[plot_area],
)
run_btn_c3d.click(
fn=run_retargeting_c3d,
inputs=[api_key, c3d_files, markerset],
outputs=[status_box, output_file, df_state, joint_dropdown, plot_area],
)
run_v2m_btn_video.click(
fn=run_retargeting_video,
inputs=[api_key, video_file],
outputs=[
status_box,
output_file,
df_state,
joint_dropdown,
plot_area,
video_file,
],
)
if __name__ == "__main__":
app.launch(
share=True,
# server_port=7860,
)
|