-
Notifications
You must be signed in to change notification settings - Fork 4
/
face.py
66 lines (52 loc) · 2.26 KB
/
face.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
#"Rayo emprendedor" filter
#Based on Instagram Face Filter
#Original by Sergio Canu on https://pysource.com/2019/03/25/pigs-nose-instagram-face-filter-opencv-with-python/
#Face landmark map by Sergio Canu
#Modified by Manuel Cota
import cv2 as cv
from math import hypot
def face_tracker(frame, l_image,rens,cols,face_mask, detector, predictor, gray_frame, faces):
cont = 0
for face in faces:
cont+=1
if cont < 1:
return frame
for face in faces:
landmarks = predictor(gray_frame, face)
#Face coords
top_face = (landmarks.part(21).x + 1000,
landmarks.part(27).y + 1000)
center_face = (landmarks.part(27).x,
landmarks.part(27).y)
left_face = (landmarks.part(21).x,
landmarks.part(21).y)
right_face = (landmarks.part(22).x,
landmarks.part(22).y)
face_width = int(hypot(left_face[0] -
right_face[0],
left_face[1] -
right_face[1]) * 1.77)
face_height = int(face_width * 2.2)
#New face position
top_left = (int (center_face[0] - face_width / 2),
int(center_face[1] - face_height / 2))
bottom_right = (int(center_face[0] + face_width / 2),
int(center_face[1] + face_height / 2))
#Adding new face
face_light = cv.resize(l_image, (face_width, face_height))
face_light_gray = cv.cvtColor(face_light, cv.COLOR_BGR2GRAY)
_, face_mask = cv.threshold(face_light_gray,
25,
255,
cv.THRESH_BINARY_INV)
face_area = frame[top_left[1]: top_left[1] +
face_height,top_left[0]: top_left[0] +
face_width]
face_area_no_face = frame[top_left[1]: top_left[1] +
face_height, top_left[0]: top_left[0] +
face_width]
final_face = cv.add(face_area_no_face, face_light)
frame[top_left[1]: top_left[1] +
face_height,top_left[0]: top_left[0] +
face_width] = final_face
return frame