File size: 2,074 Bytes
9f5a1dc
b2f457f
 
9f5a1dc
a7a8193
b2f457f
 
6b49d7b
 
b2f457f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f5a1dc
b2f457f
 
9f5a1dc
b2f457f
 
 
 
a74b809
b2f457f
 
 
 
6b49d7b
a74b809
6b49d7b
 
 
 
a7a8193
 
 
 
 
 
 
 
 
 
 
 
 
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
from sentence_transformers import util
from transformers import pipeline
from PIL import Image, ImageDraw
from sentence_transformers import util,SentenceTransformer
import gradio as gr
checkpoint = "google/owlvit-base-patch32"
detector = pipeline(model=checkpoint, task="zero-shot-object-detection")
model = SentenceTransformer('clip-ViT-L-14')

def get_face_image(im1):
  predictions = detector(
    im1,
    candidate_labels=["human face"],
  )
  max_score = 0
  box_area = None
  for prediction in predictions:
      box = prediction["box"]
      label = prediction["label"]
      score = prediction["score"]
      if score > max_score :
        xmin, ymin, xmax, ymax = box.values()
        box_area = (xmin, ymin, xmax, ymax)
        max_score = score
      else:
        continue
      draw = ImageDraw.Draw(im1)
      draw.rectangle(box_area, outline="red", width=1)
      #draw.text((xmin, ymin), f"{label}: {round(score,2)}", fill="blue")
      crop_img1 = im1.crop(box_area)
      #display(crop_img1)
      newsize = (256, 256)
      face_img1 = crop_img1.resize(newsize)
      #display(face_img1)
  return face_img1

def predict(im1, im2,inp_sim):
  face_image1 = get_face_image(im1)
  face_image2 = get_face_image(im2)

  img_emb = model.encode([face_image1, face_image2])
  sim = util.cos_sim(img_emb[0], img_emb[1])
  if sim > inp_sim:
    return sim, "SAME PERSON, UNLOCK PHONE"
  else:
    return sim, "DIFFERENT PEOPLE, DON'T UNLOCK"


description = "An application that can recognize if two faces belong to the same person or not"
title = "Facial Identity Recognition System"

interface = gr.Interface(fn=predict, 
                         inputs= [gr.Image(type="pil", source="webcam"), 
                                  gr.Image(type="pil"),
                                  gr.Slider(0, 1, value=0.8, label="Similarity Percentage", info="Choose betwen 0 and 1")], 
                         outputs= [gr.Number(label="Similarity"),
                                   gr.Textbox(label="Message")]
                         )

interface.launch(debug=True)