File size: 2,893 Bytes
c44d66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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}")