forked from vearutop/face-postgre
-
Notifications
You must be signed in to change notification settings - Fork 2
/
face-add.py
52 lines (43 loc) · 1.93 KB
/
face-add.py
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
import sys
import dlib
import cv2
import face_recognition
import os
import psycopg2
if len(sys.argv) < 2:
print("Usage: face-add <image>")
exit(1)
# Take the image file name from the command line
file_name = sys.argv[1]
# Create a HOG face detector using the built-in dlib class
face_detector = dlib.get_frontal_face_detector()
# Load the image
image = cv2.imread(file_name)
# Run the HOG face detector on the image data
detected_faces = face_detector(image, 1)
print("Found {} faces in the image file {}".format(len(detected_faces), file_name))
if not os.path.exists("./.faces"):
os.mkdir("./.faces")
"host='localhost' dbname='my_database' user='postgres' password='secret'"
connection_db = psycopg2.connect("user='jfaceprojectuser' password='jfaceprojectpassword' host='172.17.0.2' dbname='jfaceprojectdb'")
db=connection_db.cursor()
# Loop through each face we found in the image
for i, face_rect in enumerate(detected_faces):
# Detected faces are returned as an object with the coordinates
# of the top, left, right and bottom edges
print("- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}".format(i, face_rect.left(), face_rect.top(),
face_rect.right(), face_rect.bottom()))
crop = image[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()]
encodings = face_recognition.face_encodings(crop)
if len(encodings) > 0:
query = "INSERT INTO vectors (file, vec_low, vec_high) VALUES ('{}', CUBE(array[{}]), CUBE(array[{}]));".format(
file_name,
','.join(str(s) for s in encodings[0][0:63]),
','.join(str(s) for s in encodings[0][64:127]),
)
db.execute(query)
print(query)
connection_db.commit()
cv2.imwrite("./.faces/aligned_face_{}_{}_crop.jpg".format(file_name.replace('/', '_'), i), crop)
if connection_db is not None:
connection_db.close()