piperod91 commited on
Commit
3e8b98f
1 Parent(s): 50ba4a7

fixing labels, putting border again

Browse files
Files changed (4) hide show
  1. app.py +44 -52
  2. inference.py +4 -4
  3. metrics.py +2 -0
  4. pre-requeriments.txt +0 -1
app.py CHANGED
@@ -65,28 +65,31 @@ def overlay_text_on_image(image, text_list, font=cv2.FONT_HERSHEY_SIMPLEX, font_
65
  cv2.putText(image, line, (image.shape[1] - text_width - margin, y), font, font_size, color, font_thickness, lineType=cv2.LINE_AA)
66
  return image
67
 
68
- def draw_cockpit(frame, top_pred, cnt):
69
- # # Bullet points:
70
- # high_danger_color = (255,0,0)
71
- # low_danger_color = yellowgreen = (154,205,50)
72
-
73
- # shark_sighted = 'Shark Detected: ' + str(top_pred['shark_sighted'])
74
- # human_sighted = 'Number of Humans: ' + str(top_pred['human_n'])
75
- # shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size'])
76
- # shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight'])
77
- # danger_level = 'Danger Level: '
78
- # danger_level += 'High' if top_pred['dangerous_dist'] else 'Low'
79
- # danger_color = high_danger_color if top_pred['dangerous_dist'] else low_danger_color # 'orangered' for high danger , 'yellowgreen' for low danger'
80
-
81
- # # Create a list of strings to plot
82
- # strings = [shark_sighted, human_sighted, shark_size_estimate, shark_weight_estimate, danger_level]
83
- # relative = max(frame.shape[0],frame.shape[1])
84
- # if top_pred['shark_sighted'] and (cnt % 2) == 0:
85
- # relative = max(frame.shape[0],frame.shape[1])
86
- # frame = add_border(frame, color=danger_color, thickness=int(relative*0.025))
87
- # frame = overlay_text_on_image(frame, strings, font=cv2.FONT_HERSHEY_SIMPLEX, font_size=1.5, font_thickness=3, margin=int(relative*0.05), color=(255, 255, 255))
 
88
  return frame
89
 
 
 
90
  def process_video(input_video, out_fps = 'auto', skip_frames = 7):
91
  cap = cv2.VideoCapture(input_video)
92
 
@@ -107,46 +110,38 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
107
  cnt = 0
108
 
109
  while iterating:
110
- print(cnt)
111
  if (cnt % skip_frames) == 0:
112
- # Get inference results and output masks
 
113
  display_frame, result = inference_frame_serial(frame)
114
- video.write(cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB))
115
 
116
  #print(result)
117
- print('start top_pred')
118
  top_pred = process_results_for_plot(predictions = result.numpy(),
119
  classes = classes,
120
  class_sizes = class_sizes_lower)
121
-
122
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
123
  prediction_frame = cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
124
 
125
- frame = cv2.resize(frame, (int(width*4), int(height*4)))
126
 
127
- # Only show the prediction frame every other frame
128
- # print('first if')
129
- # if ((cnt*skip_frames) % 2 == 0): # and top_pred['shark_sighted']:
130
- # prediction_frame = cv2.resize(prediction_frame, (int(width*4), int(height*4)))
131
- # frame = prediction_frame
132
-
133
- # # Add cockpit to frame
134
- # print('cockput if')
135
- # if top_pred['shark_sighted']:
136
- # frame = draw_cockpit(frame, top_pred, cnt*skip_frames)
137
 
138
- print('dashboard')
 
 
 
 
 
 
139
  pred_dashbord = prediction_dashboard(top_pred = top_pred)
140
  #print('sending frame')
141
- print('finished dashboard')
142
- print(cnt)
143
- # if cnt == 119:
144
- # import ipdb; ipdb.set_trace()
145
-
146
- yield prediction_frame, frame , None, pred_dashbord
147
  cnt += 1
148
  iterating, frame = cap.read()
149
- print('interating: ', iterating)
150
 
151
  video.release()
152
  yield None, None, output_path, None
@@ -154,15 +149,12 @@ def process_video(input_video, out_fps = 'auto', skip_frames = 7):
154
  with gr.Blocks(theme=theme) as demo:
155
  with gr.Row().style(equal_height=True,height='50%'):
156
  input_video = gr.Video(label="Input")
157
- output_video = gr.Video(label="Output Video")
158
-
159
- with gr.Row():
160
  processed_frames = gr.Image(label="Shark Engine")
 
161
  dashboard = gr.Image(label="Dashboard")
162
- original_frames = gr.Image(label="Original Frame") #, width='100%', height='100%')
163
 
