-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglasses.py
More file actions
102 lines (93 loc) · 3.46 KB
/
glasses.py
File metadata and controls
102 lines (93 loc) · 3.46 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
import cv2
import os
import face_recognition as fr
from PIL import Image
def load_resources(path):
files = os.listdir(path)
templates = []
for file_path in files:
if '.png' in file_path:
relative_path = os.path.join(path, file_path)
templates.append(cv2.imread(relative_path, -1))
print (templates[-1].shape)
return templates
def add_glasses(img, glasses, rate, cnt):
face_cascade = cv2.CascadeClassifier('/home/york_io/.local/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/york_io/.local/lib/python3.6/site-packages/cv2/data/haarcascade_eye.xml')
face_locs = []
face_locs = []
hat_locs = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_locs = face_cascade.detectMultiScale(gray, 1.3, 5)
for loc in face_locs:
x, y, w, h = loc
roi_gray = gray[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
if (len(eyes) < 2):
continue
expected_y = h // 3
best_y = 123456789
for eye in eyes:
if abs(y - expected_y) < abs(best_y - expected_y):
best_y = eye[1]
gw = int(w * 0.9)
delta = (w - gw) // 2
print(glasses.shape)
gh = int(gw / glasses.shape[1] * glasses.shape[0])
gx = x + int(delta * 1.2)
gy = y + expected_y - int(0.15 * gh)
if gx < 0 or gy < 0:
continue
glass_img = cv2.resize(glasses, (gw, gh))
for yi in range(gy, gy + gh):
for xi in range(gx, gx + gw):
if glass_img[yi - gy, xi - gx, 3] != 0:
img[yi, xi] = glass_img[yi - gy, xi - gx, 0:3]
return face_locs, hat_locs, img
def add_hat(img, hat_templ, rate, cnt):
face_cascade = cv2.CascadeClassifier('/home/york_io/.local/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml')
face_locs = []
hat_locs = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_locs = face_cascade.detectMultiScale(gray, 1.3, 5)
for loc in face_locs:
width_scaled = int(loc[2] * 1.5)
width_original = hat_templ.shape[1]
scale_factor = width_scaled / width_original
estimated_height = int(hat_templ.shape[0] * scale_factor / 1.5)
delta = int(0.2 * estimated_height)
x, y, w, h = loc
hat_locs.append([y - estimated_height + delta, x + w, y + delta, x])
for hat_loc in hat_locs:
if hat_loc[0] < 0:
continue
t, r, b, l = hat_loc
w = r - l
h = b - t
hat_img = cv2.resize(hat_templ, (w, h))
for y in range(t, b):
for x in range(l, r):
if hat_img[y - t, x - l, 3] != 0:
img[y, x] = hat_img[y - t, x - l, 0:3]
return face_locs, hat_locs, img
def show_webcam(templ, process, rate = 5):
cam = cv2.VideoCapture(0)
cnt = 0
face_locs = []
while True:
ret_val, img = cam.read()
face_locs, obj_locs, img = process(img, templ, rate, cnt)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cnt += 1
cv2.destroyAllWindows()
def main():
hat_templ = load_resources('hat_resources')
glass_templ = load_resources('glass_resources')
for t in glass_templ:
show_webcam(t, add_glasses, rate=3)
for t in hat_templ:
show_webcam(t, add_hat, rate=3)
if __name__ == '__main__':
main()