piperod91 commited on
Commit
555f068
·
1 Parent(s): 761af93
Files changed (2) hide show
  1. app.py +3 -13
  2. inference.py +2 -54
app.py CHANGED
@@ -1,8 +1,6 @@
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,9 +14,6 @@ if os.getenv('SYSTEM') == 'spaces':
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,16 +28,12 @@ from inference import classes
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
43
  from time import time
44
 
45
- if not os.path.exists('videos_example') and not os.getenv('SYSTEM') == 'spaces':
46
  REPO_ID='SharkSpace/videos_examples'
47
  snapshot_download(repo_id=REPO_ID, token=os.environ.get('SHARK_MODEL'),repo_type='dataset',local_dir='videos_example')
48
 
@@ -143,7 +134,7 @@ def draw_cockpit(frame, top_pred,cnt):
143
 
144
 
145
 
146
- def process_video(input_video,out_fps = 'auto', skip_frames = 7):
147
  cap = cv2.VideoCapture(input_video)
148
 
149
  output_path = "output.mp4"
@@ -205,9 +196,8 @@ def process_video(input_video,out_fps = 'auto', skip_frames = 7):
205
  yield None, output_path
206
 
207
  with gr.Blocks(theme=theme) as demo:
208
- with gr.Row().style(equal_height=True):
209
  input_video = gr.Video(label="Input")
210
-
211
  original_frames = gr.Image(label="Processed Frame").style( height=650)
212
  #processed_frames = gr.Image(label="Shark Engine")
213
  output_video = gr.Video(label="Output Video")
 
1
 
 
2
  import subprocess
3
  import os
 
4
  if os.getenv('SYSTEM') == 'spaces':
5
 
6
  subprocess.call('pip install -U openmim'.split())
 
14
 
15
  import gradio as gr
16
 
 
 
 
17
  from huggingface_hub import snapshot_download
18
  import cv2
19
  import dotenv
 
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
34
  from time import time
35
 
36
+ if not os.path.exists('videos_example'):
37
  REPO_ID='SharkSpace/videos_examples'
38
  snapshot_download(repo_id=REPO_ID, token=os.environ.get('SHARK_MODEL'),repo_type='dataset',local_dir='videos_example')
39
 
 
134
 
135
 
136
 
137
+ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
138
  cap = cv2.VideoCapture(input_video)
139
 
140
  output_path = "output.mp4"
 
196
  yield None, output_path
197
 
198
  with gr.Blocks(theme=theme) as demo:
199
+ with gr.Row().style(equal_height=True,height='25%'):
200
  input_video = gr.Video(label="Input")
 
201
  original_frames = gr.Image(label="Processed Frame").style( height=650)
202
  #processed_frames = gr.Image(label="Shark Engine")
203
  output_video = gr.Video(label="Output Video")
inference.py CHANGED
@@ -15,7 +15,6 @@ from huggingface_hub import hf_hub_download
15
  from huggingface_hub import snapshot_download
16
  from time import time
17
 
18
- classes = ['Beach',
19
  classes = ['Beach',
20
  'Sea',
21
  'Wave',
@@ -25,19 +24,13 @@ classes = ['Beach',
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,15 +46,13 @@ classes = ['Beach',
53
  'Person',
54
  'ocean',
55
  'Surfer',
56
- 'ocean',
57
- 'Surfer',
58
  'Surfer',
59
  'Fish',
60
  'Killer whale',
61
  'Whale',
62
  'Dolphin',
63
  'Miscellaneous',
64
- 'Shadow',
65
  'C Shark',
66
  'Dusty shark',
67
  'Blue shark',
@@ -76,16 +67,6 @@ classes = ['Beach',
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,
@@ -124,7 +105,7 @@ class_sizes = {'Beach': None,
124
  'Whale': {'feet':[15, 30], 'meter': [4.5, 10], 'kg': [2500, 80000], 'pounds': [55000, 176000]},
125
  'Dolphin': {'feet':[6.6, 13.1], 'meter': [2, 4], 'kg': [150, 650], 'pounds': [330, 1430]},
126
  'Miscellaneous': None,
127
- 'Shadow': {'feet': [2, 15], 'meter': [0.6, 4.5], 'kg': [50, 1000], 'pounds': [110, 2200]},
128
  'C Shark': {'feet': [4, 10], 'meter': [1.25, 3], 'kg': [50, 1000], 'pounds': [110, 2200]}, # Prob incorrect
129
  'Dusty shark': {'feet': [9, 14], 'meter': [3, 4.25], 'kg': [160, 180], 'pounds': [350, 400]},
130
  'Blue shark': {'feet': [7.9, 12.5], 'meter': [2.4, 3], 'kg': [60, 120], 'pounds': [130, 260]},
@@ -143,11 +124,6 @@ class_sizes = {'Beach': 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,15 +131,8 @@ 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,7 +151,6 @@ register_all_modules()
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,20 +162,12 @@ visualizer.dataset_meta = model.dataset_meta
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,18 +181,6 @@ def inference_frame_serial(image, visualize = True):
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)
 
15
  from huggingface_hub import snapshot_download
16
  from time import time
17
 
 
18
  classes = ['Beach',
19
  'Sea',
20
  'Wave',
 
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
  'Person',
47
  'ocean',
48
  'Surfer',
 
 
49
  'Surfer',
50
  'Fish',
51
  'Killer whale',
52
  'Whale',
53
  'Dolphin',
54
  'Miscellaneous',
55
+ 'Unidentifiable shark',
56
  'C Shark',
57
  'Dusty shark',
58
  'Blue shark',
 
67
  'Blacktip shark',
68
  'Tiger shark',
69
  'Bull shark']*3
 
 
 
 
 
 
 
 
 
 
70
 
71
  class_sizes = {'Beach': None,
72
  'Sea': None,
 
105
  'Whale': {'feet':[15, 30], 'meter': [4.5, 10], 'kg': [2500, 80000], 'pounds': [55000, 176000]},
106
  'Dolphin': {'feet':[6.6, 13.1], 'meter': [2, 4], 'kg': [150, 650], 'pounds': [330, 1430]},
107
  'Miscellaneous': None,
108
+ 'Unidentifiable shark': {'feet': [2, 15], 'meter': [0.6, 4.5], 'kg': [50, 1000], 'pounds': [110, 2200]},
109
  'C Shark': {'feet': [4, 10], 'meter': [1.25, 3], 'kg': [50, 1000], 'pounds': [110, 2200]}, # Prob incorrect
110
  'Dusty shark': {'feet': [9, 14], 'meter': [3, 4.25], 'kg': [160, 180], 'pounds': [350, 400]},
111
  'Blue shark': {'feet': [7.9, 12.5], 'meter': [2.4, 3], 'kg': [60, 120], 'pounds': [130, 260]},
 
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
  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
  # 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
  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
  else:
182
  frame = None
183
  return frame, result
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  def inference_frame(image):
186
  result = inference_detector(model, image)