-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_fiducials.py
87 lines (62 loc) · 2.41 KB
/
face_fiducials.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import cv2
import dlib
import numpy as np
from imutils import face_utils as utils
def face_fiducials(frame):
imp_pts = []
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
hog_face_detector = dlib.get_frontal_face_detector()
dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
faces = hog_face_detector(gray, 1)
total_faces = len(faces)
face_landmarks_list = []
hull_list = []
# print("Total Faces in current Frame:-> ", total_faces)
for i,face in enumerate(faces):
face_landmarks = dlib_facelandmark(gray, face)
face_landmarks = utils.shape_to_np(face_landmarks)
face_landmarks_list.append(face_landmarks)
# print(" Face LandMarks ", len(face_landmarks))
(p, q, w, h) = utils.rect_to_bb(face)
cv2.rectangle(frame, (p, q), (p + w, q + h), (0,255,255), 1)
# if visualization == True:
for (p,q) in face_landmarks:
cv2.circle(frame, (p, q), 2, (0, 0, 255), -1)
imp_pts.append((p,q))
hull = cv2.convexHull(np.array(imp_pts), False)
hull = np.reshape(hull, (hull.shape[0], hull.shape[2]))
hull_list.append(hull)
# print(" Hull List ", hull_list)
# cv2.imshow("Face Landmarks", frame)
# print(len(imp_pts))
# key = cv2.waitKey(0)
# if key == 27:
# break
#
# cv2.destroyAllWindows()
return total_faces, imp_pts, hull_list
def twoFaces(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
rects = detector(gray,1)
points = []
faces = []
hull_list = []
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
num_faces = len(rects)
if(num_faces==2):
for (i,rect) in enumerate(rects):
shape = predictor(gray,rect)
shape = utils.shape_to_np(shape)
(x,y,w,h) = utils.rect_to_bb(rect)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
for (x,y) in shape:
cv2.circle(img,(x,y),2,(0,0,255),-1)
points.append((x,y))
hull = cv2.convexHull(np.array(points), False)
hull = np.reshape(hull, (hull.shape[0], hull.shape[2]))
hull_list.append(hull)
faces.append(points)
points = []
return num_faces, faces, hull_list