-
Notifications
You must be signed in to change notification settings - Fork 2
/
facial_landmarks.py
53 lines (41 loc) · 1.26 KB
/
facial_landmarks.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
import cv2
import dlib
import numpy as np
# Open webcam video capturer
cap = cv2.VideoCapture(0)
# Overlay Configurations
color_green = (0,255,0)
line_width = 3
# Loading Face detector
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces from the grayscale image
faces = detector(gray)
for face in faces:
# Draw bounding boxes around faces
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
cv2.rectangle(frame, (x1, y1), (x2, y2), color_green, 3)
# Plot the facial landmark points on the frame
landmarks = predictor(gray, face)
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(frame, (x, y), 4, color_green, -1)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
'''
Run: python3 facial_landmarks.py
'''