-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacial_landmarks.py
130 lines (93 loc) · 3.41 KB
/
facial_landmarks.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Dlib
import dlib
import cv2
import timeit
import numpy as np
# From https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
# From imutils - https://github.com/jrosebr1/imutils
def rect_to_bb(rect):
# take a bounding predicted by dlib and convert it
# to the format (x, y, w, h) as we would normally do
# with OpenCV
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
# return a tuple of (x, y, w, h)
return (x, y, w, h)
def shape_to_np(shape, dtype="int"):
# initialize the list of (x, y)-coordinates
N = shape.num_parts
coords = np.zeros((N, 2), dtype=dtype)
# loop over the N facial landmarks and convert them
# to a 2-tuple of (x, y)-coordinates
for i in range(0, N):
coords[i] = (shape.part(i).x, shape.part(i).y)
# coords = [(shape.part(i).x, shape.part(i).y) for i in range(N)]
# return the list of (x, y)-coordinates
return coords
# OpenCV
cap = cv2.VideoCapture(0)
# cap.set(cv2.CAP_PROP_FRAME_WIDTH,640);
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480);
# Dlib
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')
# predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
N = 5
list_fps_det = [30]*N
list_fps_cam = [30]*N
k = 0
cam_stop = timeit.default_timer()
while(True):
k += 1
# Capture frame-by-frame
ret, frame = cap.read()
cam_start = cam_stop
cam_stop = timeit.default_timer()
start = cam_stop
frame = cv2.flip(frame,1)
## Dlib face detection
# scale = 0.5
# scale = 0.33
scale = 0.25
frame_small = cv2.resize(frame, (0,0), fx=scale, fy=scale)
frame_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2GRAY)
if(k==1):
print("(h,w,c) original = ",frame.shape)
print("(h,w,c) small = ",frame_small.shape)
rects = detector(frame_small)
for rect in rects:
rect = dlib.rectangle(round(rect.left()/scale),round(rect.top()/scale),round(rect.right()/scale),round(rect.bottom()/scale))
# print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
# i, d.left(), d.top(), d.right(), d.bottom()))
# cv2.rectangle(frame,(rect.left(),rect.top()),(rect.right(),rect.bottom()),(0,255,255),1)
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = predictor(frame, rect)
shape = shape_to_np(shape)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image
for (x, y) in shape:
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1, cv2.LINE_AA)
# Our operations on the frame come here
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
stop = timeit.default_timer()
det_fps = 1/(stop-start)
cam_fps = 1/(cam_stop-cam_start)
list_fps_det[k % N] = det_fps
list_fps_cam[k % N] = cam_fps
# print("FPS:", 1/(stop-start))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "fps detection: {0:.1f}".format(sum(list_fps_det)/N), (0, 13), font, 0.5, (0, 255, 0), 1, cv2.LINE_AA)
cv2.putText(frame, "fps camera: {0:.1f}".format(sum(list_fps_cam)/N), (0, 30), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
# Hack not to "hang" the window in *nix systems (Linux,Mac)
cv2.waitKey(1)