rosebe commited on
Commit
30fec0d
·
1 Parent(s): 9e299ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -130,7 +130,7 @@ import cv2
130
  import os
131
 
132
  # Load the model
133
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
134
 
135
  def detect_image(image):
136
  results = model(image)
@@ -145,12 +145,15 @@ def detect_video(video_path):
145
  os.makedirs(frames_dir, exist_ok=True)
146
 
147
  frame_count = 0
 
148
  while True:
149
  success, frame = video.read()
150
  if not success:
151
  break
152
- frame_output_path = os.path.join(frames_dir, f'frame_{frame_count:04d}.jpg')
153
- cv2.imwrite(frame_output_path, frame)
 
 
154
  frame_count += 1
155
 
156
  video.release()
@@ -160,7 +163,7 @@ def detect_video(video_path):
160
  results_dir = 'results'
161
  os.makedirs(results_dir, exist_ok=True)
162
 
163
- for i in range(frame_count):
164
  frame_path = os.path.join(frames_dir, f'frame_{i:04d}.jpg')
165
  frame = cv2.imread(frame_path)
166
  results = model(frame)
@@ -175,7 +178,7 @@ def detect_video(video_path):
175
 
176
  video_output_path = 'output_video.mp4' # Replace with your desired output video path
177
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can change the codec as needed
178
- video_writer = cv2.VideoWriter(video_output_path, fourcc, frame_rate, (width, height))
179
 
180
  for frame_file in frame_files:
181
  frame_path = os.path.join(results_dir, frame_file)
@@ -213,3 +216,4 @@ tabbed_interface = gr.TabbedInterface(interfaces, ["Image", "Video"])
213
 
214
  # Launch the tabbed interface
215
  tabbed_interface.launch(debug=True)
 
 
130
  import os
131
 
132
  # Load the model
133
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
134
 
135
  def detect_image(image):
136
  results = model(image)
 
145
  os.makedirs(frames_dir, exist_ok=True)
146
 
147
  frame_count = 0
148
+ process_frame_count = 0
149
  while True:
150
  success, frame = video.read()
151
  if not success:
152
  break
153
+ if frame_count % 5 == 0: # Process every 5th frame (adjust as needed)
154
+ frame_output_path = os.path.join(frames_dir, f'frame_{process_frame_count:04d}.jpg')
155
+ cv2.imwrite(frame_output_path, frame)
156
+ process_frame_count += 1
157
  frame_count += 1
158
 
159
  video.release()
 
163
  results_dir = 'results'
164
  os.makedirs(results_dir, exist_ok=True)
165
 
166
+ for i in range(process_frame_count):
167
  frame_path = os.path.join(frames_dir, f'frame_{i:04d}.jpg')
168
  frame = cv2.imread(frame_path)
169
  results = model(frame)
 
178
 
179
  video_output_path = 'output_video.mp4' # Replace with your desired output video path
180
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can change the codec as needed
181
+ video_writer = cv2.VideoWriter(video_output_path, fourcc, frame_rate/5, (width, height)) # Adjust frame rate
182
 
183
  for frame_file in frame_files:
184
  frame_path = os.path.join(results_dir, frame_file)
 
216
 
217
  # Launch the tabbed interface
218
  tabbed_interface.launch(debug=True)
219
+