piperod91 commited on
Commit
afeb582
·
1 Parent(s): 2098c98

Iterating predictions even when no shark is sighted. Improving messages for info

Browse files
Files changed (2) hide show
  1. app.py +49 -17
  2. metrics.py +5 -3
app.py CHANGED
@@ -60,8 +60,13 @@ def overlay_text_on_image(image, text_list, font=cv2.FONT_HERSHEY_SIMPLEX, font_
60
  y0, dy = margin, int(relative*0.1) # start y position and line gap
61
  for i, line in enumerate(text_list):
62
  y = y0 + i * dy
63
- text_width, _ = cv2.getTextSize(line, font, font_size, font_thickness)[0]
64
- cv2.putText(image, line, (image.shape[1] - text_width - margin, y), font, font_size, color, font_thickness, lineType=cv2.LINE_AA)
 
 
 
 
 
65
  return image
66
 
67
  def overlay_logo(frame,logo, position=(10, 10)):
@@ -108,20 +113,46 @@ def draw_cockpit(frame, top_pred,cnt):
108
  # Bullet points:
109
  high_danger_color = (255,0,0)
110
  low_danger_color = yellowgreen = (154,205,50)
111
- shark_sighted = 'Shark Detected: ' + str(top_pred['shark_sighted'])
112
- human_sighted = 'Number of Humans: ' + str(top_pred['human_n'])
113
- shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size'])
114
- shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  danger_level = 'Danger Level: '
116
- danger_level += 'High' if top_pred['dangerous_dist'] else 'Low'
117
- danger_color = 'orangered' if top_pred['dangerous_dist'] else 'yellowgreen'
 
 
118
  # Create a list of strings to plot
119
- strings = [shark_sighted, human_sighted, shark_size_estimate, shark_weight_estimate, danger_level]
 
 
 
 
 
 
 
 
 
 
120
  relative = max(frame.shape[0],frame.shape[1])
121
- if top_pred['shark_sighted'] and top_pred['dangerous_dist'] and cnt%2 == 0:
122
  frame = add_border(frame, color=high_danger_color, thickness=int(relative*0.025))
123
  frame = add_danger_symbol_from_image(frame, top_pred)
124
- elif top_pred['shark_sighted'] and not top_pred['dangerous_dist'] and cnt%2 == 0:
125
  frame = add_border(frame, color=low_danger_color, thickness=int(relative*0.025))
126
  frame = add_danger_symbol_from_image(frame, top_pred)
127
  else:
@@ -192,13 +223,14 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
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)
201
 
 
202
  frame = cv2.resize(frame, (int(width), int(height)))
203
  video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
204
 
@@ -208,10 +240,10 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
208
  drawn_count += 1
209
  #print('sending frame')
210
  print('finalizing frame:',cnt)
211
- print(pred_dashbord.shape)
212
- print(frame.shape)
213
- print(prediction_frame.shape)
214
- print(width, height)
215
  yield frame , None
216
 
217
  cnt += 1
 
60
  y0, dy = margin, int(relative*0.1) # start y position and line gap
61
  for i, line in enumerate(text_list):
62
  y = y0 + i * dy
63
+ if 'Shark' in line or 'Human' in line :
64
+ text_width, _ = cv2.getTextSize(line, font, font_size*1.5, font_thickness)[0]
65
+ cv2.putText(image, line, (image.shape[1] - text_width - margin, y), font, font_size*1.2, color, font_thickness, lineType=cv2.LINE_AA)
66
+ else:
67
+ text_width, _ = cv2.getTextSize(line, font, font_size, font_thickness)[0]
68
+ cv2.putText(image, line, (image.shape[1] - text_width - margin, y), font, font_size, color, font_thickness, lineType=cv2.LINE_AA)
69
+
70
  return image
71
 
72
  def overlay_logo(frame,logo, position=(10, 10)):
 
113
  # Bullet points:
114
  high_danger_color = (255,0,0)
115
  low_danger_color = yellowgreen = (154,205,50)
116
+
117
+ if top_pred['shark_sighted'] > 0:
118
+ shark_suspected = 'Shark Sighted !'
119
+ elif top_pred['shark_suspected'] > 0:
120
+ shark_suspected = 'Shark Suspected !'
121
+ else:
122
+ shark_suspected = 'No Sharks ...'
123
+
124
+ if top_pred['human_sighted'] > 0:
125
+ human_suspected = 'Human Sighted !'
126
+ elif top_pred['human_suspected'] > 0:
127
+ human_suspected = 'Human Suspected !'
128
+ else:
129
+ human_suspected = 'No Humans ...'
130
+
131
+ shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size']) if top_pred['biggest_shark_size'] else 'Biggest shark size: ...'
132
+ shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight']) if top_pred['biggest_shark_weight'] else 'Biggest shark weight: ...'
133
+
134
  danger_level = 'Danger Level: '
135
+ danger_level += 'High' if top_pred['dangerous_dist_confirmed'] else 'Low'
136
+
137
+ danger_color = 'orangered' if top_pred['dangerous_dist_confirmed'] else 'yellowgreen'
138
+
139
  # Create a list of strings to plot