164
- #with gr.Row(height='100%',width='100%'):
165
- #original_frames = gr.Image(label="Original Frame", width='100%', height='100%')
166
 
167
  with gr.Row():
168
  paths = sorted(pathlib.Path('videos_example/').rglob('*.mp4'))
@@ -174,6 +166,6 @@ with gr.Blocks(theme=theme) as demo:
174
 
175
  demo.queue()
176
  if os.getenv('SYSTEM') == 'spaces':
177
- demo.launch(width='40%',auth=(os.environ.get('SHARK_USERNAME'), os.environ.get('SHARK_PASSWORD')), debug = True)
178
  else:
179
- demo.launch(debug = True)
 
65
  cv2.putText(image, line, (image.shape[1] - text_width - margin, y), font, font_size, color, font_thickness, lineType=cv2.LINE_AA)
66
  return image
67
 
68
+ def draw_cockpit(frame, top_pred,cnt):
69
+ # Bullet points:
70
+ high_danger_color = (255,0,0)
71
+ low_danger_color = yellowgreen = (154,205,50)
72
+ shark_sighted = 'Shark Detected: ' + str(top_pred['shark_sighted'])
73
+ human_sighted = 'Number of Humans: ' + str(top_pred['human_n'])
74
+ shark_size_estimate = 'Biggest shark size: ' + str(top_pred['biggest_shark_size'])
75
+ shark_weight_estimate = 'Biggest shark weight: ' + str(top_pred['biggest_shark_weight'])
76
+ danger_level = 'Danger Level: '
77
+ danger_level += 'High' if top_pred['dangerous_dist'] else 'Low'
78
+ danger_color = 'orangered' if top_pred['dangerous_dist'] else 'yellowgreen'
79
+ # Create a list of strings to plot
80
+ strings = [shark_sighted, human_sighted, shark_size_estimate, shark_weight_estimate, danger_level]
81
+ relative = max(frame.shape[0],frame.shape[1])
82
+ if top_pred['shark_sighted'] and top_pred['dangerous_dist'] and cnt%2 == 0:
83
+ relative = max(frame.shape[0],frame.shape[1])
84
+ frame = add_border(frame, color=high_danger_color, thickness=int(relative*0.025))
85
+ elif top_pred['shark_sighted'] and not top_pred['dangerous_dist'] and cnt%2 == 0:
86
+ relative = max(frame.shape[0],frame.shape[1])
87
+ frame = add_border(frame, color=low_danger_color, thickness=int(relative*0.025))
88
+ overlay_text_on_image(frame, strings, font=cv2.FONT_HERSHEY_SIMPLEX, font_size=relative*0.0007, font_thickness=1, margin=int(relative*0.05), color=(255, 255, 255))
89
  return frame
90
 
91
+
92
+
93
  def process_video(input_video, out_fps = 'auto', skip_frames = 7):
94
  cap = cv2.VideoCapture(input_video)
95
 
 
110
  cnt = 0
111
 
112
  while iterating:
 
113
  if (cnt % skip_frames) == 0:
114
+ print('starting Frame: ', cnt)
115
+ # flip frame vertically
116
  display_frame, result = inference_frame_serial(frame)
 
117
 
118
  #print(result)
 
119
  top_pred = process_results_for_plot(predictions = result.numpy(),
120
  classes = classes,
121
  class_sizes = class_sizes_lower)
 
122
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
123
  prediction_frame = cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
124
 
 
125
 
126
+ #frame = cv2.resize(frame, (int(width), int(height)))
 
 
 
 
 
 
 
 
 
127
 
128
+ if cnt*skip_frames %2==0 and top_pred['shark_sighted']:
129
+ #prediction_frame = cv2.resize(prediction_frame, (int(width), int(height)))
130
+ frame =prediction_frame
131
+
132
+ if top_pred['shark_sighted']:
133
+ frame = draw_cockpit(frame, top_pred,cnt*skip_frames)
134
+ video.write(frame)
135
  pred_dashbord = prediction_dashboard(top_pred = top_pred)
136
  #print('sending frame')
137
+ print('finalizing frame:',cnt)
138
+ print(pred_dashbord.shape)
139
+ print(frame.shape)
140
+ print(prediction_frame.shape)
141
+ yield prediction_frame,frame , None, pred_dashbord
142
+ print('overall count ', cnt)
143
  cnt += 1
144
  iterating, frame = cap.read()
 
145
 
146
  video.release()
147
  yield None, None, output_path, None
 
149
  with gr.Blocks(theme=theme) as demo:
150
  with gr.Row().style(equal_height=True,height='50%'):
151
  input_video = gr.Video(label="Input")
 
 
 
152
  processed_frames = gr.Image(label="Shark Engine")
