-
Notifications
You must be signed in to change notification settings - Fork 0
/
facialLandmarks.py
84 lines (70 loc) · 2.75 KB
/
facialLandmarks.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
import cv2
import numpy as np
import dlib
webcam = False
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def empty(a):
pass
cv2.namedWindow("BGR")
cv2.resizeWindow("BGR", 640, 240)
cv2.createTrackbar("Blue", "BGR", 0, 255, empty)
cv2.createTrackbar("Green", "BGR", 0, 255, empty)
cv2.createTrackbar("Red", "BGR", 0, 255, empty)
def createBox(img, points, scale=5, masked=False, cropped=True):
if masked:
mask = np.zeros_like(img)
mask = cv2.fillPoly(mask, [points], (255,255,255))
img = cv2.bitwise_and(img, mask)
# cv2.imshow('Mask', img)
if cropped:
bbox = cv2.boundingRect(points)
x,y,w,h = bbox
imgCrop = img[y:y+h, x:x+h]
imgCrop = cv2.resize(imgCrop, (0,0), None, scale, scale)
return imgCrop
else:
return mask
while True:
if webcam:
success, img = cap.read()
else:
img = cv2.imread('me5.jpg')
img = cv2.resize(img, (0,0), None, 0.5, 0.5)
imgOriginal = img.copy()
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector(imgGray)
for face in faces:
x1, y1 = face.left(), face.top()
x2, y2 = face.right(), face.bottom()
imgOriginal = cv2.rectangle(img, (x1,y1), (x2,y2), (255,0,0), 2)
landmarks = predictor(imgGray, face)
myPoints = []
for n in range(68):
x = landmarks.part(n).x
y = landmarks.part(n).y
myPoints.append([x,y])
# cv2.circle(imgOriginal, (x,y), 5, (50,50,255), cv2.FILLED)
# cv2.putText(imgOriginal, str(n), (x,y-10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8,
# (0,255,0), 1)
myPoints = np.array(myPoints)
imgLeftEye = createBox(img, myPoints[36:42])
imgLips = createBox(img, myPoints[48:61], 3, masked=True, cropped=False)
# cv2.imshow('Left Eye', imgLeftEye)
# cv2.imshow('Lips', imgLips)
imgColorLips = np.zeros_like(imgLips)
b = cv2.getTrackbarPos('Blue', 'BGR')
g = cv2.getTrackbarPos('Green', 'BGR')
r = cv2.getTrackbarPos('Red', 'BGR')
imgColorLips[:] = b, g, r
imgColorLips = cv2.bitwise_and(imgLips, imgColorLips)
imgColorLips = cv2.GaussianBlur(imgColorLips, (7,7), 10)
imgOriginalGray = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY)
imgOriginalGray = cv2.cvtColor(imgOriginalGray, cv2.COLOR_GRAY2BGR)
imgColorLips = cv2.addWeighted(imgOriginalGray, 1, imgColorLips, 0.4, 0)
cv2.imshow('Colored Lips', imgColorLips)
cv2.imshow('Original Image', imgOriginal)
# cv2.imshow('Left Eye', imgLeftEye)
# cv2.imshow('Lips', imgLips)
cv2.waitKey(1)