-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
97 lines (63 loc) · 2.71 KB
/
app.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
import cv2
import dlib
import numpy as np
import os
import os.path
video_capture = cv2.VideoCapture(0)
face_detector = dlib.get_frontal_face_detector()
dir_path = os.path.dirname(os.path.realpath(__file__))
landmark_file_path = os.path.realpath(os.path.join(dir_path, './shape_predictor_68_face_landmarks.dat'))
predictor = dlib.shape_predictor(landmark_file_path)
sun_glasses_file = os.path.realpath(os.path.join(dir_path, './images/sunglasses.png'))
def rect_to_bounding_box(rect):
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
return x, y, w, h
def scale_rect(rect, scale):
(x, y, w, h) = rect
return int(x / scale), int(y / scale), int(w / scale), int(h / scale)
def shape_to_np(shape, dtype="int"):
coords = np.zeros((68, 2), dtype=dtype)
for i in range(0, 68):
coords[i] = (shape.part(i).x, shape.part(i).y)
return coords
def scale_point(point, scale):
(x, y) = point
return int(x / scale), int(y / scale)
def add_sun_glasses(target_image, center_x, center_y, width, height=None):
sun_glasses = cv2.imread(sun_glasses_file)
if height is None:
height = int((sun_glasses.shape[0] / sun_glasses.shape[1]) * width)
sun_glasses = cv2.resize(sun_glasses, (width, height), interpolation=cv2.INTER_AREA)
_, mask = cv2.threshold(cv2.cvtColor(sun_glasses, cv2.COLOR_BGR2GRAY), 200, 255, cv2.THRESH_BINARY_INV)
inv_mask = cv2.bitwise_not(mask)
start_x = int(center_x - width / 2)
start_y = int(center_y - height / 2)
glasses_area = target_image[start_y:start_y + height, start_x:start_x + width]
glasses_area_mask = cv2.bitwise_and(glasses_area, glasses_area, mask=inv_mask)
masked_glasses = cv2.bitwise_and(sun_glasses, sun_glasses, mask=mask)
merged = cv2.add(glasses_area_mask, masked_glasses)
target_image[start_y:start_y + height, start_x:start_x + width] = merged
return target_image
def trace_face(frame):
scale = 200 / min(frame.shape[1], frame.shape[0])
thumb = cv2.resize(frame, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(thumb, cv2.COLOR_BGR2GRAY)
faces = face_detector(gray, 1)
for i, face_rect in enumerate(faces):
shape = predictor(gray, face_rect)
shape = shape_to_np(shape)
center_x = int(shape[27][0] / scale)
center_y = int(shape[27][1] / scale)
add_sun_glasses(frame, center_x, center_y, int(abs(shape[17][0] - shape[26][0]) / scale) + 30)
return frame
while True:
ret, frame = video_capture.read()
face_trace_frame = trace_face(frame)
cv2.imshow('Video', face_trace_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()