piperod91 commited on
Commit
70b8d29
·
1 Parent(s): 2c688c3

adding code code for counting, but still buggy so is commented

Browse files
Files changed (2) hide show
  1. inference.py +6 -0
  2. metrics.py +58 -1
inference.py CHANGED
@@ -153,6 +153,7 @@ model = init_detector(config_file, checkpoint_file, device='cuda:0') # or devic
153
  model.dataset_meta['palette'] = model.dataset_meta['palette'] + model.dataset_meta['palette'][-23:]
154
  model.dataset_meta['classes'] = classes
155
  print(model.cfg.visualizer)
 
156
  # init visualizer(run the block only once in jupyter notebook)
157
  visualizer = VISUALIZERS.build(model.cfg.visualizer)
158
  visualizer.img_save_dir ='temp'
@@ -165,12 +166,16 @@ palette = visualizer.dataset_meta.get('palette', None)
165
 
166
  print(len(classes))
167
  print(len(palette))
 
 
 
168
  def inference_frame_serial(image, visualize = True):
169
  #start = time()
170
  result = inference_detector(model, image)
171
  #print(f'inference time: {time()-start}')
172
  # show the results
173
  if visualize:
 
174
  visualizer.add_datasample(
175
  'result',
176
  image,
@@ -178,6 +183,7 @@ def inference_frame_serial(image, visualize = True):
178
  draw_gt = None,
179
  show=False
180
  )
 
181
  frame = visualizer.get_image()
182
  else:
183
  frame = None
 
153
  model.dataset_meta['palette'] = model.dataset_meta['palette'] + model.dataset_meta['palette'][-23:]
154
  model.dataset_meta['classes'] = classes
155
  print(model.cfg.visualizer)
156
+
157
  # init visualizer(run the block only once in jupyter notebook)
158
  visualizer = VISUALIZERS.build(model.cfg.visualizer)
159
  visualizer.img_save_dir ='temp'
 
166
 
167
  print(len(classes))
168
  print(len(palette))
169
+
170
+
171
+
172
  def inference_frame_serial(image, visualize = True):
173
  #start = time()
174
  result = inference_detector(model, image)
175
  #print(f'inference time: {time()-start}')
176
  # show the results
177
  if visualize:
178
+
179
  visualizer.add_datasample(
180
  'result',
181
  image,
 
183
  draw_gt = None,
184
  show=False
185
  )
186
+
187
  frame = visualizer.get_image()
188
  else:
189
  frame = None
metrics.py CHANGED
@@ -1,8 +1,56 @@
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
 
 
 
 
 
 
4
 
5
- def get_top_predictions(prediction = None, threshold = 0.7):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  if prediction is None:
7
  return None, None
8
  else:
