rekotvd commited on
Commit
f9d8a61
·
verified ·
1 Parent(s): 0c9e0c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -38
app.py CHANGED
@@ -734,22 +734,15 @@ elif input_method == "Upload Video":
734
 
735
  status_text.text("Creating processed video...")
736
 
737
- # Create a file in the Streamlit static directory (will be accessible via URL)
738
- # This is a directory that Streamlit serves statically
739
  timestamp = int(time.time())
740
  filename = f"processed_video_{timestamp}.mp4"
 
741
 
742
- # For Hugging Face Spaces, use a publicly accessible directory
743
- # First try to create within the app directory
744
- static_dir = "static"
745
- os.makedirs(static_dir, exist_ok=True)
746
-
747
- output_path = os.path.join(static_dir, filename)
748
-
749
- # Create video writer with H.264 codec (most compatible)
750
- fourcc = cv2.VideoWriter_fourcc(*'avc1')
751
  out = cv2.VideoWriter(
752
- output_path,
753
  fourcc,
754
  fps,
755
  (frame_width, frame_height)
@@ -762,39 +755,48 @@ elif input_method == "Upload Video":
762
  # Release the writer
763
  out.release()
764
 
765
- # Try multiple approaches to display the video
766
-
767
- try:
768
- # First attempt: Try to display using st.video
769
- with open(output_path, 'rb') as video_file:
770
  video_bytes = video_file.read()
771
- st.video(video_bytes)
772
- except Exception as e:
773
- st.warning(f"Could not display video directly: {e}")
774
 
775
- # Fallback: Provide a download link
776
- with open(output_path, "rb") as file:
777
- btn = st.download_button(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778
  label="Download Processed Video",
779
- data=file,
780
  file_name=filename,
781
  mime="video/mp4"
782
  )
 
 
 
 
 
 
783
 
784
- # Also display frames with a slider as another option
785
- st.write("Preview frames:")
786
-
787
- # Create a slider to navigate through frames
788
- selected_frame = st.slider("Navigate Frames", 0, len(processed_frames)-1, 0)
789
-
790
- # Display the selected frame
791
- st.image(processed_frames[selected_frame]["frame"],
792
- channels="BGR",
793
- use_column_width=True,
794
- caption=f"Frame {selected_frame}")
795
-
796
- # Analysis complete, display results
797
- st.success(f"Video analysis complete. {len(processed_frames)} frames processed, {len(crash_frames)} crashes detected.")
798
 
799
  # After video display, process the last detected crash (if any)
800
  if crash_frames:
@@ -846,6 +848,12 @@ elif input_method == "Upload Video":
846
  else:
847
  st.info("No crashes detected in this video.")
848
 
 
 
 
 
 
 
849
  except Exception as e:
850
  st.error(f"Error processing video: {e}")
851
  st.exception(e) # Show the full traceback for debugging
 
734
 
735
  status_text.text("Creating processed video...")
736
 
737
+ # Create a temporary file in the system's temp directory
 
738
  timestamp = int(time.time())
739
  filename = f"processed_video_{timestamp}.mp4"
740
+ temp_output_path = os.path.join(tempfile.gettempdir(), filename)
741
 
742
+ # Create video writer
743
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use mp4v codec which is widely compatible
 
 
 
 
 
 
 
744
  out = cv2.VideoWriter(
745
+ temp_output_path,
746
  fourcc,
747
  fps,
748
  (frame_width, frame_height)
 
755
  # Release the writer
756
  out.release()
757
 
758
+ # Verify file exists before proceeding
759
+ if os.path.exists(temp_output_path) and os.path.getsize(temp_output_path) > 0:
760
+ # Read the processed video into memory
761
+ with open(temp_output_path, 'rb') as video_file:
 
762
  video_bytes = video_file.read()
 
 
 
763
 
764
+ # Create a frame display interface
765
+ display_option = st.radio(
766
+ "Display Option",
767
+ ["View Frames", "Download Video"],
768
+ horizontal=True
769
+ )
770
+
771
+ if display_option == "View Frames":
772
+ # Display frames with a slider
773
+ st.write("Navigate through processed frames:")
774
+ selected_frame = st.slider("Frame", 0, len(processed_frames)-1, 0)
775
+ st.image(
776
+ processed_frames[selected_frame]["frame"],
777
+ channels="BGR",
778
+ use_column_width=True,
779
+ caption=f"Frame {selected_frame}"
780
+ )
781
+ else:
782
+ # Provide a download button for the video
783
+ st.download_button(
784
  label="Download Processed Video",
785
+ data=video_bytes,
786
  file_name=filename,
787
  mime="video/mp4"
788
  )
789
+
790
+ # Try to display the video directly as well
791
+ try:
792
+ st.video(video_bytes)
793
+ except:
794
+ st.info("Video preview not available. Please use the download button.")
795
 
796
+ # Analysis complete, display results
797
+ st.success(f"Video analysis complete. {len(processed_frames)} frames processed, {len(crash_frames)} crashes detected.")
798
+ else:
799
+ st.error(f"Failed to create video file at {temp_output_path}")
 
 
 
 
 
 
 
 
 
 
800
 
801
  # After video display, process the last detected crash (if any)
802
  if crash_frames:
 
848
  else:
849
  st.info("No crashes detected in this video.")
850
 
851
+ # Clean up temporary file
852
+ try:
853
+ if os.path.exists(temp_output_path):
854
+ os.remove(temp_output_path)
855
+ except:
856
+ pass
857
  except Exception as e:
858
  st.error(f"Error processing video: {e}")
859
  st.exception(e) # Show the full traceback for debugging