-
-
Save uiur/eb8538a9c16f7f4078dca59c2c815952 to your computer and use it in GitHub Desktop.
mesen with dlib
This file contains hidden or 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
import sys | |
import os | |
import dlib | |
import glob | |
import cv2 | |
import numpy as np | |
import argparse | |
import base64 | |
import time | |
def image_to_bytes(image): | |
flag, buf = cv2.imencode('.png', image) | |
return buf.tobytes() | |
def point_to_vector(p): | |
return np.array([p.x, p.y]) | |
def draw_black_line(image, positions): | |
PADDING_VERTICAL_RATIO = 0.25 | |
PADDING_HORIZONTAL_RATIO = 0.4 | |
left = point_to_vector(positions['LEFT_EYE']) | |
right = point_to_vector(positions['RIGHT_EYE']) | |
left_top = np.array(left) | |
left_bottom = np.array(left) | |
right_top = np.array(right) | |
right_bottom = np.array(right) | |
horizontal_direction = right - left | |
normal = np.array([horizontal_direction[1], -horizontal_direction[0]], int) | |
normal = normal / np.linalg.norm(normal) | |
# vertical | |
left_height = np.linalg.norm(point_to_vector(positions['LEFT_EYE_BOTTOM_BOUNDARY']) - point_to_vector(positions['LEFT_EYE_TOP_BOUNDARY'])) | |
right_height = np.linalg.norm(point_to_vector(positions['RIGHT_EYE_BOTTOM_BOUNDARY']) - point_to_vector(positions['RIGHT_EYE_TOP_BOUNDARY'])) | |
height = max(left_height, right_height) | |
left_top += np.array(height * PADDING_VERTICAL_RATIO * normal, int) | |
left_bottom -= np.array(height * PADDING_VERTICAL_RATIO * normal, int) | |
right_top += np.array(height * PADDING_VERTICAL_RATIO * normal, int) | |
right_bottom -= np.array(height * PADDING_VERTICAL_RATIO * normal, int) | |
horizontal_pad = np.array(PADDING_HORIZONTAL_RATIO * (right - left), int) | |
left_top -= horizontal_pad | |
left_bottom -= horizontal_pad | |
right_top += horizontal_pad | |
right_bottom += horizontal_pad | |
cv2.fillPoly(image, [np.array([ | |
left_top, | |
left_bottom, | |
right_bottom, | |
right_top, | |
])], color=(0, 0, 0), lineType=cv2.LINE_AA) | |
predictor_path = 'shape_predictor_68_face_landmarks.dat' | |
f = sys.argv[1] | |
def detect_face_landmarks(img, region): | |
predictor = dlib.shape_predictor(predictor_path) | |
shape = predictor(img, region) | |
return { | |
'LEFT_EYE': shape.part(37), | |
'RIGHT_EYE': shape.part(46), | |
'LEFT_EYE_BOTTOM_BOUNDARY': shape.part(42), | |
'LEFT_EYE_TOP_BOUNDARY': shape.part(38), | |
'RIGHT_EYE_BOTTOM_BOUNDARY': shape.part(47), | |
'RIGHT_EYE_TOP_BOUNDARY': shape.part(45), | |
} | |
img = cv2.imread(f) | |
detector = dlib.get_frontal_face_detector() | |
regions = detector(img, 1) | |
for i, region in enumerate(regions): | |
# predictor = dlib.shape_predictor(predictor_path) | |
# shape = predictor(img, region) | |
# for part in shape.parts(): | |
# cv2.circle(img, (part.x, part.y), 1, (0, 255, 0), thickness=-1, lineType=cv2.LINE_AA) | |
position = detect_face_landmarks(img, region) | |
draw_black_line(img, position) | |
sys.stdout.buffer.write(image_to_bytes(img)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment