-
Notifications
You must be signed in to change notification settings - Fork 0
/
face-find.py
54 lines (44 loc) · 2.09 KB
/
face-find.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
53
54
import sys
import dlib
import cv2
import face_recognition
import os
import postgresql
import postgresql.driver as pg_driver
if len(sys.argv) < 2:
print("Usage: face-find <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")
db = pg_driver.connect(user = 'postgres',password = 'postgres', host = 'localhost', port = 5432, database = 'face')
# 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)
threshold = 0.6
if len(encodings) > 0:
query = "SELECT file FROM vectors WHERE sqrt(power(CUBE(array[{}]) <-> vec_low, 2) + power(CUBE(array[{}]) <-> vec_high, 2)) <= {} ".format(
','.join(str(s) for s in encodings[0][0:64]),
','.join(str(s) for s in encodings[0][64:128]),
threshold,
) + \
"ORDER BY sqrt(power(CUBE(array[{}]) <-> vec_low, 2) + power(CUBE(array[{}]) <-> vec_high, 2)) <-> vec_high) ASC LIMIT 1".format(
','.join(str(s) for s in encodings[0][0:64]),
','.join(str(s) for s in encodings[0][64:128]),
)
print(db.query(query))
else:
print("No encodings")