Muhammad Anas Akhtar commited on
Commit
64ec002
·
verified ·
1 Parent(s): 91c84d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -49,7 +49,7 @@ def draw_bounding_boxes(frame, detections):
49
  frame_with_boxes = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
50
  return frame_with_boxes
51
 
52
- def process_video(video_path):
53
  """
54
  Process the video file and return the path to the processed video
55
  """
@@ -57,36 +57,37 @@ def process_video(video_path):
57
  # Open the video file
58
  cap = cv2.VideoCapture(video_path)
59
  if not cap.isOpened():
60
- return None
61
 
62
  # Get video properties
63
  fps = int(cap.get(cv2.CAP_PROP_FPS))
64
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
65
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
66
-
67
- # Create temporary file for output video
68
- temp_output = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
69
- output_path = temp_output.name
70
- temp_output.close()
71
 
72
- # Initialize video writer
73
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
 
 
 
74
  out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
75
 
 
 
 
76
  frame_count = 0
77
- total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
78
 
79
- # Process every nth frame to speed up processing
80
- process_every_n_frames = 2 # Adjust this value to process more or fewer frames
81
 
82
- while cap.isOpened():
83
  ret, frame = cap.read()
84
  if not ret:
85
  break
86
 
87
  frame_count += 1
88
 
89
- # Only process every nth frame
90
  if frame_count % process_every_n_frames == 0:
91
  # Convert frame to RGB for the model
92
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
@@ -100,56 +101,57 @@ def process_video(video_path):
100
  # Write the frame
101
  out.write(frame)
102
 
103
- # Print progress
104
- progress = (frame_count / total_frames) * 100
105
- print(f"Processing: {progress:.1f}% complete", end='\r')
106
 
107
  # Release everything
108
  cap.release()
109
  out.release()
110
 
 
 
 
 
111
  return output_path
112
 
113
  except Exception as e:
114
  print(f"Error processing video: {str(e)}")
115
- return None
116
 
117
  def detect_objects_in_video(video):
118
  """
119
  Gradio interface function for video object detection
120
  """
121
  if video is None:
122
- return None
123
 
124
  try:
125
  # Process the video
126
  output_path = process_video(video)
127
- if output_path is None:
128
- return None
129
-
130
  return output_path
131
 
132
  except Exception as e:
133
- print(f"Error during video processing: {str(e)}")
134
- return None
135
 
136
  # Create the Gradio interface
137
  demo = gr.Interface(
138
  fn=detect_objects_in_video,
139
  inputs=[
140
- gr.Video(label="Upload Video")
141
  ],
142
  outputs=[
143
- gr.Video(label="Processed Video")
144
  ],
145
- title="Video Object Detection",
146
  description="""
147
  Upload a video to detect and track objects within it.
148
  The application will process the video and draw bounding boxes around detected objects
149
  with their labels and confidence scores.
150
  Note: Processing may take some time depending on the video length.
151
- """
 
 
152
  )
153
 
154
  if __name__ == "__main__":
155
- demo.launch()
 
49
  frame_with_boxes = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
50
  return frame_with_boxes
51
 
52
+ def process_video(video_path, progress=gr.Progress()):
53
  """
54
  Process the video file and return the path to the processed video
55
  """
 
57
  # Open the video file
58
  cap = cv2.VideoCapture(video_path)
59
  if not cap.isOpened():
60
+ raise ValueError("Could not open video file")
61
 
62
  # Get video properties
63
  fps = int(cap.get(cv2.CAP_PROP_FPS))
64
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
65
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
66
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
 
 
 
 
67
 
68
+ # Create output video file
69
+ output_path = os.path.join(tempfile.gettempdir(), 'output_video.mp4')
70
+
71
+ # Initialize video writer with H264 codec
72
+ fourcc = cv2.VideoWriter_fourcc(*'avc1')
73
  out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
74
 
75
+ if not out.isOpened():
76
+ raise ValueError("Could not create output video file")
77
+
78
  frame_count = 0
79
+ process_every_n_frames = 1 # Process every frame
80
 
81
+ progress(0, desc="Processing video...")
 
82
 
83
+ while True:
84
  ret, frame = cap.read()
85
  if not ret:
86
  break
87
 
88
  frame_count += 1
89
 
90
+ # Process frame
91
  if frame_count % process_every_n_frames == 0:
92
  # Convert frame to RGB for the model
93
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 
101
  # Write the frame
102
  out.write(frame)
103
 
104
+ # Update progress
105
+ progress((frame_count / total_frames), desc=f"Processing frame {frame_count}/{total_frames}")
 
106
 
107
  # Release everything
108
  cap.release()
109
  out.release()
110
 
111
+ # Verify the output file exists and has size
112
+ if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
113
+ raise ValueError("Output video file is empty or was not created")
114
+
115
  return output_path
116
 
117
  except Exception as e:
118
  print(f"Error processing video: {str(e)}")
119
+ raise gr.Error(f"Error processing video: {str(e)}")
120
 
121
  def detect_objects_in_video(video):
122
  """
123
  Gradio interface function for video object detection
124
  """
125
  if video is None:
126
+ raise gr.Error("Please upload a video file")
127
 
128
  try:
129
  # Process the video
130
  output_path = process_video(video)
 
 
 
131
  return output_path
132
 
133
  except Exception as e:
134
+ raise gr.Error(f"Error during video processing: {str(e)}")
 
135
 
136
  # Create the Gradio interface
137
  demo = gr.Interface(
138
  fn=detect_objects_in_video,
139
  inputs=[
140
+ gr.Video(label="Upload Video", format="mp4")
141
  ],
142
  outputs=[
143
+ gr.Video(label="Processed Video", format="mp4")
144
  ],
145
+ title="@GenAILearniverse Project: Video Object Detection",
146
  description="""
147
  Upload a video to detect and track objects within it.
148
  The application will process the video and draw bounding boxes around detected objects
149
  with their labels and confidence scores.
150
  Note: Processing may take some time depending on the video length.
151
+ """,
152
+ examples=[],
153
+ cache_examples=False
154
  )
155
 
156
  if __name__ == "__main__":
157
+ demo.launch()