piperod91 commited on
Commit
761af93
2 Parent(s): c30af9b a10edf8

Merge branch 'alex_dev' of https://huggingface.co/spaces/piperod91/shark_detection into alex_dev

Browse files
Files changed (3) hide show
  1. app.py +9 -0
  2. inference.py +52 -0
  3. output.mp4 +0 -0
app.py CHANGED
@@ -1,6 +1,8 @@
1
 
 
2
  import subprocess
3
  import os
 
4
  if os.getenv('SYSTEM') == 'spaces':
5
 
6
  subprocess.call('pip install -U openmim'.split())
@@ -14,6 +16,9 @@ if os.getenv('SYSTEM') == 'spaces':
14
 
15
  import gradio as gr
16
 
 
 
 
17
  from huggingface_hub import snapshot_download
18
  import cv2
19
  import dotenv
@@ -28,6 +33,10 @@ from inference import classes
28
  from inference import class_sizes_lower
29
  from metrics import process_results_for_plot
30
  from metrics import prediction_dashboard
 
 
 
 
31
  import os
32
  import pathlib
33
  import multiprocessing as mp
 
1
 
2
+
3
  import subprocess
4
  import os
5
+ import os
6
  if os.getenv('SYSTEM') == 'spaces':
7
 
8
  subprocess.call('pip install -U openmim'.split())
 
16
 
17
  import gradio as gr
18
 
19
+ import gradio as gr
20
+
21
+ from huggingface_hub import snapshot_download
22
  from huggingface_hub import snapshot_download
23
  import cv2
24
  import dotenv
 
33
  from inference import class_sizes_lower
34
  from metrics import process_results_for_plot
35
  from metrics import prediction_dashboard
36
+ from inference import classes
37
+ from inference import class_sizes_lower
38
+ from metrics import process_results_for_plot
39
+ from metrics import prediction_dashboard
40
  import os
41
  import pathlib
42
  import multiprocessing as mp
inference.py CHANGED
@@ -15,6 +15,7 @@ from huggingface_hub import hf_hub_download
15
  from huggingface_hub import snapshot_download
16
  from time import time