153
+ output_video = gr.Video(label="Output Video")
154
  dashboard = gr.Image(label="Dashboard")
 
155
 
156
+ with gr.Row():
157
+ original_frames = gr.Image(label="Original Frame").style( height=768)
158
 
159
  with gr.Row():
160
  paths = sorted(pathlib.Path('videos_example/').rglob('*.mp4'))
 
166
 
167
  demo.queue()
168
  if os.getenv('SYSTEM') == 'spaces':
169
+ demo.launch(width='40%',auth=(os.environ.get('SHARK_USERNAME'), os.environ.get('SHARK_PASSWORD')))
170
  else:
171
+ demo.launch()
inference.py CHANGED
@@ -53,11 +53,11 @@ classes = ['Beach',
53
  'Dolphin',
54
  'Miscellaneous',
55
  'Unidentifiable shark',
56
- 'Carpet shark',
57
  'Dusty shark',
58
  'Blue shark',
59
  'Great white shark',
60
- 'Copper shark',
61
  'Nurse shark',
62
  'Silky shark',
63
  'Leopard shark',
@@ -106,11 +106,11 @@ class_sizes = {'Beach': None,
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
- 'Carpet 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]},
112
  'Great white shark': {'feet': [13.1, 20], 'meter': [4, 6], 'kg': [680, 1800], 'pounds': [1500, 4000]},
113
- 'Copper shark': {'feet': [7.2, 10.8], 'meter': [2.2, 3.3], 'kg': [130, 300], 'pounds': [290, 660]},
114
  'Nurse shark': {'feet': [7.9, 9.8], 'meter': [2.4, 3], 'kg': [90, 115], 'pounds': [200, 250]},
115
  'Silky shark': {'feet': [6.6, 8.2], 'meter': [2, 2.5], 'kg': [300, 380], 'pounds': [660, 840]},
116
  'Leopard shark': {'feet': [3.9, 4.9], 'meter': [1.2, 1.5], 'kg': [11, 20], 'pounds': [22, 44]},
 
53
  'Dolphin',
54
  'Miscellaneous',
55
  'Unidentifiable shark',
56
+ 'C Shark',
57
  'Dusty shark',
58
  'Blue shark',
59
  'Great white shark',
60
+ 'Shark',
61
  'Nurse shark',
62
  'Silky shark',
63
  'Leopard shark',
 
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]},
112
  'Great white shark': {'feet': [13.1, 20], 'meter': [4, 6], 'kg': [680, 1800], 'pounds': [1500, 4000]},
113
+ 'Shark': {'feet': [7.2, 10.8], 'meter': [2.2, 3.3], 'kg': [130, 300], 'pounds': [290, 660]},
114
  'Nurse shark': {'feet': [7.9, 9.8], 'meter': [2.4, 3], 'kg': [90, 115], 'pounds': [200, 250]},
115
  'Silky shark': {'feet': [6.6, 8.2], 'meter': [2, 2.5], 'kg': [300, 380], 'pounds': [660, 840]},
116
  'Leopard shark': {'feet': [3.9, 4.9], 'meter': [1.2, 1.5], 'kg': [11, 20], 'pounds': [22, 44]},
metrics.py CHANGED
@@ -49,6 +49,7 @@ def add_class_sizes(top_pred = {}, class_sizes = None):
49
  tmp_class_sizes = class_sizes[tmp_pred.lower()]
50
  if tmp_class_sizes == None:
51
  size_list.append(None)
 
52
  else:
53
  size_list.append(tmp_class_sizes['feet'])
54
 
@@ -74,6 +75,7 @@ def add_class_weights(top_pred = {}, class_weights = None):
74
  tmp_class_weights = class_weights[tmp_pred.lower()]
75
  if tmp_class_weights == None:
76
  weight_list.append(None)
 
77
  else:
78
  weight_list.append(tmp_class_weights['pounds'])
79
 
 
49
  tmp_class_sizes = class_sizes[tmp_pred.lower()]
50
  if tmp_class_sizes == None:
51
  size_list.append(None)
52
+ continue
53
  else:
54
  size_list.append(tmp_class_sizes['feet'])
55
 
 
75
  tmp_class_weights = class_weights[tmp_pred.lower()]
76
  if tmp_class_weights == None:
77
  weight_list.append(None)
78
+ continue
79
  else:
80
  weight_list.append(tmp_class_weights['pounds'])
81
 
pre-requeriments.txt CHANGED
@@ -1,5 +1,4 @@
1
 
2
-
3
  numpy==1.22.4
4
  opencv-python-headless==4.5.5.64
5
  openmim==0.1.5
 
1
 
 
2
  numpy==1.22.4
3
  opencv-python-headless==4.5.5.64
4
  openmim==0.1.5