Spaces:
Runtime error
Runtime error
Amanpreet
commited on
Commit
·
4276ea6
1
Parent(s):
5072251
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import shutil
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
def run_command(command, description, working_dir):
|
| 10 |
+
st.write(f"Running command: {' '.join(command)}")
|
| 11 |
+
try:
|
| 12 |
+
start_time = time.time()
|
| 13 |
+
process = subprocess.run(
|
| 14 |
+
command,
|
| 15 |
+
check=True,
|
| 16 |
+
cwd=working_dir,
|
| 17 |
+
capture_output=True,
|
| 18 |
+
text=True
|
| 19 |
+
)
|
| 20 |
+
end_time = time.time()
|
| 21 |
+
execution_time = end_time - start_time
|
| 22 |
+
st.write(f"{description} completed in {execution_time:.2f} seconds")
|
| 23 |
+
if process.stdout:
|
| 24 |
+
st.text(f"Output:\n{process.stdout}")
|
| 25 |
+
if process.stderr:
|
| 26 |
+
st.text(f"Warnings:\n{process.stderr}")
|
| 27 |
+
return True
|
| 28 |
+
|
| 29 |
+
except subprocess.CalledProcessError as e:
|
| 30 |
+
st.error(f"Error in {description}: {e.stderr}")
|
| 31 |
+
return False
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"Unexpected error in {description}: {str(e)}")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
def process_video(video_file):
|
| 38 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 39 |
+
gen_skes_path = os.path.join(base_dir, "VideoToNPZ", "gen_skes.py")
|
| 40 |
+
convert_obj_path = os.path.join(base_dir, "convertNPZtoBVH", "conver_obj.py")
|
| 41 |
+
convert_bvh_path = os.path.join(base_dir, "convertNPZtoBVH", "conver_bvh.py")
|
| 42 |
+
|
| 43 |
+
st.write(f"Base directory: {base_dir}")
|
| 44 |
+
st.write(f"gen_skes_path: {gen_skes_path}")
|
| 45 |
+
|
| 46 |
+
for script_path in [gen_skes_path, convert_obj_path, convert_bvh_path]:
|
| 47 |
+
if not os.path.exists(script_path):
|
| 48 |
+
st.error(f"Required script not found: {script_path}")
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 52 |
+
video_path = os.path.join(tmp_dir, "input_video.mp4")
|
| 53 |
+
with open(video_path, "wb") as f:
|
| 54 |
+
f.write(video_file.read())
|
| 55 |
+
|
| 56 |
+
if not os.path.exists(video_path):
|
| 57 |
+
st.error(f"Video file not found at: {video_path}")
|
| 58 |
+
return None
|
| 59 |
+
st.write(f"Video file saved at: {video_path}, size: {os.path.getsize(video_path)} bytes")
|
| 60 |
+
|
| 61 |
+
pipeline_steps = [
|
| 62 |
+
{
|
| 63 |
+
"command": [sys.executable, gen_skes_path, "-v", video_path],
|
| 64 |
+
"description": "Generating skeletons",
|
| 65 |
+
"working_dir": os.path.dirname(gen_skes_path)
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"command": [sys.executable, convert_obj_path],
|
| 69 |
+
"description": "Converting to OBJ sequence",
|
| 70 |
+
"working_dir": os.path.dirname(convert_obj_path)
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"command": [sys.executable, convert_bvh_path],
|
| 74 |
+
"description": "Converting to BVH",
|
| 75 |
+
"working_dir": os.path.dirname(convert_bvh_path)
|
| 76 |
+
}
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
successful = 0
|
| 80 |
+
failed = 0
|
| 81 |
+
progress_bar = st.progress(0)
|
| 82 |
+
status_text = st.empty()
|
| 83 |
+
|
| 84 |
+
for i, step in enumerate(pipeline_steps):
|
| 85 |
+
status_text.text(f"Processing: {step['description']}...")
|
| 86 |
+
if run_command(step["command"], step["description"], step["working_dir"]):
|
| 87 |
+
successful += 1
|
| 88 |
+
if step["description"] == "Generating skeletons":
|
| 89 |
+
npz_path = os.path.join(base_dir, "outputs", "npz", "input_video.npz")
|
| 90 |
+
st.write(f"Expected NPZ file at: {npz_path}")
|
| 91 |
+
if os.path.exists(npz_path):
|
| 92 |
+
st.write(f"NPZ file found, size: {os.path.getsize(npz_path)} bytes")
|
| 93 |
+
else:
|
| 94 |
+
st.error("NPZ file not generated!")
|
| 95 |
+
else:
|
| 96 |
+
failed += 1
|
| 97 |
+
progress_bar.progress((i + 1) / len(pipeline_steps))
|
| 98 |
+
|
| 99 |
+
status_text.text("Processing complete!")
|
| 100 |
+
|
| 101 |
+
bvh_output_dir = os.path.join(os.path.dirname(convert_bvh_path), "../outputs/bvh")
|
| 102 |
+
bvh_file = os.path.join(bvh_output_dir, "output.bvh")
|
| 103 |
+
st.write(f"Checking for BVH file at: {bvh_file}")
|
| 104 |
+
|
| 105 |
+
if os.path.exists(bvh_file) and successful == len(pipeline_steps):
|
| 106 |
+
return bvh_file
|
| 107 |
+
else:
|
| 108 |
+
st.error("Failed to generate BVH file")
|
| 109 |
+
return None
|
| 110 |
+
|
| 111 |
+
def main():
|
| 112 |
+
st.title("Video to BVH Converter")
|
| 113 |
+
st.write("Upload a video file to convert it to BVH format")
|
| 114 |
+
|
| 115 |
+
uploaded_file = st.file_uploader("Choose a video file", type=['mp4', 'avi', 'mov'])
|
| 116 |
+
|
| 117 |
+
if uploaded_file is not None:
|
| 118 |
+
st.video(uploaded_file)
|
| 119 |
+
|
| 120 |
+
if st.button("Convert to BVH"):
|
| 121 |
+
with st.spinner("Processing video... This may take a few minutes"):
|
| 122 |
+
bvh_file = process_video(uploaded_file)
|
| 123 |
+
|
| 124 |
+
if bvh_file:
|
| 125 |
+
with open(bvh_file, "rb") as f:
|
| 126 |
+
st.download_button(
|
| 127 |
+
label="Download BVH File",
|
| 128 |
+
data=f,
|
| 129 |
+
file_name="output.bvh",
|
| 130 |
+
mime="application/octet-stream"
|
| 131 |
+
)
|
| 132 |
+
st.success("Conversion completed successfully!")
|
| 133 |
+
|
| 134 |
+
st.markdown("""
|
| 135 |
+
### Instructions
|
| 136 |
+
1. Upload a video file using the uploader above
|
| 137 |
+
2. Click 'Convert to BVH' to start the conversion process
|
| 138 |
+
3. Wait for processing to complete
|
| 139 |
+
4. Download the resulting BVH file using the provided button
|
| 140 |
+
""")
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
main()
|