•
├── images
│ ├── Barack Obama.png
│ └── Barack Obama 2.png
├── models
│ ├── shape_predictor_68_face_landmarks.dat
│ └── mmod_human_face_detector.dat
├── facial-landmarks-using-dlib.py
├── facial-landmarks-using-face-recognition.py
└── detect-specific-keypoints.py
Last active
April 22, 2020 17:46
-
-
Save KevinPatel04/cbc935bca53d5b0f838826e2a77b3162 to your computer and use it in GitHub Desktop.
Detect & Mark Face Landmarks Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# detect face from image using cnn_face_detector | |
all_face_locations = cnn_face_detector(face_image, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imports required libraries | |
import dlib | |
import cv2 | |
import numpy as np | |
from imutils import face_utils | |
# only for google colab | |
# from google.colab.patches import cv2_imshow | |
# initialize dlib face detector and shape predictor | |
# CNN face detector with mmod_human_face_detector.dat weights | |
weights = 'models/mmod_human_face_detector.dat' | |
cnn_face_detector = dlib.cnn_face_detection_model_v1(weights) | |
predictor_path = 'models/shape_predictor_68_face_landmarks.dat' | |
shape_predictor = dlib.shape_predictor(predictor_path) | |
# detect only jawline | |
def detect_jawline(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(0,17): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
# detect only left eyebrow | |
def detect_left_eyebrow(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(17,22): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
# detect only right eyebrow | |
def detect_right_eyebrow(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(22,27): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
# detect only nose | |
def detect_nose(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(27,36): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
# detect only left eye | |
def detect_left_eye(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(36,42): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
# detect only right eye | |
def detect_right_eye(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(42,48): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
# detect only lips | |
def detect_lips(face_image,keypoints): | |
for index,(x, y) in enumerate(np_keypoints): | |
if index in range(48,68): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
return face_image | |
if __name__ == "__main__": | |
# read image using opencv | |
face_image = cv2.imread('images/Barack Obama.png') | |
# detect face from image using cnn_face_detector | |
all_face_locations = cnn_face_detector(face_image, 1) | |
# loop over every detected face | |
for index, face_location in enumerate(all_face_locations): | |
# pass the face image along with location to shape predictor | |
keypoints = shape_predictor(face_image, face_location.rect) | |
np_keypoints = face_utils.shape_to_np(keypoints) | |
# detect only left eye | |
face_image = detect_left_eye(face_image, np_keypoints) | |
# dispaly results | |
cv2.imshow("Face Landmarks", face_image) | |
# only for google colab | |
# cv2_imshow(face_image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# show the results | |
cv2.imshow("Face Landmarks",face_image) | |
# only for google colab | |
# cv2_imshow(face_image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import dlib | |
import cv2 | |
from imutils import face_utils | |
# only for google colab | |
# from google.colab.patches import cv2_imshow | |
# read image using opencv | |
face_image = cv2.imread('images/Barack Obama.png') | |
# initialize dlib face detector and shape predictor | |
# CNN face detector with mmod_human_face_detector.dat weights | |
weights = 'models/mmod_human_face_detector.dat' | |
cnn_face_detector = dlib.cnn_face_detection_model_v1(weights) | |
predictor_path = 'models/shape_predictor_68_face_landmarks.dat' | |
shape_predictor = dlib.shape_predictor(predictor_path) | |
# detect face from image using cnn_face_detector | |
all_face_locations = cnn_face_detector(face_image, 1) | |
# loop over every detected face | |
for index, face_location in enumerate(all_face_locations): | |
# pass the face image along with location to shape predictor | |
keypoints = shape_predictor(face_image, face_location.rect) | |
# convert keypoints to numpy array | |
np_keypoints = face_utils.shape_to_np(keypoints) | |
# marks each keypoint detected on the face | |
for index,(x, y) in enumerate(np_keypoints): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
# number each point | |
cv2.putText(face_image, str(index), (x+2,y), font, 0.5, (0,255,0), 1) | |
# show the results | |
cv2.imshow("Face Landmarks",face_image) | |
# only for google colab | |
# cv2_imshow(face_image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import dlib | |
import cv2 | |
from imutils import face_utils | |
# only for google colab | |
# from google.colab.patches import cv2_imshow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initialize dlib face detector and shape predictor | |
# CNN face detector with mmod_human_face_detector.dat weights | |
weights = 'models/mmod_human_face_detector.dat' | |
cnn_face_detector = dlib.cnn_face_detection_model_v1(weights) | |
predictor_path = 'models/shape_predictor_68_face_landmarks.dat' | |
shape_predictor = dlib.shape_predictor(predictor_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# loop over every detected face | |
for index, face_location in enumerate(all_face_locations): | |
# pass the face image along with location to shape predictor | |
keypoints = shape_predictor(face_image, face_location.rect) | |
# convert keypoints to numpy array | |
np_keypoints = face_utils.shape_to_np(keypoints) | |
# marks each keypoint detected on the face | |
for index,(x, y) in enumerate(np_keypoints): | |
cv2.circle(face_image,(x,y), 3, (0,0,255), -1) | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
# number each point | |
cv2.putText(face_image, str(index), (x+2,y), font, 0.5, (0,255,0), 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import face_recognition | |
from PIL import Image, ImageDraw | |
# Load the jpg file into a numpy array | |
face_image = face_recognition.load_image_file("images/Barack Obama.png") | |
# get the face landmarks list | |
face_landmarks_list = face_recognition.face_landmarks(face_image) | |
# print all the face landmarks | |
print(face_landmarks_list) | |
# for loop tointerate through all land marks in the list | |
for face_landmarks in face_landmarks_list: | |
# convert numpy array to pil image and create a Draw Object | |
pil_image = Image.fromarray(face_image) | |
d = ImageDraw.Draw(pil_image) | |
# draw green line connecting the dots | |
d.line(face_landmarks['chin'], fill=(0,255,0), width=2) | |
d.line(face_landmarks['left_eyebrow'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['right_eyebrow'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['nose_bridge'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['nose_tip'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['right_eye'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['left_eye'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['top_lip'], fill= (0,255,0), width=2) | |
d.line(face_landmarks['bottom_lip'], fill= (0,255,0), width=2) | |
# display the image | |
pil_image.show() | |
# You can also asave a copy of the new image to diskk | |
pil_image.save("Face_With_Landmarks.jpg") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read image using opencv | |
face_image = cv2.imread('images/Barack Obama.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment