-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam.py
More file actions
115 lines (94 loc) · 4.05 KB
/
webcam.py
File metadata and controls
115 lines (94 loc) · 4.05 KB
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
# !/usr/bin/env python
# Import modules
import cv2
import dlib
import numpy as np
# Import dlib face alignment file
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# Define facial landmarks
landmarks = {'jawline': list(range(0, 17)),
'right_eyebrow': list(range(17, 22)),
'left_eyebrow': list(range(22, 27)),
'nose': list(range(27, 36)),
'right_eye': list(range(36, 42)),
'left_eye': list(range(42, 48)),
'outer_mouth': list(range(48, 60)),
'inner_mouth': list(range(60, 68))}
# Face tracking
crop = np.zeros((256, 256, 3), dtype=np.uint8)
videoCapture = cv2.VideoCapture(0)
while True:
# Read in mirror image video frame
ret, frame = videoCapture.read()
frame = cv2.flip(frame, 1)
# Convert to gray scale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect dlib face rectangles
factor = 4
gray = cv2.resize(gray, None, fx=1 / factor, fy=1 / factor, interpolation=cv2.INTER_LINEAR)
rectangles = detector(gray, 0)
# Track face features if bounding box detected
if rectangles:
# Face shape prediction
shape = predictor(gray, rectangles[0])
coordinates = np.zeros((shape.num_parts, 2), dtype='int')
for x in range(0, shape.num_parts):
coordinates[x] = (shape.part(x).x, shape.part(x).y)
shape = factor * coordinates
# Forehead top and side anchors
forehead_rt = 2 * (shape[19] - shape[36]) + shape[19]
forehead_lt = 2 * (shape[24] - shape[45]) + shape[24]
forehead_rs = 2 * (shape[19] - shape[36]) + shape[0]
forehead_ls = 2 * (shape[24] - shape[45]) + shape[16]
# Forehead anchor midpoints
midpoint_r = [0.25 * (forehead_rt[0] - forehead_rs[0]) + forehead_rs[0],
0.75 * (forehead_rt[1] - forehead_rs[1]) + forehead_rs[1]]
midpoint_l = [0.25 * (forehead_lt[0] - forehead_ls[0]) + forehead_ls[0],
0.75 * (forehead_lt[1] - forehead_ls[1]) + forehead_ls[1]]
# Add forehead anchor points
shape = np.vstack((shape, forehead_rt, forehead_lt, forehead_rs,
forehead_ls, midpoint_r, midpoint_l)).astype(np.int)
# Generate face mask
face_mask = np.zeros(frame.shape[:2])
cv2.fillConvexPoly(face_mask, cv2.convexHull(shape), 1)
# Overlay focused face over blurred background
background = cv2.blur(frame, (50, 50))
background[face_mask.astype(np.bool)] = 0
frame[~face_mask.astype(np.bool)] = 0
frame = frame + background
# Draw landmarks
for feature, points in landmarks.items():
if feature == 'nose':
points += [points[3]]
elif feature == 'jawline':
pass
else:
points += [points[0]]
for i in range(len(points) - 1):
cv2.line(frame, tuple(shape[points[i]]), tuple(shape[points[i+1]]), (0, 0, 255), 2)
# Determine bounding box dimensions
left, top = shape.min(axis=0)
width, height = shape.max(axis=0) - shape.min(axis=0)
left = left if width >= height else left - int((height - width) / 2)
top = top if height >= width else top - int((width - height) / 2)
length = max(width, height)
# Ensure bounding box is within frame
if left < 0:
left = 0
elif left + length >= frame.shape[1]:
left -= frame.shape[1] - left - length + 1
if top < 0:
top = 0
elif top + length >= frame.shape[0]:
top -= frame.shape[0] - top - length + 1
crop = cv2.resize(frame[top:top + length, left:left + length],
(256, 256), interpolation=cv2.INTER_LINEAR)
# Display the resulting frame
cv2.imshow('Video', crop)
# Exit by pressing q
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When finished, release the capture
videoCapture.release()
cv2.destroyAllWindows()