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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -14
app.py CHANGED
@@ -49,6 +49,37 @@ 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, progress=gr.Progress()):
53
  """
54
  Process the video file and return the path to the processed video
@@ -59,21 +90,17 @@ def process_video(video_path, progress=gr.Progress()):
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
@@ -137,10 +164,10 @@ def detect_objects_in_video(video):
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="""
 
49
  frame_with_boxes = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
50
  return frame_with_boxes
51
 
52
+ def create_output_writer(cap, output_path):
53
+ """
54
+ Create video writer with different codecs, trying multiple options
55
+ """
56
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
57
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
58
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
59
+
60
+ # Try different codecs
61
+ codecs = [
62
+ ('mp4v', '.mp4'),
63
+ ('avc1', '.mp4'),
64
+ ('XVID', '.avi'),
65
+ ('MJPG', '.avi')
66
+ ]
67
+
68
+ for codec, ext in codecs:
69
+ try:
70
+ output_file = os.path.splitext(output_path)[0] + ext
71
+ fourcc = cv2.VideoWriter_fourcc(*codec)
72
+ out = cv2.VideoWriter(output_file, fourcc, fps, (frame_width, frame_height))
73
+
74
+ if out is not None and out.isOpened():
75
+ return out, output_file
76
+
77
+ except Exception as e:
78
+ print(f"Failed with codec {codec}: {str(e)}")
79
+ continue
80
+
81
+ raise ValueError("Could not initialize any video codec")
82
+
83
  def process_video(video_path, progress=gr.Progress()):
84
  """
85
  Process the video file and return the path to the processed video
 
90
  if not cap.isOpened():
91
  raise ValueError("Could not open video file")
92
 
 
 
 
 
93
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
94
 
95
+ # Create output directory if it doesn't exist
96
+ output_dir = os.path.join(os.path.expanduser("~"), "Videos", "ObjectDetection")
97
+ os.makedirs(output_dir, exist_ok=True)
98
 
99
+ # Create output path
100
+ output_path = os.path.join(output_dir, "output_video.mp4")
101
+
102
+ # Initialize video writer
103
+ out, output_path = create_output_writer(cap, output_path)
 
104
 
105
  frame_count = 0
106
  process_every_n_frames = 1 # Process every frame
 
164
  demo = gr.Interface(
165
  fn=detect_objects_in_video,
166
  inputs=[
167
+ gr.Video(label="Upload Video")
168
  ],
169
  outputs=[
170
+ gr.Video(label="Processed Video")
171
  ],
172
  title="@GenAILearniverse Project: Video Object Detection",
173
  description="""