nickmuchi commited on
Commit
2a9d4ec
·
1 Parent(s): fae29f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py CHANGED
@@ -3,7 +3,41 @@ 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)
 
3
  from sentence_transformers import SentenceTransformer
4
  from PIL import Image
5
  import cv2
6
+ import os
7
 
8
+ def extract_face(im):
9
+
10
+ prototxt_path = 'deploy.prototxt'
11
+ caffemodel_path = 'weights.caffemodel'
12
+
13
+ # Read the model
14
+ cv2_model = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)
15
+
16
+ #pil_image = PIL.Image.open('image.jpg')
17
+ image = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
18
+ #image = cv2.imread(im)
19
+
20
+ (h, w) = image.shape[:2]
21
+ blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
22
+
23
+ cv2_model.setInput(blob)
24
+ detections = cv2_model.forward()
25
+
26
+ # Identify each face
27
+ for i in range(0, detections.shape[2]):
28
+ box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
29
+ (startX, startY, endX, endY) = box.astype("int")
30
+
31
+ confidence = detections[0, 0, i, 2]
32
+
33
+ # If confidence > 0.5, save it as a separate file
34
+ if (confidence > 0.5):
35
+ frame = image[startY:endY, startX:endX]
36
+ #PIL_image = Image.fromarray(frame)
37
+ file_name = 'faces/' + str(np.random.randint(1,10)) + '_' + 'face.png'
38
+ cv2.imwrite(file_name, frame)
39
+
40
+ return file_name
41
  def predict(im1, im2,thresh,model_name):
42
  im1_face = Image.open(im1)
43
  im2_face = Image.open(im2)