Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -91,11 +91,11 @@ def get_bvh_from_folder(drive_service, folder_id=None):
|
|
91 |
bvh_file = files[0]
|
92 |
bvh_id = bvh_file['id']
|
93 |
bvh_url = f"https://drive.google.com/uc?id={bvh_id}"
|
94 |
-
return bvh_id, bvh_url
|
95 |
-
return None, None
|
96 |
except Exception as e:
|
97 |
st.error(f"Error checking folder for BVH: {e}")
|
98 |
-
return None, None
|
99 |
|
100 |
def download_notebook_from_drive(drive_service, temp_dir):
|
101 |
try:
|
@@ -197,6 +197,22 @@ def check_kernel_exists(api, notebook_slug):
|
|
197 |
st.error(f"Kernel check failed: {e}")
|
198 |
return False
|
199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
def process_video(api, drive_service, video_file):
|
201 |
video_file_id = None
|
202 |
bvh_file_id = None
|
@@ -259,18 +275,32 @@ def process_video(api, drive_service, video_file):
|
|
259 |
delete_from_drive(drive_service, video_file_id)
|
260 |
return None
|
261 |
|
262 |
-
bvh_file_id, bvh_url = get_bvh_from_folder(drive_service)
|
263 |
-
if bvh_url:
|
264 |
-
|
265 |
-
|
|
|
|
|
266 |
progress_bar.progress(1.0)
|
267 |
progress_text.text("Complete!")
|
|
|
|
|
268 |
if video_file_id:
|
269 |
delete_from_drive(drive_service, video_file_id)
|
270 |
if bvh_file_id:
|
271 |
delete_from_drive(drive_service, bvh_file_id)
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
if execution_started and current_status in ['complete', 'error']:
|
276 |
progress_bar.progress(0.8 if current_status == 'complete' else 0.6)
|
@@ -381,15 +411,26 @@ def main():
|
|
381 |
result = process_video(api, drive_service, st.session_state['uploaded_file'])
|
382 |
|
383 |
if result and 'bvh_data' in result:
|
384 |
-
|
|
|
385 |
st.download_button(
|
386 |
label="Download BVH",
|
387 |
data=result['bvh_data'],
|
388 |
-
file_name=
|
389 |
mime="application/octet-stream"
|
390 |
)
|
391 |
else:
|
392 |
-
st.error("Failed to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
st.markdown('</div>', unsafe_allow_html=True)
|
394 |
|
395 |
if __name__ == "__main__":
|
|
|
91 |
bvh_file = files[0]
|
92 |
bvh_id = bvh_file['id']
|
93 |
bvh_url = f"https://drive.google.com/uc?id={bvh_id}"
|
94 |
+
return bvh_id, bvh_url, bvh_file['name']
|
95 |
+
return None, None, None
|
96 |
except Exception as e:
|
97 |
st.error(f"Error checking folder for BVH: {e}")
|
98 |
+
return None, None, None
|
99 |
|
100 |
def download_notebook_from_drive(drive_service, temp_dir):
|
101 |
try:
|
|
|
197 |
st.error(f"Kernel check failed: {e}")
|
198 |
return False
|
199 |
|
200 |
+
def download_and_save_bvh(bvh_url, filename):
|
201 |
+
try:
|
202 |
+
response = requests.get(bvh_url)
|
203 |
+
if response.status_code == 200:
|
204 |
+
temp_dir = tempfile.mkdtemp()
|
205 |
+
bvh_path = os.path.join(temp_dir, filename)
|
206 |
+
with open(bvh_path, 'wb') as f:
|
207 |
+
f.write(response.content)
|
208 |
+
return bvh_path, response.content
|
209 |
+
else:
|
210 |
+
st.error(f"Failed to download BVH: Status code {response.status_code}")
|
211 |
+
return None, None
|
212 |
+
except Exception as e:
|
213 |
+
st.error(f"Error downloading BVH: {e}")
|
214 |
+
return None, None
|
215 |
+
|
216 |
def process_video(api, drive_service, video_file):
|
217 |
video_file_id = None
|
218 |
bvh_file_id = None
|
|
|
275 |
delete_from_drive(drive_service, video_file_id)
|
276 |
return None
|
277 |
|
278 |
+
bvh_file_id, bvh_url, bvh_filename = get_bvh_from_folder(drive_service)
|
279 |
+
if bvh_url and bvh_filename:
|
280 |
+
# Automatically download the BVH file
|
281 |
+
bvh_path, bvh_data = download_and_save_bvh(bvh_url, bvh_filename or "motion_capture.bvh")
|
282 |
+
|
283 |
+
if bvh_path and bvh_data:
|
284 |
progress_bar.progress(1.0)
|
285 |
progress_text.text("Complete!")
|
286 |
+
|
287 |
+
# Cleanup files on Google Drive
|
288 |
if video_file_id:
|
289 |
delete_from_drive(drive_service, video_file_id)
|
290 |
if bvh_file_id:
|
291 |
delete_from_drive(drive_service, bvh_file_id)
|
292 |
+
|
293 |
+
st.success("Motion capture complete! BVH file ready for download.")
|
294 |
+
|
295 |
+
# Save the BVH data to session state for download
|
296 |
+
st.session_state['bvh_data'] = bvh_data
|
297 |
+
st.session_state['bvh_filename'] = bvh_filename or "motion_capture.bvh"
|
298 |
+
|
299 |
+
return {
|
300 |
+
'bvh_data': bvh_data,
|
301 |
+
'bvh_path': bvh_path,
|
302 |
+
'bvh_filename': bvh_filename or "motion_capture.bvh"
|
303 |
+
}
|
304 |
|
305 |
if execution_started and current_status in ['complete', 'error']:
|
306 |
progress_bar.progress(0.8 if current_status == 'complete' else 0.6)
|
|
|
411 |
result = process_video(api, drive_service, st.session_state['uploaded_file'])
|
412 |
|
413 |
if result and 'bvh_data' in result:
|
414 |
+
# The BVH file has already been downloaded and deleted from Drive
|
415 |
+
# Now just offer it for download to the user
|
416 |
st.download_button(
|
417 |
label="Download BVH",
|
418 |
data=result['bvh_data'],
|
419 |
+
file_name=result['bvh_filename'],
|
420 |
mime="application/octet-stream"
|
421 |
)
|
422 |
else:
|
423 |
+
st.error("Failed to generate BVH file.")
|
424 |
+
|
425 |
+
# If BVH data is in session state (from a previous run), offer it for download
|
426 |
+
if 'bvh_data' in st.session_state and 'bvh_filename' in st.session_state:
|
427 |
+
st.download_button(
|
428 |
+
label="Download BVH",
|
429 |
+
data=st.session_state['bvh_data'],
|
430 |
+
file_name=st.session_state['bvh_filename'],
|
431 |
+
mime="application/octet-stream"
|
432 |
+
)
|
433 |
+
|
434 |
st.markdown('</div>', unsafe_allow_html=True)
|
435 |
|
436 |
if __name__ == "__main__":
|