Spaces:
Runtime error
Runtime error
| from pathlib import Path | |
| import os | |
| import streamlit as st | |
| import datetime | |
| from deepface import DeepFace | |
| DISTANCE_METRIC = "cosine" # cosine, euclidean, euclidean_l2 | |
| MODEL_NAME = 'ArcFace' # VGG-Face, Facenet, OpenFace, DeepFace, DeepID, Dlib, ArcFace or Ensemble | |
| DETECTOR_BACKEND = "opencv" # Retinaface, mtcnn, opencv, ssd or dlib | |
| 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" | |
| # Create folder if not exist | |
| if not os.path.exists(save_dir): | |
| print("Path not exist") | |
| os.makedirs(save_dir) | |
| # Save image | |
| 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}" | |
| # Create folder if not exist | |
| 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 # This should be removed after @Thao adds the function | |
| # user historical data verification | |
| # @Dora: This is to put the function face verification | |
| if not (verification): | |
| # # Upload photo | |
| 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}") | |