dassum commited on
Commit
b2f457f
1 Parent(s): ea9edc9

updated app

Browse files
Files changed (1) hide show
  1. app.py +37 -2
app.py CHANGED
@@ -1,9 +1,44 @@
1
 
2
- from sentence_transformers import util,SentenceTransformer
 
 
 
 
 
3
  model = SentenceTransformer('clip-ViT-L-14')
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def predict(im1, im2,inp_sim):
6
- img_emb = model.encode([im1, im2])
 
 
 
7
  sim = util.cos_sim(img_emb[0], img_emb[1])
8
  if sim > inp_sim:
9
  return sim, "SAME PERSON, UNLOCK PHONE"
 
1
 
2
+ from transformers import pipeline
3
+ from PIL import Image, ImageDraw
4
+ from sentence_transformers import util, SentenceTransformer
5
+
6
+ checkpoint = "google/owlvit-base-patch32"
7
+ detector = pipeline(model=checkpoint, task="zero-shot-object-detection")
8
  model = SentenceTransformer('clip-ViT-L-14')
9
 
10
+ def get_face_image(im1):
11
+ predictions = detector(
12
+ im1,
13
+ candidate_labels=["human face"],
14
+ )
15
+ max_score = 0
16
+ box_area = None
17
+ for prediction in predictions:
18
+ box = prediction["box"]
19
+ label = prediction["label"]
20
+ score = prediction["score"]
21
+ if score > max_score :
22
+ xmin, ymin, xmax, ymax = box.values()
23
+ box_area = (xmin, ymin, xmax, ymax)
24
+ max_score = score
25
+ else:
26
+ continue
27
+ draw = ImageDraw.Draw(im1)
28
+ draw.rectangle(box_area, outline="red", width=1)
29
+ draw.text((xmin, ymin), f"{label}: {round(score,2)}", fill="blue")
30
+ crop_img1 = im1.crop(box_area)
31
+ #display(crop_img1)
32
+ newsize = (200, 200)
33
+ face_img1 = crop_img1.resize(newsize)
34
+ #display(face_img1)
35
+ return face_img1
36
+
37
  def predict(im1, im2,inp_sim):
38
+ face_image1 = get_face_image(im1)
39
+ face_image2 = get_face_image(im2)
40
+
41
+ img_emb = model.encode([face_image1, face_image2])
42
  sim = util.cos_sim(img_emb[0], img_emb[1])
43
  if sim > inp_sim:
44
  return sim, "SAME PERSON, UNLOCK PHONE"