PrashanthB461 commited on
Commit
068739f
·
verified ·
1 Parent(s): 4c4b661

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -10
app.py CHANGED
@@ -505,16 +505,39 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
505
 
506
  def process_video(video_data):
507
  """Process video to detect safety violations"""
 
508
  try:
509
  # Validate video data
510
  if not video_data:
511
  raise ValueError("Empty video data provided.")
512
-
513
- # Save video to a temporary file
514
- video_fd, video_path = tempfile.mkstemp(suffix=".mp4", dir=TEMP_DIR)
515
- with os.fdopen(video_fd, "wb") as f:
516
- f.write(video_data)
517
- logger.info(f"Video saved: {video_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
 
519
  # Open video with OpenCV
520
  cap = cv2.VideoCapture(video_path)
@@ -676,8 +699,6 @@ def process_video(video_data):
676
 
677
  # Ensure resources are released
678
  cap.release()
679
- if os.path.exists(video_path):
680
- os.remove(video_path)
681
 
682
  processing_time = time.time() - start_time
683
  logger.info(f"Processing complete in {processing_time:.2f}s")
@@ -754,13 +775,19 @@ def process_video(video_data):
754
 
755
  except Exception as e:
756
  logger.error(f"Error processing video: {str(e)}", exc_info=True)
757
- if 'video_path' in locals() and os.path.exists(video_path):
758
- os.remove(video_path)
759
  yield f"Error processing video: {str(e)}", "", "", "", ""
760
  finally:
 
 
 
 
 
 
 
761
  # Clean up temporary directory
762
  if os.path.exists(TEMP_DIR):
763
  shutil.rmtree(TEMP_DIR, ignore_errors=True)
 
764
 
765
  def gradio_interface(video_file):
766
  """Gradio interface for the video processing"""
@@ -768,13 +795,20 @@ def gradio_interface(video_file):
768
  return "No file uploaded.", "", "No file uploaded.", "", ""
769
 
770
  try:
 
771
  with open(video_file, "rb") as f:
772
  video_data = f.read()
773
 
 
 
 
 
 
774
  # Validate FFmpeg availability
775
  if not FFMPEG_AVAILABLE:
776
  return "FFmpeg is not available in the environment. Please install FFmpeg to process videos.", "", "", "", ""
777
 
 
778
  for status, score, snapshots_text, record_id, details_url in process_video(video_data):
779
  yield status, score, snapshots_text, record_id, details_url
780
 
 
505
 
506
  def process_video(video_data):
507
  """Process video to detect safety violations"""
508
+ video_path = None # Initialize video_path for cleanup
509
  try:
510
  # Validate video data
511
  if not video_data:
512
  raise ValueError("Empty video data provided.")
513
+
514
+ # Log the size of the video data
515
+ logger.info(f"Received video data size: {len(video_data)} bytes")
516
+ if len(video_data) == 0:
517
+ raise ValueError("Video data is empty.")
518
+
519
+ # Save video to a temporary file using NamedTemporaryFile
520
+ with tempfile.NamedTemporaryFile(suffix=".mp4", dir=TEMP_DIR, delete=False) as temp_file:
521
+ temp_file.write(video_data)
522
+ temp_file.flush() # Ensure all data is written to disk
523
+ video_path = temp_file.name
524
+ logger.info(f"Video saved to temporary file: {video_path}")
525
+
526
+ # Verify the file exists and has content
527
+ if not os.path.exists(video_path):
528
+ raise FileNotFoundError(f"Temporary video file not found: {video_path}")
529
+ file_size = os.path.getsize(video_path)
530
+ if file_size == 0:
531
+ raise ValueError(f"Temporary video file is empty: {video_path}")
532
+ logger.info(f"Temporary video file size: {file_size} bytes")
533
+
534
+ # Ensure the file is readable
535
+ try:
536
+ with open(video_path, "rb") as f:
537
+ f.read(1) # Test read access
538
+ logger.info(f"Temporary video file is readable: {video_path}")
539
+ except Exception as e:
540
+ raise IOError(f"Cannot read temporary video file {video_path}: {str(e)}")
541
 
542
  # Open video with OpenCV
543
  cap = cv2.VideoCapture(video_path)
 
699
 
700
  # Ensure resources are released
701
  cap.release()
 
 
702
 
703
  processing_time = time.time() - start_time
704
  logger.info(f"Processing complete in {processing_time:.2f}s")
 
775
 
776
  except Exception as e:
777
  logger.error(f"Error processing video: {str(e)}", exc_info=True)
 
 
778
  yield f"Error processing video: {str(e)}", "", "", "", ""
779
  finally:
780
+ # Clean up the temporary video file
781
+ if video_path and os.path.exists(video_path):
782
+ try:
783
+ os.remove(video_path)
784
+ logger.info(f"Cleaned up temporary video file: {video_path}")
785
+ except Exception as e:
786
+ logger.error(f"Failed to clean up temporary video file {video_path}: {e}")
787
  # Clean up temporary directory
788
  if os.path.exists(TEMP_DIR):
789
  shutil.rmtree(TEMP_DIR, ignore_errors=True)
790
+ logger.info(f"Cleaned up temporary directory: {TEMP_DIR}")
791
 
792
  def gradio_interface(video_file):
793
  """Gradio interface for the video processing"""
 
795
  return "No file uploaded.", "", "No file uploaded.", "", ""
796
 
797
  try:
798
+ # Read the video file
799
  with open(video_file, "rb") as f:
800
  video_data = f.read()
801
 
802
+ # Log the size of the uploaded video file
803
+ logger.info(f"Uploaded video file size: {len(video_data)} bytes")
804
+ if len(video_data) == 0:
805
+ return "Uploaded video file is empty.", "", "", "", ""
806
+
807
  # Validate FFmpeg availability
808
  if not FFMPEG_AVAILABLE:
809
  return "FFmpeg is not available in the environment. Please install FFmpeg to process videos.", "", "", "", ""
810
 
811
+ # Process the video
812
  for status, score, snapshots_text, record_id, details_url in process_video(video_data):
813
  yield status, score, snapshots_text, record_id, details_url
814