ozyman commited on
Commit
fd56c8a
·
1 Parent(s): fa7b864

added sequential processing

Browse files
Files changed (1) hide show
  1. app.py +61 -51
app.py CHANGED
@@ -104,8 +104,7 @@ def find_largest_face(faces):
104
  return largest_face
105
 
106
 
107
- def inference(img, model_name):
108
- confidences = {}
109
  grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
110
  faces = faceClassifier.detectMultiScale(
111
  grey, scaleFactor=1.1, minNeighbors=4)
@@ -118,60 +117,71 @@ def inference(img, model_name):
118
  faceRegion = tfms(faceRegion)
119
  faceRegion = faceRegion.unsqueeze(0)
120
 
121
- if model_name == 'DeePixBiS':
122
- mask, binary = deepix_model.forward(faceRegion)
123
- res = torch.mean(mask).item()
124
- cls = 'Real' if res >= pix_threshhold else 'Spoof'
125
- res = 1 - res
126
-
127
- else:
128
- dense_flag = True
129
- boxes = list(face)
130
- boxes.append(1)
131
- param_lst, roi_box_lst = tddfa(img, [boxes])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- ver_lst = tddfa.recon_vers(param_lst, roi_box_lst, dense_flag=dense_flag)
134
- depth_img = depth(img, ver_lst, tddfa.tri, with_bg_flag=False)
135
- with torch.no_grad():
136
- map_score_list = []
137
- image_x, map_x = prepare_data([img], [list(face)], [depth_img])
138
- # get the inputs
139
- image_x = image_x.unsqueeze(0)
140
- map_x = map_x.unsqueeze(0)
141
- inputs = image_x.to(device)
142
- test_maps = map_x.to(device)
143
- optimizer.zero_grad()
144
-
145
- map_score = 0.0
146
- for frame_t in range(inputs.shape[1]):
147
- mu, logvar, map_x, x_concat, x_Block1, x_Block2, x_Block3, x_input = cdcn_model(inputs[:, frame_t, :, :, :])
148
-
149
- score_norm = torch.sum(mu) / torch.sum(test_maps[:, frame_t, :, :])
150
- map_score += score_norm
151
- map_score = map_score / inputs.shape[1]
152
- map_score_list.append(map_score)
153
-
154
- res = map_score_list[0].item()
155
- if res > 10:
156
- res = 0.0
157
- cls = 'Real' if res >= dsdg_threshold else 'Spoof'
158
- res = res * 100
159
-
160
- label = f'{cls} {res:.2f}'
161
- confidences = {label: res}
162
- color = color = (0, 255, 0) if cls == 'Real' else (255, 0, 0)
163
- cv.rectangle(img, (x, y), (x + w, y + h), color, 2)
164
- cv.putText(img, label, (x, y + h + 30),
165
- cv.FONT_HERSHEY_COMPLEX, 1, color)
166
-
167
- return img, confidences
168
 
169
 
170
  if __name__ == '__main__':
171
  demo = gr.Interface(
172
  fn=inference,
173
- inputs=[gr.Image(source='webcam', shape=None, type='numpy'),
174
- Dropdown(["DeePixBiS", "DSDG"], value="DeePixBiS")],
175
- outputs=["image", gr.Label(num_top_classes=2)],
 
 
 
176
  examples=examples).queue(concurrency_count=2)
177
  demo.launch(share=False)
 
104
  return largest_face
105
 
106
 
107
+ def inference(img):
 
108
  grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
109
  faces = faceClassifier.detectMultiScale(
110
  grey, scaleFactor=1.1, minNeighbors=4)
 
117
  faceRegion = tfms(faceRegion)
118
  faceRegion = faceRegion.unsqueeze(0)
119
 
120
+ # if model_name == 'DeePixBiS':
121
+ mask, binary = deepix_model.forward(faceRegion)
122
+ res_deepix = torch.mean(mask).item()
123
+ cls_deepix = 'Real' if res_deepix >= pix_threshhold else 'Spoof'
124
+
125
+ label_deepix = f'{cls_deepix} {res_deepix:.2f}'
126
+ confidences_deepix = {label_deepix: res_deepix}
127
+ color_deepix = (0, 255, 0) if cls_deepix == 'Real' else (255, 0, 0)
128
+ img_deepix = cv.rectangle(img.copy(), (x, y), (x + w, y + h), color_deepix, 2)
129
+ cv.putText(img_deepix, label_deepix, (x, y + h + 30),
130
+ cv.FONT_HERSHEY_COMPLEX, 1, color_deepix)
131
+
132
+ # else:
133
+ dense_flag = True
134
+ boxes = list(face)
135
+ boxes.append(1)
136
+ param_lst, roi_box_lst = tddfa(img, [boxes])
137
+
138
+ ver_lst = tddfa.recon_vers(param_lst, roi_box_lst, dense_flag=dense_flag)
139
+ depth_img = depth(img, ver_lst, tddfa.tri, with_bg_flag=False)
140
+ with torch.no_grad():
141
+ map_score_list = []
142
+ image_x, map_x = prepare_data([img], [list(face)], [depth_img])
143
+ # get the inputs
144
+ image_x = image_x.unsqueeze(0)
145
+ map_x = map_x.unsqueeze(0)
146
+ inputs = image_x.to(device)
147
+ test_maps = map_x.to(device)
148
+ optimizer.zero_grad()
149
 
150
+ map_score = 0.0
151
+ for frame_t in range(inputs.shape[1]):
152
+ mu, logvar, map_x, x_concat, x_Block1, x_Block2, x_Block3, x_input = cdcn_model(inputs[:, frame_t, :, :, :])
153
+
154
+ score_norm = torch.sum(mu) / torch.sum(test_maps[:, frame_t, :, :])
155
+ map_score += score_norm
156
+ map_score = map_score / inputs.shape[1]
157
+ map_score_list.append(map_score)
158
+
159
+ res_dsdg = map_score_list[0].item()
160
+ if res_dsdg > 10:
161
+ res_dsdg = 0.0
162
+ cls_dsdg = 'Real' if res_dsdg >= dsdg_threshold else 'Spoof'
163
+ res_dsdg = res_dsdg * 100
164
+
165
+ label_dsdg = f'{cls_dsdg} {res_dsdg:.2f}'
166
+ confidences_dsdg = {label_dsdg: res_deepix}
167
+ color_dsdg = (0, 255, 0) if cls_dsdg == 'Real' else (255, 0, 0)
168
+ img_dsdg = cv.rectangle(img.copy(), (x, y), (x + w, y + h), color_dsdg, 2)
169
+ cv.putText(img_dsdg, label_dsdg, (x, y + h + 30),
170
+ cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
171
+
172
+ return img_deepix, confidences_deepix, img_dsdg, confidences_dsdg
173
+ else:
174
+ return img, {}, img, {}
 
 
 
 
 
 
 
 
 
 
175
 
176
 
177
  if __name__ == '__main__':
178
  demo = gr.Interface(
179
  fn=inference,
180
+ inputs=[gr.Image(source='webcam', shape=None, type='numpy')],
181
+ outputs=[
182
+ gr.outputs.Image(label='DeePixBiS'),
183
+ gr.Label(num_top_classes=2, label='DeePixBiS'),
184
+ gr.outputs.Image(label='DSDG'),
185
+ gr.Label(num_top_classes=2, label='DSDG')],
186
  examples=examples).queue(concurrency_count=2)
187
  demo.launch(share=False)