File size: 1,176 Bytes
ce35fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer
import datasets
import gradio as gr

model = SentenceTransformer('clip-ViT-B-16')
dataset = datasets.load_dataset('brendenc/celeb-identities')

def predict(im1, im2):
  
  embeddings = model.encode([im1, im2])
  sim = cosine_similarity(embeddings)
  sim = sim[0, 1]
  if sim > 0.75:
    return sim, "SAME PERSON, UNLOCK PHONE"
  else:
    return sim, "DIFFERENT PEOPLE, DON'T UNLOCK"


interface = gr.Interface(fn=predict, 
                         inputs= [gr.Image(value = dataset['train']['image'][0], type="pil", source="webcam"), 
                                  gr.Image(value = dataset['train']['image'][1], type="pil", source="webcam")], 
                         outputs= [gr.Number(label="Similarity"),
                                   gr.Textbox(label="Message")],
                         title = 'Face ID',
                         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.'
                         )

interface.launch(debug=True)