LinhVuu/Banking-Advanced-Authentication-System
0
1from pathlib import Path2import os3import streamlit as st4import datetime5from deepface import DeepFace6 7DISTANCE_METRIC = "cosine" # cosine, euclidean, euclidean_l28MODEL_NAME = 'ArcFace' # VGG-Face, Facenet, OpenFace, DeepFace, DeepID, Dlib, ArcFace or Ensemble9DETECTOR_BACKEND = "opencv" # Retinaface, mtcnn, opencv, ssd or dlib10 11 12def face_verfication(image_array):13 """14 Checking user's face image with user face history images15 If one check is True -> True16 """17 result = False18 verifications = DeepFace.verify(img1_path=image_array,19 model_name=MODEL_NAME,20 distance_metric=DISTANCE_METRIC,21 detector_backend=DETECTOR_BACKEND)22 print(f"Verification detail: {verifications}")23 for k, verification in verifications.items():24 if verification["verified"]:25 result = True26 break27 28 return result29 30 31def save_user_image(username, image, input_time):32 print("Save new user image >>>")33 save_dir = f"img/user_image/{username}"34 save_file_path = f"img/user_image/{username}/{username}_{int(input_time)}.jpg"35 # Create folder if not exist36 if not os.path.exists(save_dir):37 print("Path not exist")38 os.makedirs(save_dir)39 # Save image40 with open(save_file_path, mode='wb') as w:41 w.write(image.getbuffer())42 return save_file_path43 44 45def read_user_image(username):46 print("Read user image >>>")47 list_image_path = []48 image_dir = f"img/user_image/{username}"49 # Create folder if not exist50 if not os.path.exists(image_dir):51 os.makedirs(image_dir)52 53 for x in os.listdir(image_dir):54 if x.lower().endswith(".jpg") or x.lower().endswith(".png") or \55 x.lower().endswith(".jpeg"):56 image_path = os.path.join(image_dir, x)57 list_image_path.append(image_path)58 return list_image_path59 60username = "dora"61verification = False # This should be removed after @Thao adds the function62# user historical data verification63 64# @Dora: This is to put the function face verification65if not (verification):66 67 # # Upload photo68 st.info('Please upload your photo')69 img_file_buffer = st.camera_input("Take a picture")70 input_time = datetime.datetime.now().timestamp()71 if img_file_buffer is not None:72 print("We have imgage")73 list_history_image = read_user_image(username)74 image_path = save_user_image(username, img_file_buffer, input_time)75 print(f"Image path after saving: {image_path}")76 if not list_history_image:77 verification = True78 else:79 img_arr = [[image_path, x] for x in list_history_image]80 verification = face_verfication(img_arr)81 print(f"Face verification: {verification}")82 83print(f"Verification: {verification}")84 