Skip to content

Instantly share code, notes, and snippets.

@biranchi2018
Created July 17, 2018 21:50
Show Gist options
  • Save biranchi2018/b68fddba7128490f45aacd18abe8a29c to your computer and use it in GitHub Desktop.
Save biranchi2018/b68fddba7128490f45aacd18abe8a29c to your computer and use it in GitHub Desktop.
Face Landmark Detection
from imutils import face_utils
import dlib
import cv2
import time
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
cap = cv2.VideoCapture(0)
cap.set(3,500)
cap.set(4,400)
time.sleep(2)
cap.set(15, -8.0)
p = "models/shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
def apply_invert(frame):
return cv2.bitwise_not(frame)
while True:
ret, image = cap.read()
invert_image = apply_invert(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
# loop over the face detections
for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image
for (x, y) in shape:
cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
cv2.circle(invert_image, (x, y), 2, (0, 255, 0), -1)
# show the output image with the face detections + facial landmarks
cv2.imshow("Landmarks", image)
cv2.imshow("Inverted", invert_image)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment