Alexander Fengler commited on
Commit
2098c98
·
1 Parent(s): 7b5103c

change metrics towards using running averages

Browse files
Files changed (3) hide show
  1. app.py +20 -4
  2. l_alpha.png +0 -0
  3. metrics.py +18 -7
app.py CHANGED
@@ -43,7 +43,6 @@ theme = gr.themes.Soft(
43
  )
44
 
45
 
46
-
47
  def add_border(frame, color = (255, 0, 0), thickness = 2):
48
  # Add a red border to the image
49
  relative = max(frame.shape[0],frame.shape[1])
@@ -157,11 +156,17 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
157
 
158
  iterating, frame = cap.read()
159
  cnt = 0
160
-
 
 
 
 
161
  while iterating:
162
  print('overall count ', cnt)
163
 
164
  if (cnt % skip_frames) == 0:
 
 
165
  frame = cv2.resize(frame, (int(width), int(height)))
166
  print('starting Frame: ', cnt)
167
  # flip frame vertically
@@ -171,16 +176,25 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
171
  top_pred = process_results_for_plot(predictions = result.numpy(),
172
  classes = classes,
173
  class_sizes = class_sizes_lower)
 
 
 
 
 
 
 
 
 
 
174
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
175
  prediction_frame = cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
176
 
177
-
178
  #
179
  #video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
180
 
181
  if cnt*skip_frames %2==0 and top_pred['shark_sighted']:
182
  prediction_frame = cv2.resize(prediction_frame, (int(width), int(height)))
183
- frame =prediction_frame
184
 
185
  if top_pred['shark_sighted']:
186
  frame = draw_cockpit(frame, top_pred,cnt*skip_frames)
@@ -190,6 +204,8 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
190
 
191
 
192
  pred_dashbord = prediction_dashboard(top_pred = top_pred)
 
 
193
  #print('sending frame')
194
  print('finalizing frame:',cnt)
195
  print(pred_dashbord.shape)
 
43
  )
44
 
45
 
 
46
  def add_border(frame, color = (255, 0, 0), thickness = 2):
47
  # Add a red border to the image
48
  relative = max(frame.shape[0],frame.shape[1])
 
156
 
157
  iterating, frame = cap.read()
158
  cnt = 0
159
+ drawn_count = 0
160
+ last_5_shark_detected = np.array([0, 0, 0, 0, 0])
161
+ last_5_human_detected = np.array([0, 0, 0, 0, 0])
162
+ last_5_dangerous_dist = np.array([0, 0, 0, 0, 0])
163
+
164
  while iterating:
165
  print('overall count ', cnt)
166
 
167
  if (cnt % skip_frames) == 0:
168
+ drawn_count += 1
169
+
170
  frame = cv2.resize(frame, (int(width), int(height)))
171
  print('starting Frame: ', cnt)
172
  # flip frame vertically
 
176
  top_pred = process_results_for_plot(predictions = result.numpy(),
177
  classes = classes,
178
  class_sizes = class_sizes_lower)
179
+
180
+ # add to last 5
181
+ last_5_shark_detected[drawn_count % 5] = int(top_pred['shark_n'] > 0)
182
+ last_5_human_detected[drawn_count % 5] = int(top_pred['human_n'] > 0)
183
+ last_5_dangerous_dist[drawn_count % 5] = int(top_pred['dangerous_dist'] > 0)
184
+
185
+ top_pred['shark_sighted'] = int(np.sum(last_5_shark_detected) > 3)
186
+ top_pred['human_sighted'] = int(np.sum(last_5_human_detected) > 3)
187
+ top_pred['dangerous_dist_confirmed'] = int(np.sum(last_5_dangerous_dist) > 3)
188
+
189
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
190
  prediction_frame = cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
191
 
 
192
  #
193
  #video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
194
 
195
  if cnt*skip_frames %2==0 and top_pred['shark_sighted']:
