Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Created May 8, 2020 23:07
Show Gist options
  • Save rajathithan/f47bc0618bb452d3f9ef30e9c13e2f87 to your computer and use it in GitHub Desktop.
Save rajathithan/f47bc0618bb452d3f9ef30e9c13e2f87 to your computer and use it in GitHub Desktop.
Dlib's 68 point face detection
import cv2
import imutils
import dlib
import sys
from renderface2 import renderFace2
# Capture the webcam feed
cap = cv2.VideoCapture(0)
# 68 point face model
PREDICTOR_PATH = "./models/shape_predictor_68_face_landmarks.dat"
# face detector
detector = dlib.get_frontal_face_detector()
# 68 points predictor
predictor = dlib.shape_predictor(PREDICTOR_PATH)
while(True):
# Read the frame
ret, frame = cap.read()
if ret == True:
pass
else:
print("Unable to read frame")
sys.exit()
#Resize the frame
frame = imutils.resize(frame, width = 640)
frame = imutils.resize(frame, height = 480)
#Convert to RGB
framecvt = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Detect faces in the frame
faces = detector(framecvt,0)
# Iterate over faces in the frame
for face in faces:
newRect = dlib.rectangle(int(face.left()), int(face.top()),int(face.right()), int(face.bottom()))
# Find face landmarks by providing reactangle for each face
shape = predictor(framecvt, newRect)
# Draw facial landmarks
renderFace2(frame, shape)
# Display the resulting frame
winname = '68 Point detection'
cv2.imshow(winname,frame)
#Waits for a user input to quit the application
key = cv2.waitKey(10)
mouse = cv2.getWindowProperty(winname,cv2.WND_PROP_VISIBLE)
if key == 27 or mouse < 1:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment