added sequential processing
Browse files
app.py
CHANGED
|
@@ -104,8 +104,7 @@ def find_largest_face(faces):
|
|
| 104 |
return largest_face
|
| 105 |
|
| 106 |
|
| 107 |
-
def inference(img
|
| 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 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 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 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 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)
|