Update app.py
Browse files
app.py
CHANGED
|
@@ -671,7 +671,6 @@ elif input_method == "Upload Video":
|
|
| 671 |
tfile_path = tfile.name
|
| 672 |
tfile.close()
|
| 673 |
|
| 674 |
-
# Process button
|
| 675 |
if st.sidebar.button("Process Video"):
|
| 676 |
try:
|
| 677 |
cap = cv2.VideoCapture(tfile_path)
|
|
@@ -722,7 +721,6 @@ elif input_method == "Upload Video":
|
|
| 722 |
|
| 723 |
cap.release()
|
| 724 |
|
| 725 |
-
# Write processed frames to a new video file
|
| 726 |
status_text.text("Creating processed video...")
|
| 727 |
output_video_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
| 728 |
|
|
@@ -730,41 +728,30 @@ elif input_method == "Upload Video":
|
|
| 730 |
first_frame = processed_frames[0]["frame"]
|
| 731 |
h, w = first_frame.shape[:2]
|
| 732 |
|
| 733 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v') #
|
| 734 |
out = cv2.VideoWriter(output_video_path, fourcc, fps, (w, h))
|
| 735 |
|
| 736 |
for frame_data in processed_frames:
|
| 737 |
out.write(frame_data["frame"])
|
| 738 |
|
| 739 |
out.release()
|
| 740 |
-
time.sleep(0.5) #
|
| 741 |
|
| 742 |
try:
|
| 743 |
with open(output_video_path, 'rb') as video_file:
|
| 744 |
video_bytes = video_file.read()
|
| 745 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 746 |
status_text.empty()
|
| 747 |
progress_bar.empty()
|
| 748 |
-
|
| 749 |
-
if "huggingface.co" in os.environ.get("HOST", "") or "SPACE_ID" in os.environ:
|
| 750 |
-
# Display video using HTML for Hugging Face Spaces
|
| 751 |
-
video_base64 = base64.b64encode(video_bytes).decode("utf-8")
|
| 752 |
-
st.markdown(
|
| 753 |
-
f"""
|
| 754 |
-
<video width="100%" controls autoplay>
|
| 755 |
-
<source src="data:video/mp4;base64,{video_base64}" type="video/mp4">
|
| 756 |
-
Your browser does not support the video tag.
|
| 757 |
-
</video>
|
| 758 |
-
""",
|
| 759 |
-
unsafe_allow_html=True
|
| 760 |
-
)
|
| 761 |
-
else:
|
| 762 |
-
# Local streamlit display
|
| 763 |
-
st.video(video_bytes)
|
| 764 |
|
| 765 |
except Exception as e:
|
| 766 |
-
st.error(f"Error
|
| 767 |
-
|
| 768 |
st.success(f"Video analysis complete. {len(processed_frames)} frames processed, {len(crash_frames)} crashes detected.")
|
| 769 |
|
| 770 |
if crash_frames:
|
|
@@ -825,6 +812,7 @@ elif input_method == "Upload Video":
|
|
| 825 |
except:
|
| 826 |
pass
|
| 827 |
|
|
|
|
| 828 |
st.markdown("---")
|
| 829 |
col1, col2, col3, col4 = st.columns(4)
|
| 830 |
|
|
|
|
| 671 |
tfile_path = tfile.name
|
| 672 |
tfile.close()
|
| 673 |
|
|
|
|
| 674 |
if st.sidebar.button("Process Video"):
|
| 675 |
try:
|
| 676 |
cap = cv2.VideoCapture(tfile_path)
|
|
|
|
| 721 |
|
| 722 |
cap.release()
|
| 723 |
|
|
|
|
| 724 |
status_text.text("Creating processed video...")
|
| 725 |
output_video_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
| 726 |
|
|
|
|
| 728 |
first_frame = processed_frames[0]["frame"]
|
| 729 |
h, w = first_frame.shape[:2]
|
| 730 |
|
| 731 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'mp4v' for compatibility
|
| 732 |
out = cv2.VideoWriter(output_video_path, fourcc, fps, (w, h))
|
| 733 |
|
| 734 |
for frame_data in processed_frames:
|
| 735 |
out.write(frame_data["frame"])
|
| 736 |
|
| 737 |
out.release()
|
| 738 |
+
time.sleep(0.5) # Ensure the file is fully flushed
|
| 739 |
|
| 740 |
try:
|
| 741 |
with open(output_video_path, 'rb') as video_file:
|
| 742 |
video_bytes = video_file.read()
|
| 743 |
|
| 744 |
+
# Generate download link
|
| 745 |
+
b64 = base64.b64encode(video_bytes).decode()
|
| 746 |
+
href = f'<a href="data:video/mp4;base64,{b64}" download="processed_video.mp4">📥 Click here to download the processed video</a>'
|
| 747 |
+
|
| 748 |
status_text.empty()
|
| 749 |
progress_bar.empty()
|
| 750 |
+
st.markdown(href, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
|
| 752 |
except Exception as e:
|
| 753 |
+
st.error(f"Error preparing video download: {e}")
|
| 754 |
+
|
| 755 |
st.success(f"Video analysis complete. {len(processed_frames)} frames processed, {len(crash_frames)} crashes detected.")
|
| 756 |
|
| 757 |
if crash_frames:
|
|
|
|
| 812 |
except:
|
| 813 |
pass
|
| 814 |
|
| 815 |
+
|
| 816 |
st.markdown("---")
|
| 817 |
col1, col2, col3, col4 = st.columns(4)
|
| 818 |
|