|
from pathlib import Path |
|
import os |
|
import streamlit as st |
|
import datetime |
|
from deepface import DeepFace |
|
|
|
DISTANCE_METRIC = "cosine" |
|
MODEL_NAME = 'ArcFace' |
|
DETECTOR_BACKEND = "opencv" |
|
|
|
|
|
def face_verfication(image_array): |
|
""" |
|
Checking user's face image with user face history images |
|
If one check is True -> True |
|
""" |
|
result = False |
|
verifications = DeepFace.verify(img1_path=image_array, |
|
model_name=MODEL_NAME, |
|
distance_metric=DISTANCE_METRIC, |
|
detector_backend=DETECTOR_BACKEND) |
|
print(f"Verification detail: {verifications}") |
|
for k, verification in verifications.items(): |
|
if verification["verified"]: |
|
result = True |
|
break |
|
|
|
return result |
|
|
|
|
|
def save_user_image(username, image, input_time): |
|
print("Save new user image >>>") |
|
save_dir = f"img/user_image/{username}" |
|
save_file_path = f"img/user_image/{username}/{username}_{int(input_time)}.jpg" |
|
|
|
if not os.path.exists(save_dir): |
|
print("Path not exist") |
|
os.makedirs(save_dir) |
|
|
|
with open(save_file_path, mode='wb') as w: |
|
w.write(image.getbuffer()) |
|
return save_file_path |
|
|
|
|
|
def read_user_image(username): |
|
print("Read user image >>>") |
|
list_image_path = [] |
|
image_dir = f"img/user_image/{username}" |
|
|
|
if not os.path.exists(image_dir): |
|
os.makedirs(image_dir) |
|
|
|
for x in os.listdir(image_dir): |
|
if x.lower().endswith(".jpg") or x.lower().endswith(".png") or \ |
|
x.lower().endswith(".jpeg"): |
|
image_path = os.path.join(image_dir, x) |
|
list_image_path.append(image_path) |
|
return list_image_path |
|
|
|
username = "dora" |
|
verification = False |
|
|
|
|
|
|
|
if not (verification): |
|
|
|
|
|
st.info('Please upload your photo') |
|
img_file_buffer = st.camera_input("Take a picture") |
|
input_time = datetime.datetime.now().timestamp() |
|
if img_file_buffer is not None: |
|
print("We have imgage") |
|
list_history_image = read_user_image(username) |
|
image_path = save_user_image(username, img_file_buffer, input_time) |
|
print(f"Image path after saving: {image_path}") |
|
if not list_history_image: |
|
verification = True |
|
else: |
|
img_arr = [[image_path, x] for x in list_history_image] |
|
verification = face_verfication(img_arr) |
|
print(f"Face verification: {verification}") |
|
|
|
print(f"Verification: {verification}") |
|
|