Spaces:
Runtime error
Runtime error
File size: 19,525 Bytes
4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 ef58188 9d2691d ef58188 9d2691d 4e2aa43 9d2691d 4e2aa43 9d2691d 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 9d2691d 4e2aa43 4276ea6 4e2aa43 9d2691d 4e2aa43 4276ea6 4e2aa43 4276ea6 4e2aa43 9d2691d 4e2aa43 9d2691d 4e2aa43 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4e2aa43 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 9d2691d 4276ea6 |
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 |
import streamlit as st
import subprocess
import sys
import os
import time
import shutil
import tempfile
import threading
import queue
from datetime import datetime
from pathlib import Path
def run_command(command, working_dir, progress_bar, progress_text, step_start_progress, step_weight, show_progress=True):
try:
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
process = subprocess.Popen(
command,
cwd=working_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
universal_newlines=True,
env=env
)
stdout_queue = queue.Queue()
stderr_queue = queue.Queue()
def read_output(pipe, q, source):
for line in iter(pipe.readline, ''):
q.put((source, line))
pipe.close()
stdout_thread = threading.Thread(target=read_output, args=(process.stdout, stdout_queue, 'stdout'))
stderr_thread = threading.Thread(target=read_output, args=(process.stderr, stderr_queue, 'stderr'))
stdout_thread.daemon = True
stderr_thread.daemon = True
stdout_thread.start()
stderr_thread.start()
total_progress = step_start_progress
stderr_lines = []
while process.poll() is None or not (stdout_queue.empty() and stderr_queue.empty()):
try:
source, line = next((q.get_nowait() for q in [stdout_queue, stderr_queue] if not q.empty()), (None, None))
if line:
if source == 'stdout':
if show_progress and line.startswith("PROGRESS:"):
try:
progress = float(line.strip().split("PROGRESS:")[1]) / 100
if Path(command[1]).name == 'gen_skes.py':
if progress <= 1.0:
adjusted_progress = step_start_progress + (progress * 0.6)
else:
adjusted_progress = step_start_progress + 0.6 + ((progress - 1.0) * 0.2)
else:
adjusted_progress = step_start_progress + (progress * step_weight)
total_progress = min(adjusted_progress, step_start_progress + step_weight)
progress_bar.progress(total_progress)
progress_text.text(f"Progress: {int(total_progress * 100)}%")
except ValueError:
pass
elif source == 'stderr':
stderr_lines.append(line.strip())
except queue.Empty:
time.sleep(0.01)
stdout_thread.join()
stderr_thread.join()
if process.returncode != 0:
stderr_output = '\n'.join(stderr_lines)
st.error(f"Error in {Path(command[1]).name}:\n{stderr_output}")
return False
if show_progress:
progress_bar.progress(step_start_progress + step_weight)
progress_text.text(f"Progress: {int((step_start_progress + step_weight) * 100)}%")
return True
except Exception as e:
st.error(f"Exception in {Path(command[1]).name}: {str(e)}")
return False
def cleanup_output_folder(output_dir, delay=1800):
time.sleep(delay)
if os.path.exists(output_dir):
shutil.rmtree(output_dir, ignore_errors=True)
print(f"Deleted temporary output folder after timeout: {output_dir}")
def process_video(video_file):
base_dir = Path(__file__).parent.resolve()
gen_skes_path = base_dir / "VideoToNPZ" / "gen_skes.py"
convert_obj_path = base_dir / "convertNPZtoBVH" / "conver_obj.py"
convert_bvh_path = base_dir / "convertNPZtoBVH" / "conver_bvh.py"
for script_path in [gen_skes_path, convert_obj_path, convert_bvh_path]:
if not script_path.exists():
st.error(f"Required script not found: {script_path}")
return None
with tempfile.TemporaryDirectory() as tmp_dir:
video_path = Path(tmp_dir) / "input_video.mp4"
with open(video_path, "wb") as f:
f.write(video_file.read())
if not video_path.exists():
st.error(f"Video file not found at: {video_path}")
return None
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
output_dir = base_dir / f"outputs_{timestamp}"
output_dir.mkdir(exist_ok=True)
if not os.access(output_dir, os.W_OK):
st.error(f"Cannot write to output directory: {output_dir}")
return None
default_output_dir = base_dir / "outputs"
pipeline_steps = [
{"command": [sys.executable, str(gen_skes_path), "-v", str(video_path)], "working_dir": gen_skes_path.parent, "weight": 0.8, "show_progress": True},
{"command": [sys.executable, str(convert_obj_path), "--output-dir", str(output_dir)], "working_dir": convert_obj_path.parent, "weight": 0.0, "show_progress": False},
{"command": [sys.executable, str(convert_bvh_path), "--output-dir", str(output_dir)], "working_dir": convert_bvh_path.parent, "weight": 0.2, "show_progress": True}
]
progress_bar = st.progress(0.0)
progress_text = st.empty()
total_progress = 0.0
for i, step in enumerate(pipeline_steps):
success = run_command(
step["command"],
step["working_dir"],
progress_bar,
progress_text,
total_progress,
step["weight"],
show_progress=step["show_progress"]
)
if not success:
st.error(f"Failed at step: {' '.join(map(str, step['command']))}")
if default_output_dir.exists():
shutil.rmtree(default_output_dir, ignore_errors=True)
return None
if i == 0 and default_output_dir.exists():
npz_dir = default_output_dir / "npz"
if npz_dir.exists():
target_npz_dir = output_dir / "npz"
shutil.move(str(npz_dir), str(target_npz_dir))
if default_output_dir.exists():
shutil.rmtree(default_output_dir, ignore_errors=True)
total_progress += step["weight"]
if step["show_progress"]:
progress_bar.progress(min(total_progress, 1.0))
progress_text.text(f"Progress: {int(total_progress * 100)}%")
bvh_output_dir = output_dir / "bvh"
bvh_file = bvh_output_dir / "output.bvh"
if bvh_file.exists():
cleanup_thread = threading.Thread(target=cleanup_output_folder, args=(output_dir,))
cleanup_thread.daemon = True
cleanup_thread.start()
return {
'bvh_file': bvh_file,
'output_dir': output_dir
}
else:
st.error(f"Failed to generate BVH file at: {bvh_file}")
if default_output_dir.exists():
shutil.rmtree(default_output_dir, ignore_errors=True)
return None
def cleanup_immediate(output_dir):
if output_dir and os.path.exists(output_dir):
shutil.rmtree(output_dir, ignore_errors=True)
st.success("Output folder cleaned up successfully.")
else:
st.warning("No output folder to clean up.")
def main():
st.set_page_config(
page_title="Motion Capture Studio | Video to BVH Converter",
page_icon="π¬",
layout="wide",
initial_sidebar_state="collapsed"
)
st.markdown("""
<style>
:root {
--bg-color: #1a1a1a;
--card-bg: #252525;
--primary-color: #bb86fc;
--secondary-color: #03dac6;
--error-color: #cf6679;
--text-color: #e0e0e0;
--text-secondary: #a0a0a0;
--border-color: #404040;
--shadow-color: rgba(0, 0, 0, 0.5);
}
.stApp {
background-color: var(--bg-color);
font-family: 'Arial', sans-serif;
}
h1, h2, h3, h4, h5, h6, p, li, div {
color: var(--text-color) !important;
}
.card {
background-color: var(--card-bg);
border-radius: 20px;
padding: 2rem;
margin: 1rem auto;
border: 1px solid var(--border-color);
box-shadow: 0 8px 30px var(--shadow-color);
max-width: 1200px;
}
.main-title {
font-size: 3.5rem;
font-weight: 900;
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-align: center;
margin: 1.5rem 0 0.5rem;
text-shadow: 0 2px 10px rgba(187, 134, 252, 0.3);
}
.subtitle {
font-size: 1.3rem;
color: var(--text-secondary);
text-align: center;
margin-bottom: 2.5rem;
font-weight: 300;
letter-spacing: 0.5px;
}
.section-title {
font-size: 1.5rem;
font-weight: 700;
color: var(--primary-color) !important;
margin-bottom: 1.2rem;
text-transform: uppercase;
letter-spacing: 1px;
}
.stButton > button {
background: linear-gradient(135deg, var(--primary-color), #9b59f5);
color: #fff !important;
border-radius: 12px;
padding: 0.8rem 2.5rem;
font-weight: 600;
font-size: 1.2rem;
border: none;
transition: all 0.3s ease;
width: 100%;
box-shadow: 0 4px 15px rgba(187, 134, 252, 0.3);
}
.stButton > button:hover {
transform: translateY(-3px);
box-shadow: 0 6px 20px rgba(187, 134, 252, 0.5);
background: linear-gradient(135deg, #9b59f5, var(--primary-color));
}
.stDownloadButton > button {
background: linear-gradient(135deg, var(--secondary-color), #02b3a3);
color: #fff !important;
border-radius: 12px;
padding: 0.8rem 2.5rem;
font-weight: 600;
font-size: 1.2rem;
border: none;
transition: all 0.3s ease;
width: 100%;
box-shadow: 0 4px 15px rgba(3, 218, 198, 0.3);
}
.stDownloadButton > button:hover {
transform: translateY(-3px);
box-shadow: 0 6px 20px rgba(3, 218, 198, 0.5);
background: linear-gradient(135deg, #02b3a3, var(--secondary-color));
}
.upload-container {
border: 2px dashed var(--border-color);
border-radius: 15px;
padding: 2.5rem;
text-align: center;
background-color: rgba(255, 255, 255, 0.05);
transition: all 0.3s ease;
}
.upload-container:hover {
border-color: var(--primary-color);
box-shadow: 0 0 20px rgba(187, 134, 252, 0.2);
}
.video-container {
border-radius: 15px;
overflow: hidden;
box-shadow: 0 6px 25px var(--shadow-color);
margin: 1rem 0;
border: 1px solid var(--border-color);
background-color: #000;
}
.status-indicator {
padding: 1rem;
border-radius: 12px;
margin: 0.8rem 0;
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 1.1rem;
}
.status-indicator.info {
background-color: rgba(187, 134, 252, 0.1);
border-left: 5px solid var(--primary-color);
}
.status-indicator.success {
background-color: rgba(3, 218, 198, 0.1);
border-left: 5px solid var(--secondary-color);
}
.control-section {
text-align: center;
margin: 2rem 0;
padding: 1rem;
background-color: rgba(255, 255, 255, 0.03);
border-radius: 12px;
border: 1px solid var(--border-color);
}
.stProgress {
width: 100%;
height: 12px;
border-radius: 6px;
background-color: #333;
margin: 1rem 0;
}
.stProgress > div > div {
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
border-radius: 6px;
}
.progress-text {
font-weight: 600;
padding: 0.5rem 1rem;
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
color: #fff !important;
border-radius: 20px;
box-shadow: 0 2px 10px rgba(187, 134, 252, 0.3);
display: inline-block;
}
.separator {
height: 2px;
background: linear-gradient(90deg, transparent, var(--border-color), transparent);
margin: 2.5rem 0;
}
.footer {
text-align: center;
padding: 2.5rem 0 1.5rem;
font-size: 0.95rem;
color: var(--text-secondary);
border-top: 1px solid var(--border-color);
letter-spacing: 0.5px;
}
.pipeline-step {
display: flex;
align-items: center;
background-color: rgba(255, 255, 255, 0.05);
padding: 1rem;
border-radius: 12px;
margin-bottom: 1rem;
border: 1px solid var(--border-color);
transition: all 0.3s ease;
}
.pipeline-step:hover {
background-color: rgba(255, 255, 255, 0.08);
border-color: var(--primary-color);
box-shadow: 0 4px 15px rgba(187, 134, 252, 0.1);
}
.step-number {
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
color: #fff;
border-radius: 50%;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
margin-right: 1.2rem;
font-weight: 600;
font-size: 1.2rem;
box-shadow: 0 2px 10px rgba(187, 134, 252, 0.3);
}
.step-title {
font-weight: 600;
font-size: 1.1rem;
margin-bottom: 0.3rem;
}
.step-description {
color: var(--text-secondary);
font-size: 0.95rem;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.03); }
100% { transform: scale(1); }
}
.animate-pulse {
animation: pulse 2s infinite;
}
</style>
""", unsafe_allow_html=True)
# Header
st.markdown('<h1 class="main-title animate-pulse">Motion Capture Studio</h1>', unsafe_allow_html=True)
st.markdown('<p class="subtitle">Transform your videos into professional BVH motion files with cutting-edge AI</p>', unsafe_allow_html=True)
# Main content
st.markdown('<div class="card">', unsafe_allow_html=True)
col1, col2 = st.columns([1, 1], gap="medium")
with col1:
st.markdown('<h3 class="section-title">Upload Your Video</h3>', unsafe_allow_html=True)
st.markdown('<div class="upload-container">', unsafe_allow_html=True)
uploaded_file = st.file_uploader(
"Drop your video here or click to browse",
type=['mp4', 'avi', 'mov'],
help="For best results, use clear full-body motion videos with good lighting",
key="file_uploader"
)
if uploaded_file:
st.session_state['uploaded_file'] = uploaded_file
st.markdown('</div>', unsafe_allow_html=True)
if not st.session_state.get('uploaded_file'):
st.markdown("""
<div class="status-indicator info">
π‘ <strong>Pro Tip:</strong> Use MP4, AVI, or MOV files with clear, well-lit full-body motion
</div>
""", unsafe_allow_html=True)
with col2:
st.markdown('<h3 class="section-title">Video Preview</h3>', unsafe_allow_html=True)
if uploaded_file := st.session_state.get('uploaded_file', None):
st.markdown('<div class="video-container">', unsafe_allow_html=True)
st.video(uploaded_file)
st.markdown('</div>', unsafe_allow_html=True)
else:
st.markdown("""
<div style="height: 250px; display: flex; align-items: center; justify-content: center;
border: 2px dashed var(--border-color); border-radius: 15px; background-color: rgba(255, 255, 255, 0.05);">
<span style="color: var(--text-secondary); font-size: 1.2rem;">Your video preview will appear here</span>
</div>
""", unsafe_allow_html=True)
# Control Section (Button and Progress Bar)
if st.session_state.get('uploaded_file'):
st.markdown('<div class="control-section">', unsafe_allow_html=True)
if st.button("β‘ Start Motion Capture", key="convert_btn"):
with st.spinner("Processing your video..."):
progress_bar = st.progress(0.0)
progress_text = st.empty()
st.markdown('<div class="status-indicator info">π Analyzing motion patterns...</div>', unsafe_allow_html=True)
result = process_video(st.session_state['uploaded_file'])
if result:
st.markdown('<div class="status-indicator success">β
Motion capture complete!</div>', unsafe_allow_html=True)
with open(result['bvh_file'], "rb") as f:
st.download_button(
label="π₯ Download BVH File",
data=f,
file_name="motion_capture.bvh",
mime="application/octet-stream",
on_click=cleanup_immediate,
args=(result['output_dir'],),
key="download_btn"
)
st.markdown('</div>', unsafe_allow_html=True)
# Pipeline Info
st.markdown('<div class="separator"></div>', unsafe_allow_html=True)
st.markdown('<h3 class="section-title">Processing Pipeline</h3>', unsafe_allow_html=True)
st.markdown("""
<div class="pipeline-step">
<div class="step-number">1</div>
<div>
<div class="step-title">Pose Estimation</div>
<div class="step-description">AI detects and tracks human movements frame-by-frame</div>
</div>
</div>
<div class="pipeline-step">
<div class="step-number">2</div>
<div>
<div class="step-title">3D Conversion</div>
<div class="step-description">Converts 2D poses into 3D spatial data</div>
</div>
</div>
<div class="pipeline-step">
<div class="step-number">3</div>
<div>
<div class="step-title">BVH Generation</div>
<div class="step-description">Formats motion data into industry-standard BVH files</div>
</div>
</div>
""", unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
# Footer
st.markdown("""
<div class="footer">
Β© 2025 Motion Capture Studio | Powered by Streamlit & Advanced AI Technology
</div>
""", unsafe_allow_html=True)
if __name__ == "__main__":
main() |