rekotvd commited on
Commit
79fa216
·
verified ·
1 Parent(s): 9ea686f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -734,22 +734,38 @@ elif input_method == "Upload Video":
734
 
735
  status_text.text("Creating processed video...")
736
 
737
- # Create memory buffer for video data
738
- video_buffer = io.BytesIO()
 
739
 
740
- # Write the processed frames directly to memory
741
- with imageio.get_writer(video_buffer, format='FFMPEG', fps=fps, codec='libx264',
742
- pixelformat='yuv420p', quality=8) as writer:
743
- for frame_data in processed_frames:
744
- # Convert frame from BGR (OpenCV) to RGB (imageio expects RGB)
745
- rgb_frame = cv2.cvtColor(frame_data["frame"], cv2.COLOR_BGR2RGB)
746
- writer.append_data(rgb_frame)
 
747
 
748
- # Reset buffer to beginning for reading
749
- video_buffer.seek(0)
 
750
 
751
- # Display video directly from memory without saving to disk
752
- st.video(video_buffer)
 
 
 
 
 
 
 
 
 
 
 
 
 
753
 
754
  # Analysis complete, display results
755
  st.success(f"Video analysis complete. {len(processed_frames)} frames processed, {len(crash_frames)} crashes detected.")
 
734
 
735
  status_text.text("Creating processed video...")
736
 
737
+ # Create a temporary file for the processed video
738
+ # Using a unique filename in the temporary directory
739
+ temp_output_path = os.path.join(tempfile.gettempdir(), f"processed_video_{int(time.time())}.mp4")
740
 
741
+ # Create video writer
742
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use mp4v codec which is widely compatible
743
+ out = cv2.VideoWriter(
744
+ temp_output_path,
745
+ fourcc,
746
+ fps,
747
+ (frame_width, frame_height)
748
+ )
749
 
750
+ # Write frames to video
751
+ for frame_data in processed_frames:
752
+ out.write(frame_data["frame"])
753
 
754
+ # Release the writer
755
+ out.release()
756
+
757
+ # Read the file into memory
758
+ with open(temp_output_path, 'rb') as f:
759
+ video_bytes = f.read()
760
+
761
+ # Display video from bytes
762
+ st.video(video_bytes)
763
+
764
+ # Clean up the temporary output file
765
+ try:
766
+ os.remove(temp_output_path)
767
+ except:
768
+ pass
769
 
770
  # Analysis complete, display results
771
  st.success(f"Video analysis complete. {len(processed_frames)} frames processed, {len(crash_frames)} crashes detected.")