Spaces:
Build error
Build error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
from PIL import Image
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
def predict(im1, im2,thresh,model_name):
|
8 |
+
im1_face = Image.open(im1)
|
9 |
+
im2_face = Image.open(im2)
|
10 |
+
|
11 |
+
model = load_model(model_name)
|
12 |
+
|
13 |
+
sim=cosine_similarity(model.encode([im1_face,im2_face]))[0][1]
|
14 |
+
|
15 |
+
if sim > thresh:
|
16 |
+
return sim, "SAME PERSON, UNLOCK PHONE"
|
17 |
+
else:
|
18 |
+
return sim, "DIFFERENT PEOPLE, DON'T UNLOCK"
|
19 |
+
|
20 |
+
def load_model(model_name):
|
21 |
+
model = SentenceTransformer(model_name)
|
22 |
+
|
23 |
+
title = """<h1 id="title">FaceID for Facial Recognition with Face Detector</h1>"""
|
24 |
+
|
25 |
+
models = ['clip-ViT-B-16','clip-ViT-B-32','clip-ViT-L-14']
|
26 |
+
|
27 |
+
twitter_link = """
|
28 |
+
[](https://twitter.com/nickmuchi)
|
29 |
+
"""
|
30 |
+
|
31 |
+
css = '''
|
32 |
+
h1#title {
|
33 |
+
text-align: center;
|
34 |
+
}
|
35 |
+
'''
|
36 |
+
demo = gr.Blocks(css=css)
|
37 |
+
|
38 |
+
with demo:
|
39 |
+
gr.Markdown(title)
|
40 |
+
gr.Markdown(twitter_link)
|
41 |
+
model_options = gr.Dropdown(choices=models,label='Embedding Models',value=models[-1],show_label=True)
|
42 |
+
thresh = gr.Slider(minimum=0.5,maximum=1,value=0.85,step=0.1,label='Confidence')
|
43 |
+
|
44 |
+
with gr.Tabs():
|
45 |
+
with gr.TabItem("Face ID with No Face Detection"):
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
nd_image_input_1 = gr.Image(label='Image 1',type='pil',source='webcam')
|
50 |
+
nd_image_input_2 = gr.Image(label='Image 2',type='pil',source='webcam')
|
51 |
+
|
52 |
+
with gr.Column():
|
53 |
+
sim = gr.Number(label="Similarity")
|
54 |
+
msg = gr.Textbox(label="Message")
|
55 |
+
|
56 |
+
nd_but = gr.Button('Verify')
|
57 |
+
|
58 |
+
with gr.TabItem("Face ID with Face Detector"):
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
fd_image_1 = gr.Image(label='Image 1',type='pil',source='webcam')
|
63 |
+
fd_image_2 = gr.Image(label='Image 2',type='pil',source='webcam')
|
64 |
+
|
65 |
+
with gr.Column():
|
66 |
+
face_1 = gr.Image(label='Face Detected 1',type='filepath')
|
67 |
+
face_2 = gr.Image(label='Face Detected 2',type='filepath')
|
68 |
+
fd_image_1.change(extract_face,fd_image_1,face_1)
|
69 |
+
fd_image_1.change(extract_face,fd_image_1,face_1)
|
70 |
+
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
sim_1 = gr.Number(label="Similarity")
|
75 |
+
msg_1 = gr.Textbox(label="Message")
|
76 |
+
|
77 |
+
fd_but = gr.Button('Verify')
|
78 |
+
|
79 |
+
|
80 |
+
nd_but.click(predict,inputs=[nd_image_input_1,nd_image_input_2,thresh,model_options],outputs=[sim,msg],queue=True)
|
81 |
+
fd_but.click(predict,inputs=[face_1,face_2,thresh,model_options],outputs=[sim_1,msg_1],queue=True)
|
82 |
+
# interface = gr.Interface(fn=predict,
|
83 |
+
# inputs= [gr.Image(type="pil", source="webcam"),
|
84 |
+
# gr.Image(type="pil", source="webcam")],
|
85 |
+
# outputs= [gr.Number(label="Similarity"),
|
86 |
+
# gr.Textbox(label="Message")]
|
87 |
+
# )
|
88 |
+
|
89 |
+
# interface.launch(debug=True)
|
90 |
+
|
91 |
+
demo.launch(debug=True,enable_queue=True)
|