196
  prediction_frame = cv2.resize(prediction_frame, (int(width), int(height)))
197
+ frame = prediction_frame
198
 
199
  if top_pred['shark_sighted']:
200
  frame = draw_cockpit(frame, top_pred,cnt*skip_frames)
 
204
 
205
 
206
  pred_dashbord = prediction_dashboard(top_pred = top_pred)
207
+
208
+ drawn_count += 1
209
  #print('sending frame')
210
  print('finalizing frame:',cnt)
211
  print(pred_dashbord.shape)
l_alpha.png ADDED
metrics.py CHANGED
@@ -213,8 +213,8 @@ def process_results_for_plot(predictions = None, threshold = 0.5, classes = None
213
  'dangerous_dist': False}
214
 
215
  return {'min_dist_str': min_dist['min_dist'],
216
- 'shark_sighted': top_pred['shark_n'] > 0,
217
- 'human_sighted': top_pred['human_n'] > 0,
218
  'shark_n': top_pred['shark_n'],
219
  #'human_n': max(top_pred['human_n'],len(bboxes)-top_pred['shark_n']),
220
  'human_n': top_pred['human_n'],
@@ -227,19 +227,30 @@ def process_results_for_plot(predictions = None, threshold = 0.5, classes = None
227
 
228
  def prediction_dashboard(top_pred = None):
229
  # Bullet points:
230
- shark_sighted = 'Shark Detected: ' + str(top_pred['shark_sighted'])
231
- human_sighted = 'Number of Humans: ' + str(top_pred['human_n'])
 
 
 
 
 
 
 
 
 
 
 
232
 
233
  shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size'])
234
  shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight'])
235
 
236
  danger_level = 'Danger Level: '
237
- danger_level += 'High' if top_pred['dangerous_dist'] else 'Low'
238
 
239
- danger_color = 'orangered' if top_pred['dangerous_dist'] else 'yellowgreen'
240
 
241
  # Create a list of strings to plot
242
- strings = [shark_sighted, human_sighted, shark_size_estimate, shark_weight_estimate, danger_level]
243
 
244
  # Create a figure and axis
245
  fig, ax = plt.subplots()
 
213
  'dangerous_dist': False}
214
 
215
  return {'min_dist_str': min_dist['min_dist'],
216
+ 'shark_suspected': top_pred['shark_n'] > 0,
217
+ 'human_suspected': top_pred['human_n'] > 0,
218
  'shark_n': top_pred['shark_n'],
219
  #'human_n': max(top_pred['human_n'],len(bboxes)-top_pred['shark_n']),
220
  'human_n': top_pred['human_n'],
 
227
 
228
  def prediction_dashboard(top_pred = None):
229
  # Bullet points:
230
+ if top_pred['shark_sighted'] > 0:
231
+ shark_suspected = 'Shark Sighted !'
232
+ elif top_pred['shark_suspected'] > 0:
233
+ shark_suspected = 'Shark Suspected !'
234
+ else:
235
+ shark_suspected = 'No Sharks ...'
236
+
237
+ if top_pred['human_sighted'] > 0:
238
+ human_suspected = 'Human Sighted !'
239
+ elif top_pred['human_suspected'] > 0:
240
+ human_suspected = 'Human Suspected !'
241
+ else:
242
+ human_suspected = 'No Humans ...'
243
 
244
  shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size'])
245
  shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight'])
246
 
247
  danger_level = 'Danger Level: '
248
+ danger_level += 'High' if top_pred['dangerous_dist_confirmed'] else 'Low'
249
 
250
+ danger_color = 'orangered' if top_pred['dangerous_dist_confirmed'] else 'yellowgreen'
251
 
252
  # Create a list of strings to plot
253
+ strings = [shark_suspected, human_suspected, shark_size_estimate, shark_weight_estimate, danger_level]
254
 
255
  # Create a figure and axis
256
  fig, ax = plt.subplots()