140
+ strings = [shark_suspected, human_suspected, shark_size_estimate, shark_weight_estimate, danger_level]
141
+
142
+ # shark_sighted = 'Shark Detected: ' + str(top_pred['shark_sighted'])
143
+ # human_sighted = 'Number of Humans: ' + str(top_pred['human_n'])
144
+ # shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size'])
145
+ # shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight'])
146
+ # danger_level = 'Danger Level: '
147
+ # danger_level += 'High' if top_pred['dangerous_dist'] else 'Low'
148
+ # danger_color = 'orangered' if top_pred['dangerous_dist'] else 'yellowgreen'
149
+ # # Create a list of strings to plot
150
+ # strings = [shark_sighted, human_sighted, shark_size_estimate, shark_weight_estimate, danger_level]
151
  relative = max(frame.shape[0],frame.shape[1])
152
+ if top_pred['shark_sighted'] and top_pred['dangerous_dist_confirmed'] and cnt%2 == 0:
153
  frame = add_border(frame, color=high_danger_color, thickness=int(relative*0.025))
154
  frame = add_danger_symbol_from_image(frame, top_pred)
155
+ elif top_pred['shark_sighted'] and not top_pred['dangerous_dist_confirmed'] and cnt%2 == 0:
156
  frame = add_border(frame, color=low_danger_color, thickness=int(relative*0.025))
157
  frame = add_danger_symbol_from_image(frame, top_pred)
158
  else:
 
223
  #
224
  #video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
225
 
226
+ if cnt*skip_frames %2==0:
227
  prediction_frame = cv2.resize(prediction_frame, (int(width), int(height)))
228
  frame = prediction_frame
229
 
230
+ #if top_pred['shark_sighted'] or top_pred['shark_suspected']:
231
  frame = draw_cockpit(frame, top_pred,cnt*skip_frames)
232
 
233
+
234
  frame = cv2.resize(frame, (int(width), int(height)))
235
  video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
236
 
 
240
  drawn_count += 1
241
  #print('sending frame')
242
  print('finalizing frame:',cnt)
243
+ #print(pred_dashbord.shape)
244
+ #print(frame.shape)
245
+ #print(prediction_frame.shape)
246
+ #print(width, height)
247
  yield frame , None
248
 
249
  cnt += 1
metrics.py CHANGED
@@ -50,7 +50,7 @@ def mask_to_boxes(mask):
50
  return ltrb_boxes
51
 
52
 
53
- def get_top_predictions(prediction = None, threshold = 0.1):
54
  if prediction is None:
55
  return None, None
56
  else:
@@ -68,10 +68,12 @@ def add_class_labels(top_pred = {}, class_labels = None):
68
  return top_pred
69
  else:
70
  top_pred['pred_above_thresh_labels'] = [class_labels[x].lower() for x in top_pred['pred_above_thresh']]
 
 
71
  top_pred['any_detection'] = len(top_pred['pred_above_thresh_labels']) > 0
72
  if top_pred['any_detection']:
73
  # Get shark / human / unknown vectors
74
- top_pred['is_shark'] = np.array([1 if 'shark' in x else 0 for x in top_pred['pred_above_thresh_labels']])
75
  top_pred['is_human'] = np.array([1 if 'person' in x else 1 if 'surfer' in x else 0 for x in top_pred['pred_above_thresh_labels']])
76
  top_pred['is_unknown'] = np.array([1 if 'unidentifiable' in x else 0 for x in top_pred['pred_above_thresh_labels']])
77
  # Get shark / human / unknown numbers of detections
@@ -101,7 +103,7 @@ def add_class_sizes(top_pred = {}, class_sizes = None):
101
  else:
102
  size_list.append(tmp_class_sizes['feet'])
103
 
104
- if 'shark' in tmp_pred.lower():
105
  shark_size_list.append(np.mean(tmp_class_sizes['feet']))
106
 
107
  top_pred['pred_above_thresh_sizes'] = size_list
 
50
  return ltrb_boxes
51
 
52
 
53
+ def get_top_predictions(prediction = None, threshold = 0.8):
54
  if prediction is None:
55
  return None, None
56
  else:
 
68
  return top_pred
69
  else:
70
  top_pred['pred_above_thresh_labels'] = [class_labels[x].lower() for x in top_pred['pred_above_thresh']]
71
+ #print(top_pred['pred_above_thresh_labels'])
72
+ #print(top_pred['pred_above_thresh_scores'])
73
  top_pred['any_detection'] = len(top_pred['pred_above_thresh_labels']) > 0
74
  if top_pred['any_detection']:
75
  # Get shark / human / unknown vectors
76
+ top_pred['is_shark'] = np.array([1 if 'shark' in x and 'shadow' not in x else 0 for x in top_pred['pred_above_thresh_labels']])
77
  top_pred['is_human'] = np.array([1 if 'person' in x else 1 if 'surfer' in x else 0 for x in top_pred['pred_above_thresh_labels']])
78
  top_pred['is_unknown'] = np.array([1 if 'unidentifiable' in x else 0 for x in top_pred['pred_above_thresh_labels']])
79
  # Get shark / human / unknown numbers of detections
 
103
  else:
104
  size_list.append(tmp_class_sizes['feet'])
105
 
106
+ if 'shark' in tmp_pred.lower() and 'shadow' not in tmp_pred.lower() :
107
  shark_size_list.append(np.mean(tmp_class_sizes['feet']))
108
 
109
  top_pred['pred_above_thresh_sizes'] = size_list