File size: 4,668 Bytes
f347761
 
500f010
 
f347761
 
 
500f010
f347761
 
 
 
5f0e788
 
500f010
 
 
5f0e788
f347761
 
 
 
 
 
 
500f010
 
 
818f301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf8e2a
 
343b529
818f301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500f010
 
818f301
500f010
 
 
 
 
 
 
 
818f301
 
500f010
 
b97e62c
a40427c
500f010
 
 
b97e62c
500f010
 
b97e62c
 
 
818f301
 
b97e62c
 
 
343b529
b97e62c
 
 
 
 
 
 
63eb88f
 
 
 
 
 
 
 
b97e62c
500f010
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import gradio as gr

import numpy as np
from deepface import DeepFace
from pymongo.mongo_client import MongoClient

credentials = "jamshaid:jamshaid19gh"

uri = f"mongodb+srv://{credentials}@cluster0.uimyui3.mongodb.net/?retryWrites=true&w=majority"
client = MongoClient(uri)
db = client["Face_identification"]
identities_collection = db["face_identities"]
model_name="Facenet"
debug=False

def save_identity(image , name):
  try:
    embeddings = DeepFace.represent(image , model_name=model_name)
    embeddings = embeddings[0]

    identity = {"embeddings":embeddings["embedding"] , "name" : name }

    result = identities_collection.insert_one(identity)

    return str(result)
  except Exception as error:
    return str(error)

def findCosineDistance(source_representation, test_representation):
    a = np.matmul(np.transpose(source_representation), test_representation)
    b = np.sum(np.multiply(source_representation, source_representation))
    c = np.sum(np.multiply(test_representation, test_representation))
    return 1 - (a / (np.sqrt(b) * np.sqrt(c)))

def findThreshold(model_name, distance_metric):

    base_threshold = {"cosine": 0.40, "euclidean": 0.55, "euclidean_l2": 0.75}

    thresholds = {
        "VGG-Face": {"cosine": 0.40, "euclidean": 0.60, "euclidean_l2": 0.86},
        "Facenet": {"cosine": 0.40, "euclidean": 10, "euclidean_l2": 0.80},
        "Facenet512": {"cosine": 0.30, "euclidean": 23.56, "euclidean_l2": 1.04},
        "ArcFace": {"cosine": 0.68, "euclidean": 4.15, "euclidean_l2": 1.13},
        "Dlib": {"cosine": 0.07, "euclidean": 0.6, "euclidean_l2": 0.4},
        "SFace": {"cosine": 0.593, "euclidean": 10.734, "euclidean_l2": 1.055},
        "OpenFace": {"cosine": 0.10, "euclidean": 0.55, "euclidean_l2": 0.55},
        "DeepFace": {"cosine": 0.23, "euclidean": 64, "euclidean_l2": 0.64},
        "DeepID": {"cosine": 0.015, "euclidean": 45, "euclidean_l2": 0.17},
    }

    threshold = thresholds.get(model_name, base_threshold).get(distance_metric, 0.4)

    return threshold

threshold = findThreshold(model_name , "cosine")

def predict_image(image):
    if debug: 
      print("1")
    # getting face embeddings from database
    results = identities_collection.find()
    faces = [dict(result) for result in results]

    if debug:
      print("2")

    # generate face embeddings for all detected faces in image
    target_embedding_array = DeepFace.represent(
        img_path=image,
        model_name=model_name,
    )
    identities = []
    # for each face compare its embeddings with all face embeddings in database
    for target_embedding_obj in target_embedding_array:
        target_embedding = target_embedding_obj["embedding"]

        if debug:
          print("4")
        # compare the face embedding with all other faces

        name = "Unknown"
        for face in faces:
          distance = findCosineDistance(face["embeddings"], target_embedding)
          if distance <= threshold:
            name = face["name"]
            break

        if debug:
          print("5")

        identities.append({"name":name , "facial_area":target_embedding_obj["facial_area"]})  
    
    return str(identities)


# image_input = gr.inputs.Image(shape=(160,160))


# Create the Gradio interface
# gr.Interface(fn=predict_image, inputs=image_input, outputs=label_output).launch()


# Create Gradio interfaces for input and output
image_input = gr.inputs.Image(shape=(160, 160))
label_input = gr.inputs.Textbox(label="Enter Label")
label_output = gr.outputs.Textbox()


# Create the Gradio interface
interface1 = gr.Interface(
    fn=save_identity,
    inputs=[image_input, label_input],
    outputs=label_output,
    title="Face Identification",
    description="Upload an image, enter the person name and store the person in database",
)


# Create Gradio interfaces for image input and output
image_input2 = gr.inputs.Image(shape=(None, None))
# output_image = gr.outputs.Image(type="numpy")
output_image = gr.outputs.Textbox()

# Create the Gradio interface for image input and output
interface2 = gr.Interface(
    fn=predict_image,
    inputs=image_input2,
    outputs=output_image,
    title="Face Identification",
    description="Upload an image and get the identity of person",
)

# Create the Gradio interface with two tabs
# interface = gr.Interface(title="Face identification App")
# interface.add_view(interface1, "Save", "Add new person")
# interface.add_view(interface2, "Predict", "Get identity of person")

gr.TabbedInterface(
    [interface1 , interface2],
    tab_names=["Predict Persons","Add new Person"]
).queue().launch(   )

# Launch the Gradio interface
interface.launch()