-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceface.py
75 lines (55 loc) · 2.04 KB
/
faceface.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
import cv2
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
img = cv2.imread("logo.png")
def overlay_image_alpha(img, img_overlay, pos, alpha_mask):
"""Overlay img_overlay on top of img at the position specified by
pos and blend using alpha_mask.
Alpha mask must contain values within the range [0, 1] and be the
same size as img_overlay.
Ref:
https://stackoverflow.com/questions/14063070/
overlay-a-smaller-image-on-a-larger-image-python-opencv/
14102014#14102014
"""
x, y = pos
x = int(x)
y = int(y)
# Image ranges
y1, y2 = max(0, y), min(img.shape[0], y + img_overlay.shape[0])
x1, x2 = max(0, x), min(img.shape[1], x + img_overlay.shape[1])
# Overlay ranges
y1o, y2o = max(0, -y), min(img_overlay.shape[0], img.shape[0] - y)
x1o, x2o = max(0, -x), min(img_overlay.shape[1], img.shape[1] - x)
# Exit if nothing to do
if y1 >= y2 or x1 >= x2 or y1o >= y2o or x1o >= x2o:
return
channels = img.shape[2]
for c in range(channels):
img[y1: y2, x1: x2, c] = (alpha_mask * img_overlay[y1o: y2o, x1o: x2o, c] +
(1.0 - alpha_mask) * img[y1: y2, x1: x2, c])
return img
while True:
# Capture frames from WebCamera
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(60, 60)
)
# Draw rectangles
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 254), 5)
# Full screen display
frame = overlay_image_alpha(frame, img[:, :, 0: 3], (1, 1), 1)
cv2.namedWindow("FaceFace", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("FaceFace", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow('FaceFace', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Cleaning up
video_capture.release()
cv2.destroyAllWindows()