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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -695,8 +695,9 @@ 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
699
- output_path = f"{temp_video_path}_processed.mp4"
 
700
 
701
  # Open the video file
702
  cap = cv2.VideoCapture(temp_video_path)
@@ -925,16 +926,22 @@ def process_video_file(video_file, analysis_types):
925
  progress_bar.empty()
926
  status_text.empty()
927
 
 
 
 
 
928
  # Read the processed video as bytes for download
929
  with open(output_path, 'rb') as file:
930
  processed_video_bytes = file.read()
931
 
932
  # Clean up temporary files
933
- os.unlink(temp_video_path)
934
- os.unlink(output_path)
 
 
 
935
 
936
  # Return both the video and the detection statistics
937
- processed_video_bytes = processed_video_bytes
938
  results = {"detection_stats": detection_stats}
939
 
940
  # Store results in session state for chatbot context
@@ -950,10 +957,15 @@ def process_video_file(video_file, analysis_types):
950
  cap.release()
951
  if 'out' in locals():
952
  out.release()
953
- os.unlink(temp_video_path)
 
 
 
954
  if os.path.exists(output_path):
955
  os.unlink(output_path)
956
- raise e
 
 
957
 
958
  def load_bigquery_table(dataset_id, table_id, limit=1000):
959
  """Load data directly from an existing BigQuery table"""
 
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)
 
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}
946
 
947
  # Store results in session state for chatbot context
 
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"""