@@ -141,9 +189,17 @@ def _calculate_dist_estimate(bbox1, bbox2, labels, class_sizes = None, measureme
141
  # 3 x2 (right, higher pixel number)
142
  # 4 y2 (bottom, higher pixel number)
143
 
 
144
  def process_results_for_plot(predictions = None, threshold = 0.5, classes = None,
145
  class_sizes = None, dangerous_distance = 100):
146
 
 
 
 
 
 
 
 
147
  top_pred = get_top_predictions(predictions, threshold = threshold)
148
  top_pred = add_class_labels(top_pred, class_labels = classes)
149
  top_pred = add_class_sizes(top_pred, class_sizes = class_sizes)
@@ -160,6 +216,7 @@ def process_results_for_plot(predictions = None, threshold = 0.5, classes = None
160
  'shark_sighted': top_pred['shark_n'] > 0,
161
  'human_sighted': top_pred['human_n'] > 0,
162
  'shark_n': top_pred['shark_n'],
 
163
  'human_n': top_pred['human_n'],
164
  'human_and_shark': (top_pred['shark_n'] > 0) and (top_pred['human_n'] > 0),
165
  'dangerous_dist': min_dist['dangerous_dist'],
 
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
 
4
+ import numpy as np
5
+ def mask_to_boxes(mask):
6
+ """ Convert a boolean (Height x Width) mask into a (N x 4) array of NON-OVERLAPPING bounding boxes
7
+ surrounding "islands of truth" in the mask. Boxes indicate the (Left, Top, Right, Bottom) bounds
8
+ of each island, with Right and Bottom being NON-INCLUSIVE (ie they point to the indices AFTER the island).
9
 
10
+ This algorithm (Downright Boxing) does not necessarily put separate connected components into
11
+ separate boxes.
12
+
13
+ You can "cut out" the island-masks with
14
+ boxes = mask_to_boxes(mask)
15
+ island_masks = [mask[t:b, l:r] for l, t, r, b in boxes]
16
+ """
17
+ max_ix = max(s+1 for s in mask.shape) # Use this to represent background
18
+ # These arrays will be used to carry the "box start" indices down and to the right.
19
+ x_ixs = np.full(mask.shape, fill_value=max_ix)
20
+ y_ixs = np.full(mask.shape, fill_value=max_ix)
21
+
22
+ # Propagate the earliest x-index in each segment to the bottom-right corner of the segment
23
+ for i in range(mask.shape[0]):
24
+ x_fill_ix = max_ix
25
+ for j in range(mask.shape[1]):
26
+ above_cell_ix = x_ixs[i-1, j] if i>0 else max_ix
27
+ still_active = mask[i, j] or ((x_fill_ix != max_ix) and (above_cell_ix != max_ix))
28
+ x_fill_ix = min(x_fill_ix, j, above_cell_ix) if still_active else max_ix
29
+ x_ixs[i, j] = x_fill_ix
30
+
31
+ # Propagate the earliest y-index in each segment to the bottom-right corner of the segment
32
+ for j in range(mask.shape[1]):
33
+ y_fill_ix = max_ix
34
+ for i in range(mask.shape[0]):
35
+ left_cell_ix = y_ixs[i, j-1] if j>0 else max_ix
36
+ still_active = mask[i, j] or ((y_fill_ix != max_ix) and (left_cell_ix != max_ix))
37
+ y_fill_ix = min(y_fill_ix, i, left_cell_ix) if still_active else max_ix
38
+ y_ixs[i, j] = y_fill_ix
39
+
40
+ # Find the bottom-right corners of each segment
41
+ new_xstops = np.diff((x_ixs != max_ix).astype(np.int32), axis=1, append=False)==-1
42
+ new_ystops = np.diff((y_ixs != max_ix).astype(np.int32), axis=0, append=False)==-1
43
+ corner_mask = new_xstops & new_ystops
44
+ y_stops, x_stops = np.array(np.nonzero(corner_mask))
45
+
46
+ # Extract the boxes, getting the top-right corners from the index arrays
47
+ x_starts = x_ixs[y_stops, x_stops]
48
+ y_starts = y_ixs[y_stops, x_stops]
49
+ ltrb_boxes = np.hstack([x_starts[:, None], y_starts[:, None], x_stops[:, None]+1, y_stops[:, None]+1])
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:
 
189
  # 3 x2 (right, higher pixel number)
190
  # 4 y2 (bottom, higher pixel number)
191
 
192
+
193
  def process_results_for_plot(predictions = None, threshold = 0.5, classes = None,
194
  class_sizes = None, dangerous_distance = 100):
195
 
196
+ # Attempt to create bounding boxes for humans(surfers) detected.
197
+ # mask = predictions.pred_panoptic_seg.sem_seg[0]
198
+ # print(np.unique(mask))
199
+ # mask = mask >2000
200
+ # bboxes = mask_to_boxes(mask)
201
+ # bboxes = [box for box in bboxes if box[2]*box[3]>100]
202
+
203
  top_pred = get_top_predictions(predictions, threshold = threshold)
204
  top_pred = add_class_labels(top_pred, class_labels = classes)
205
  top_pred = add_class_sizes(top_pred, class_sizes = class_sizes)
 
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'],
221
  'human_and_shark': (top_pred['shark_n'] > 0) and (top_pred['human_n'] > 0),
222
  'dangerous_dist': min_dist['dangerous_dist'],