CosmickVisions commited on
Commit
cc984ab
·
verified ·
1 Parent(s): 90c1601

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -40
app.py CHANGED
@@ -695,9 +695,8 @@ def process_video_file(video_file, analysis_types):
695
  temp_file.write(video_file.read())
696
  temp_video_path = temp_file.name
697
 
698
- # Create a temp file for the output video with more reliable path handling
699
- # Use Path from pathlib to ensure proper path handling across platforms
700
- output_path = str(Path(tempfile.gettempdir()) / f"{Path(temp_video_path).stem}_processed.mp4")
701
 
702
  # Open the video file
703
  cap = cv2.VideoCapture(temp_video_path)
@@ -723,19 +722,8 @@ def process_video_file(video_file, analysis_types):
723
  output_fps = fps * 0.6
724
  st.info(f"Output video will be slowed down to {output_fps:.1f} FPS (60% of original speed) for better visualization.")
725
 
726
- # Create video writer with H.264 codec for better compatibility
727
- # Try different codec options based on platform compatibility
728
- try:
729
- # First try H.264 codec (most widely compatible)
730
- if cv2.__version__.startswith('4'):
731
- fourcc = cv2.VideoWriter_fourcc(*'avc1') # H.264 codec
732
- else:
733
- # Fallback to older codec naming
734
- fourcc = cv2.VideoWriter_fourcc(*'H264')
735
- except Exception:
736
- # If H.264 isn't available, fall back to MP4V
737
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
738
-
739
  out = cv2.VideoWriter(output_path, fourcc, output_fps, (width, height), isColor=True)
740
 
741
  # Process every Nth frame to reduce API calls but increase from 10 to 5 for more detail
@@ -926,20 +914,13 @@ def process_video_file(video_file, analysis_types):
926
  progress_bar.empty()
927
  status_text.empty()
928
 
929
- # Verify the file exists before attempting to read it
930
- if not os.path.exists(output_path):
931
- raise FileNotFoundError(f"Processed video file not found at {output_path}")
932
-
933
  # Read the processed video as bytes for download
934
  with open(output_path, 'rb') as file:
935
  processed_video_bytes = file.read()
936
 
937
  # Clean up temporary files
938
- try:
939
- os.unlink(temp_video_path)
940
- os.unlink(output_path)
941
- except Exception as e:
942
- st.warning(f"Warning: Could not delete temporary files: {str(e)}")
943
 
944
  # Return both the video and the detection statistics
945
  results = {"detection_stats": detection_stats}
@@ -957,15 +938,10 @@ def process_video_file(video_file, analysis_types):
957
  cap.release()
958
  if 'out' in locals():
959
  out.release()
960
-
961
- # Only try to delete files if they exist
962
- if os.path.exists(temp_video_path):
963
- os.unlink(temp_video_path)
964
  if os.path.exists(output_path):
965
  os.unlink(output_path)
966
-
967
- # Raise the error with more context
968
- raise Exception(f"Error processing video: {str(e)} (input: {temp_video_path}, output: {output_path})")
969
 
970
  def load_bigquery_table(dataset_id, table_id, limit=1000):
971
  """Load data directly from an existing BigQuery table"""
@@ -1703,14 +1679,8 @@ def main():
1703
  mime="video/mp4"
1704
  )
1705
 
1706
- # Display processed video with fallback options
1707
- st.markdown("### Processed Video")
1708
- st.markdown("If the video below doesn't play correctly, please use the download button above to view it locally.")
1709
- try:
1710
- st.video(processed_video)
1711
- except Exception as e:
1712
- st.error(f"Error displaying video in browser: {str(e)}")
1713
- st.info("Please download the video using the button above and play it in your local media player.")
1714
 
1715
  # Show detailed analysis results
1716
  st.markdown("### Detailed Analysis Results")
 
695
  temp_file.write(video_file.read())
696
  temp_video_path = temp_file.name
697
 
698
+ # Create a temp file for the output video
699
+ output_path = f"{temp_video_path}_processed.mp4"
 
700
 
701
  # Open the video file
702
  cap = cv2.VideoCapture(temp_video_path)
 
722
  output_fps = fps * 0.6
723
  st.info(f"Output video will be slowed down to {output_fps:.1f} FPS (60% of original speed) for better visualization.")
724
 
725
+ # Create video writer with higher quality settings
726
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # MP4 codec
 
 
 
 
 
 
 
 
 
 
 
727
  out = cv2.VideoWriter(output_path, fourcc, output_fps, (width, height), isColor=True)
728
 
729
  # Process every Nth frame to reduce API calls but increase from 10 to 5 for more detail
 
914
  progress_bar.empty()
915
  status_text.empty()
916
 
 
 
 
 
917
  # Read the processed video as bytes for download
918
  with open(output_path, 'rb') as file:
919
  processed_video_bytes = file.read()
920
 
921
  # Clean up temporary files
922
+ os.unlink(temp_video_path)
923
+ os.unlink(output_path)
 
 
 
924
 
925
  # Return both the video and the detection statistics
926
  results = {"detection_stats": detection_stats}
 
938
  cap.release()
939
  if 'out' in locals():
940
  out.release()
941
+ os.unlink(temp_video_path)
 
 
 
942
  if os.path.exists(output_path):
943
  os.unlink(output_path)
944
+ raise e
 
 
945
 
946
  def load_bigquery_table(dataset_id, table_id, limit=1000):
947
  """Load data directly from an existing BigQuery table"""
 
1679
  mime="video/mp4"
1680
  )
1681
 
1682
+ # Display processed video
1683
+ st.video(processed_video)
 
 
 
 
 
 
1684
 
1685
  # Show detailed analysis results
1686
  st.markdown("### Detailed Analysis Results")