Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import datasets
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
model = SentenceTransformer('clip-ViT-B-16')
|
7 |
+
dataset = datasets.load_dataset('tadeyina/celeb-identities')
|
8 |
+
|
9 |
+
def predict(im1, im2):
|
10 |
+
|
11 |
+
embeddings = model.encode([im1, im2])
|
12 |
+
sim = cosine_similarity(embeddings)
|
13 |
+
sim = sim[0, 1]
|
14 |
+
if sim > 0.8:
|
15 |
+
return sim, "SAME PERSON, UNLOCK PHONE"
|
16 |
+
else:
|
17 |
+
return sim, "DIFFERENT PEOPLE, DON'T UNLOCK"
|
18 |
+
|
19 |
+
|
20 |
+
interface = gr.Interface(fn=predict,
|
21 |
+
inputs= [gr.Image(value = dataset['train']['image'][0], type="pil", source="webcam"),
|
22 |
+
gr.Image(value = dataset['train']['image'][1], type="pil", source="webcam")],
|
23 |
+
outputs= [gr.Number(label="Similarity"),
|
24 |
+
gr.Textbox(label="Message")],
|
25 |
+
title = 'Face ID',
|
26 |
+
description = 'This app uses emage embeddings and cosine similarity to function as a Face ID application. Cosine similarity is used, so it ranges from -1 to 1.'
|
27 |
+
)
|
28 |
+
|
29 |
+
interface.launch(debug=True)
|