17
 
 
18
  classes = ['Beach',
19
  'Sea',
20
  'Wave',
@@ -24,13 +25,19 @@ classes = ['Beach',
24
  'Foam',
25
  'Algae',
26
  'Vegetation',
 
 
27
  'Watermark',
28
  'Bird',
29
  'Ship',
30
  'Boat',
31
  'Car',
 
 
 
32
  'Kayak',
33
  "Shark's line",
 
34
  'Dock',
35
  'Dog',
36
  'Unidentifiable shade',
@@ -46,6 +53,8 @@ classes = ['Beach',
46
  'Person',
47
  'ocean',
48
  'Surfer',
 
 
49
  'Surfer',
50
  'Fish',
51
  'Killer whale',
@@ -67,6 +76,16 @@ classes = ['Beach',
67
  'Blacktip shark',
68
  'Tiger shark',
69
  'Bull shark']*3
 
 
 
 
 
 
 
 
 
 
70
 
71
  class_sizes = {'Beach': None,
72
  'Sea': None,
@@ -124,6 +143,11 @@ class_sizes = {'Beach': None,
124
 
125
  class_sizes_lower = {k.lower(): v for k, v in class_sizes.items()}
126
 
 
 
 
 
 
127
  classes_is_shark = [1 if 'shark' in x.lower() else 0 for x in classes]
128
  classes_is_human = [1 if 'person' or 'surfer' in x.lower() else 0 for x in classes]
129
  classes_is_unknown = [1 if 'unidentifiable' in x.lower() else 0 for x in classes]
@@ -131,8 +155,15 @@ classes_is_unknown = [1 if 'unidentifiable' in x.lower() else 0 for x in classes
131
  classes_is_shark_id = [i for i, x in enumerate(classes_is_shark) if x == 1]
132
  classes_is_human_id = [i for i, x in enumerate(classes_is_human) if x == 1]
133
  classes_is_unknown_id = [i for i, x in enumerate(classes_is_unknown) if x == 1]
 
 
 
134
 
135
 
 
 
 
 
136
  if not os.path.exists('model'):
137
  REPO_ID = "SharkSpace/maskformer_model"
138
  FILENAME = "mask2former"
@@ -151,6 +182,7 @@ register_all_modules()
151
  # build the model from a config file and a checkpoint file
152
  model = init_detector(config_file, checkpoint_file, device='cuda:0') # or device='cuda:0'
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)
@@ -162,12 +194,20 @@ visualizer.dataset_meta = model.dataset_meta
162
  classes = visualizer.dataset_meta.get('classes', None)
163
  palette = visualizer.dataset_meta.get('palette', None)
164
 
 
 
 
 
 
 
 
165
  print(len(classes))
166
  print(len(palette))
167
  def inference_frame_serial(image, visualize = True):
168
  #start = time()
169
  result = inference_detector(model, image)
170
  #print(f'inference time: {time()-start}')
 
171
  # show the results
172
  if visualize:
173
  visualizer.add_datasample(
@@ -181,6 +221,18 @@ def inference_frame_serial(image, visualize = True):
181
  else:
182
  frame = None
183
  return frame, result
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  def inference_frame(image):
186
  result = inference_detector(model, image)
 
15
  from huggingface_hub import snapshot_download
16
  from time import time
17
 
18
+ classes = ['Beach',
19
  classes = ['Beach',
20
  'Sea',
21
  'Wave',
 
25
  'Foam',
26
  'Algae',
27
  'Vegetation',
28
+ 'Algae',
29
+ 'Vegetation',
30
  'Watermark',
31
  'Bird',
32
  'Ship',
33
  'Boat',
34
  'Car',
35
+ 'Ship',
36
+ 'Boat',
37
+ 'Car',
38
  'Kayak',
39
  "Shark's line",
40
+ "Shark's line",
41
  'Dock',
42
  'Dog',
43
  'Unidentifiable shade',
 
53
  'Person',
54
  'ocean',
55
  'Surfer',
56
+ 'ocean',
57
+ 'Surfer',
58
  'Surfer',
59
  'Fish',
60
  'Killer whale',
 
76
  'Blacktip shark',
77
  'Tiger shark',
78
  'Bull shark']*3
79
+ 'Shark',
80
+ 'Nurse shark',
81
+ 'Silky shark',
82
+ 'Leopard shark',
83
+ 'Shortfin mako shark',
84
+ 'Hammerhead shark',
85
+ 'Oceanic whitetip shark',
86
+ 'Blacktip shark',
87
+ 'Tiger shark',
88
+ 'Bull shark']*3
89
 
90
  class_sizes = {'Beach': None,
91
  'Sea': None,
 
143
 
144
  class_sizes_lower = {k.lower(): v for k, v in class_sizes.items()}
145
 
146
+ classes_is_shark = [1 if 'shark' in x.lower() else 0 for x in classes]
147
+ classes_is_human = [1 if 'person' or 'surfer' in x.lower() else 0 for x in classes]
148
+ classes_is_unknown = [1 if 'unidentifiable' in x.lower() else 0 for x in classes]
149
+ class_sizes_lower = {k.lower(): v for k, v in class_sizes.items()}
150
+
151
  classes_is_shark = [1 if 'shark' in x.lower() else 0 for x in classes]
152
  classes_is_human = [1 if 'person' or 'surfer' in x.lower() else 0 for x in classes]
153
  classes_is_unknown = [1 if 'unidentifiable' in x.lower() else 0 for x in classes]
 
155
  classes_is_shark_id = [i for i, x in enumerate(classes_is_shark) if x == 1]
156
  classes_is_human_id = [i for i, x in enumerate(classes_is_human) if x == 1]
157
  classes_is_unknown_id = [i for i, x in enumerate(classes_is_unknown) if x == 1]
158
+ classes_is_shark_id = [i for i, x in enumerate(classes_is_shark) if x == 1]
159
+ classes_is_human_id = [i for i, x in enumerate(classes_is_human) if x == 1]
160
+ classes_is_unknown_id = [i for i, x in enumerate(classes_is_unknown) if x == 1]
161
 
162
 
163
+ if not os.path.exists('model'):
164
+ REPO_ID = "SharkSpace/maskformer_model"
165
+ FILENAME = "mask2former"
166
+ snapshot_download(repo_id=REPO_ID, token= os.environ.get('SHARK_MODEL'),local_dir='model/')
167
  if not os.path.exists('model'):
168
  REPO_ID = "SharkSpace/maskformer_model"
169
  FILENAME = "mask2former"
 
182
  # build the model from a config file and a checkpoint file
183
  model = init_detector(config_file, checkpoint_file, device='cuda:0') # or device='cuda:0'
184
  model.dataset_meta['palette'] = model.dataset_meta['palette'] + model.dataset_meta['palette'][-23:]
185
+ model.dataset_meta['palette'] = model.dataset_meta['palette'] + model.dataset_meta['palette'][-23:]
186
  model.dataset_meta['classes'] = classes
187
  print(model.cfg.visualizer)
188
  # init visualizer(run the block only once in jupyter notebook)
 
194
  classes = visualizer.dataset_meta.get('classes', None)
195
  palette = visualizer.dataset_meta.get('palette', None)
196
 
197
+ print(len(classes))
198
+ print(len(palette))
199
+ def inference_frame_serial(image, visualize = True):
200
+ #start = time()
201
+ classes = visualizer.dataset_meta.get('classes', None)
202
+ palette = visualizer.dataset_meta.get('palette', None)
203
+
204
  print(len(classes))
205
  print(len(palette))
206
  def inference_frame_serial(image, visualize = True):
207
  #start = time()
208
  result = inference_detector(model, image)
209
  #print(f'inference time: {time()-start}')
210
+ #print(f'inference time: {time()-start}')
211
  # show the results
212
  if visualize:
213
  visualizer.add_datasample(
 
221
  else:
222
  frame = None
223
  return frame, result
224
+ if visualize:
225
+ visualizer.add_datasample(
226
+ 'result',
227
+ image,
228
+ data_sample=result,
229
+ draw_gt = None,
230
+ show=False
231
+ )
232
+ frame = visualizer.get_image()
233
+ else:
234
+ frame = None
235
+ return frame, result
236
 
237
  def inference_frame(image):
238
  result = inference_detector(model, image)
output.mp4 ADDED
Binary file (262 kB). View file