fixed video
Browse files- .gitignore +4 -1
- app.py +35 -38
.gitignore
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
__pycache__/
|
| 2 |
*.pth
|
| 3 |
data/
|
| 4 |
-
.DS_Store
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
*.pth
|
| 3 |
data/
|
| 4 |
+
.DS_Store
|
| 5 |
+
flagged/
|
| 6 |
+
# main.ipynb
|
| 7 |
+
*.mp4
|
app.py
CHANGED
|
@@ -32,12 +32,12 @@ os.environ['OMP_NUM_THREADS'] = '4'
|
|
| 32 |
os.environ['AWS_ACCESS_KEY_ID'] = 'AKIA3JAMX4K53MFDKMGJ'
|
| 33 |
os.environ['AWS_SECRET_ACCESS_KEY'] = 'lHf9xIwdgO3eXrE9a4KL+BTJ7af2cgZJYRRxw4NI'
|
| 34 |
|
| 35 |
-
app_version = '
|
| 36 |
|
| 37 |
device = torch.device("cpu")
|
| 38 |
labels = ['Live', 'Spoof']
|
| 39 |
PIX_THRESHOLD = 0.45
|
| 40 |
-
DSDG_THRESHOLD =
|
| 41 |
MIN_FACE_WIDTH_THRESHOLD = 210
|
| 42 |
examples = [
|
| 43 |
['examples/1_1_21_2_33_scene_fake.jpg'],
|
|
@@ -101,6 +101,7 @@ def prepare_data_dsdg(images, boxes, depths):
|
|
| 101 |
depth_x = torch.from_numpy(depth_x.astype(float)).float()
|
| 102 |
return image_x, depth_x
|
| 103 |
|
|
|
|
| 104 |
def find_largest_face(faces):
|
| 105 |
# find the largest face in the list
|
| 106 |
largest_face = None
|
|
@@ -144,7 +145,7 @@ def deepix_model_inference(img, bbox):
|
|
| 144 |
|
| 145 |
|
| 146 |
def dsdg_model_inference(img, bbox, dsdg_thresh):
|
| 147 |
-
dsdg_thresh = dsdg_thresh /
|
| 148 |
dense_flag = True
|
| 149 |
x, y, x2, y2 = bbox
|
| 150 |
w = x2 - x
|
|
@@ -155,7 +156,7 @@ def dsdg_model_inference(img, bbox, dsdg_thresh):
|
|
| 155 |
img_dsdg = cv.rectangle(img.copy(), (x, y), (x2, y2), color_dsdg, 2)
|
| 156 |
cv.putText(img_dsdg, text, (x, y2 + 30),
|
| 157 |
cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
|
| 158 |
-
cls_dsdg =
|
| 159 |
return img_dsdg, {}, cls_dsdg
|
| 160 |
bbox_conf = list(bbox)
|
| 161 |
bbox_conf.append(1)
|
|
@@ -183,14 +184,14 @@ def dsdg_model_inference(img, bbox, dsdg_thresh):
|
|
| 183 |
res_dsdg = 0.0
|
| 184 |
cls_dsdg = 'Real' if res_dsdg >= dsdg_thresh else 'Spoof'
|
| 185 |
text = f'{cls_dsdg} {w}*{h}'
|
| 186 |
-
res_dsdg = res_dsdg * 300
|
| 187 |
confidences_dsdg = {'Real confidence': res_dsdg}
|
| 188 |
color_dsdg = (0, 255, 0) if cls_dsdg == 'Real' else (255, 0, 0)
|
| 189 |
img_dsdg = cv.rectangle(img.copy(), (x, y), (x2, y2), color_dsdg, 2)
|
| 190 |
cv.putText(img_dsdg, text, (x, y2 + 30),
|
| 191 |
cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
|
| 196 |
def inference(img, dsdg_thresh):
|
|
@@ -214,9 +215,9 @@ def process_video(vid_path, dsdg_thresh):
|
|
| 214 |
input_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
|
| 215 |
|
| 216 |
# Set video codec and create VideoWriter object to save the output video
|
| 217 |
-
fourcc = cv.VideoWriter_fourcc(*'
|
| 218 |
output_vid_path = 'output_dsdg.mp4'
|
| 219 |
-
out_dsdg = cv.VideoWriter(output_vid_path, fourcc,
|
| 220 |
|
| 221 |
frame_counter = 0
|
| 222 |
confidences_arr = []
|
|
@@ -224,76 +225,72 @@ def process_video(vid_path, dsdg_thresh):
|
|
| 224 |
ret, frame = cap.read()
|
| 225 |
if not ret:
|
| 226 |
break
|
| 227 |
-
|
| 228 |
# Process only every 5th frame
|
| 229 |
if frame_counter % 5 == 0:
|
| 230 |
# Run inference on the current frame
|
| 231 |
-
_, _, _, img_dsdg, confidences_dsdg,
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
img_dsdg = cv.resize(img_dsdg, (input_width, input_height))
|
| 236 |
-
|
| 237 |
# Write the DSDG frame to the output video
|
| 238 |
out_dsdg.write(img_dsdg)
|
| 239 |
-
|
| 240 |
frame_counter += 1
|
| 241 |
-
avg_conf = sum(confidences_arr) / len(confidences_arr)
|
| 242 |
-
confidences_dsdg = {'Average real confidence': avg_conf}
|
| 243 |
# Release resources
|
| 244 |
cap.release()
|
| 245 |
out_dsdg.release()
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
-
def upload_to_s3(
|
| 250 |
folder = 'demo'
|
| 251 |
bucket_name = 'livenessng'
|
| 252 |
-
|
|
|
|
| 253 |
return 'Error. Take a photo first.'
|
| 254 |
-
elif labels[-2] == -
|
| 255 |
return 'Error. Run the detection first.'
|
| 256 |
elif labels[0] is None:
|
| 257 |
return 'Error. Select the true label first.'
|
|
|
|
|
|
|
| 258 |
|
| 259 |
# Initialize S3 client
|
| 260 |
s3 = boto3.client('s3')
|
| 261 |
|
| 262 |
-
# Encode labels and app version in
|
| 263 |
encoded_labels = '_'.join([str(int(label)) for label in labels])
|
| 264 |
random_string = str(uuid.uuid4()).split('-')[-1]
|
| 265 |
-
|
| 266 |
|
| 267 |
-
#
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
image.save(image_bytes, format='JPEG')
|
| 271 |
-
image_bytes.seek(0)
|
| 272 |
|
| 273 |
-
#
|
| 274 |
-
res = s3.upload_fileobj(image_bytes, bucket_name, image_name)
|
| 275 |
-
# Return the S3 URL of the uploaded image
|
| 276 |
status = 'Successfully uploaded'
|
| 277 |
return status
|
| 278 |
|
| 279 |
|
| 280 |
-
# interface = .queue(concurrency_count=2)
|
| 281 |
demo = gr.Blocks()
|
| 282 |
|
| 283 |
with demo:
|
| 284 |
with gr.Row():
|
| 285 |
with gr.Column():
|
| 286 |
input_vid = gr.Video(format='mp4', source='webcam')
|
| 287 |
-
dsdg_thresh = gr.Slider(value=DSDG_THRESHOLD, label='DSDG threshold')
|
| 288 |
btn_run = gr.Button(value="Run")
|
| 289 |
with gr.Column():
|
| 290 |
outputs=[
|
| 291 |
gr.Video(label='DeePixBiS', format='mp4'),
|
| 292 |
gr.Label(num_top_classes=2, label='DeePixBiS'),
|
| 293 |
-
gr.Number(visible=False, value=-
|
| 294 |
gr.Video(label='DSDG', format='mp4'),
|
| 295 |
-
gr.
|
| 296 |
-
gr.Number(visible=False, value=-
|
| 297 |
with gr.Column():
|
| 298 |
radio = gr.Radio(
|
| 299 |
["Spoof", "Real", "None"], label="True label", type='index')
|
|
|
|
| 32 |
os.environ['AWS_ACCESS_KEY_ID'] = 'AKIA3JAMX4K53MFDKMGJ'
|
| 33 |
os.environ['AWS_SECRET_ACCESS_KEY'] = 'lHf9xIwdgO3eXrE9a4KL+BTJ7af2cgZJYRRxw4NI'
|
| 34 |
|
| 35 |
+
app_version = 'dsdg_vid_1'
|
| 36 |
|
| 37 |
device = torch.device("cpu")
|
| 38 |
labels = ['Live', 'Spoof']
|
| 39 |
PIX_THRESHOLD = 0.45
|
| 40 |
+
DSDG_THRESHOLD = 0.5
|
| 41 |
MIN_FACE_WIDTH_THRESHOLD = 210
|
| 42 |
examples = [
|
| 43 |
['examples/1_1_21_2_33_scene_fake.jpg'],
|
|
|
|
| 101 |
depth_x = torch.from_numpy(depth_x.astype(float)).float()
|
| 102 |
return image_x, depth_x
|
| 103 |
|
| 104 |
+
|
| 105 |
def find_largest_face(faces):
|
| 106 |
# find the largest face in the list
|
| 107 |
largest_face = None
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
def dsdg_model_inference(img, bbox, dsdg_thresh):
|
| 148 |
+
dsdg_thresh = dsdg_thresh / 10000
|
| 149 |
dense_flag = True
|
| 150 |
x, y, x2, y2 = bbox
|
| 151 |
w = x2 - x
|
|
|
|
| 156 |
img_dsdg = cv.rectangle(img.copy(), (x, y), (x2, y2), color_dsdg, 2)
|
| 157 |
cv.putText(img_dsdg, text, (x, y2 + 30),
|
| 158 |
cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
|
| 159 |
+
cls_dsdg = -1
|
| 160 |
return img_dsdg, {}, cls_dsdg
|
| 161 |
bbox_conf = list(bbox)
|
| 162 |
bbox_conf.append(1)
|
|
|
|
| 184 |
res_dsdg = 0.0
|
| 185 |
cls_dsdg = 'Real' if res_dsdg >= dsdg_thresh else 'Spoof'
|
| 186 |
text = f'{cls_dsdg} {w}*{h}'
|
|
|
|
| 187 |
confidences_dsdg = {'Real confidence': res_dsdg}
|
| 188 |
color_dsdg = (0, 255, 0) if cls_dsdg == 'Real' else (255, 0, 0)
|
| 189 |
img_dsdg = cv.rectangle(img.copy(), (x, y), (x2, y2), color_dsdg, 2)
|
| 190 |
cv.putText(img_dsdg, text, (x, y2 + 30),
|
| 191 |
cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
|
| 192 |
+
res_dsdg = res_dsdg * 1000000
|
| 193 |
+
# cls_dsdg = 1 if cls_dsdg == 'Real' else 0
|
| 194 |
+
return img_dsdg, confidences_dsdg, res_dsdg
|
| 195 |
|
| 196 |
|
| 197 |
def inference(img, dsdg_thresh):
|
|
|
|
| 215 |
input_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
|
| 216 |
|
| 217 |
# Set video codec and create VideoWriter object to save the output video
|
| 218 |
+
fourcc = cv.VideoWriter_fourcc(*'mp4v')
|
| 219 |
output_vid_path = 'output_dsdg.mp4'
|
| 220 |
+
out_dsdg = cv.VideoWriter(output_vid_path, fourcc, 20.0, (input_width, input_height))
|
| 221 |
|
| 222 |
frame_counter = 0
|
| 223 |
confidences_arr = []
|
|
|
|
| 225 |
ret, frame = cap.read()
|
| 226 |
if not ret:
|
| 227 |
break
|
|
|
|
| 228 |
# Process only every 5th frame
|
| 229 |
if frame_counter % 5 == 0:
|
| 230 |
# Run inference on the current frame
|
| 231 |
+
_, _, _, img_dsdg, confidences_dsdg, res_dsdg = inference(frame, dsdg_thresh)
|
| 232 |
+
if res_dsdg == -1:
|
| 233 |
+
continue
|
| 234 |
+
confidences_arr.append(res_dsdg)
|
|
|
|
|
|
|
| 235 |
# Write the DSDG frame to the output video
|
| 236 |
out_dsdg.write(img_dsdg)
|
|
|
|
| 237 |
frame_counter += 1
|
|
|
|
|
|
|
| 238 |
# Release resources
|
| 239 |
cap.release()
|
| 240 |
out_dsdg.release()
|
| 241 |
+
if not confidences_arr:
|
| 242 |
+
return vid_path, {'Not supported right now': 0}, -1, vid_path, 'Faces too small or not found', -1
|
| 243 |
+
avg_conf = sum(confidences_arr) / len(confidences_arr)
|
| 244 |
+
text_dsdg = f'Average real confidence: {avg_conf}\nFrames used: {len(confidences_arr)}\nConfidences:{confidences_arr}'
|
| 245 |
+
return vid_path, {'Not supported right now': 0}, -1, output_vid_path, text_dsdg, avg_conf
|
| 246 |
|
| 247 |
|
| 248 |
+
def upload_to_s3(vid_path, app_version, *labels):
|
| 249 |
folder = 'demo'
|
| 250 |
bucket_name = 'livenessng'
|
| 251 |
+
|
| 252 |
+
if vid_path is None:
|
| 253 |
return 'Error. Take a photo first.'
|
| 254 |
+
elif labels[-2] == -2:
|
| 255 |
return 'Error. Run the detection first.'
|
| 256 |
elif labels[0] is None:
|
| 257 |
return 'Error. Select the true label first.'
|
| 258 |
+
elif labels[0] == 2:
|
| 259 |
+
labels[0] = -1
|
| 260 |
|
| 261 |
# Initialize S3 client
|
| 262 |
s3 = boto3.client('s3')
|
| 263 |
|
| 264 |
+
# Encode labels and app version in video file name
|
| 265 |
encoded_labels = '_'.join([str(int(label)) for label in labels])
|
| 266 |
random_string = str(uuid.uuid4()).split('-')[-1]
|
| 267 |
+
video_name = f"{folder}/{app_version}/{encoded_labels}_{random_string}.mp4"
|
| 268 |
|
| 269 |
+
# Upload video to S3
|
| 270 |
+
with open(vid_path, 'rb') as video_file:
|
| 271 |
+
res = s3.upload_fileobj(video_file, bucket_name, video_name)
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
# Return the S3 URL of the uploaded video
|
|
|
|
|
|
|
| 274 |
status = 'Successfully uploaded'
|
| 275 |
return status
|
| 276 |
|
| 277 |
|
|
|
|
| 278 |
demo = gr.Blocks()
|
| 279 |
|
| 280 |
with demo:
|
| 281 |
with gr.Row():
|
| 282 |
with gr.Column():
|
| 283 |
input_vid = gr.Video(format='mp4', source='webcam')
|
| 284 |
+
dsdg_thresh = gr.Slider(value=DSDG_THRESHOLD, label='DSDG threshold', maximum=3.0, step=0.05)
|
| 285 |
btn_run = gr.Button(value="Run")
|
| 286 |
with gr.Column():
|
| 287 |
outputs=[
|
| 288 |
gr.Video(label='DeePixBiS', format='mp4'),
|
| 289 |
gr.Label(num_top_classes=2, label='DeePixBiS'),
|
| 290 |
+
gr.Number(visible=False, value=-2),
|
| 291 |
gr.Video(label='DSDG', format='mp4'),
|
| 292 |
+
gr.Textbox(label='DSDG'),
|
| 293 |
+
gr.Number(visible=False, value=-2)]
|
| 294 |
with gr.Column():
|
| 295 |
radio = gr.Radio(
|
| 296 |
["Spoof", "Real", "None"], label="True label", type='index')
|