Skip to content

Instantly share code, notes, and snippets.

@uiur

uiur/0.py

Created Jan 11, 2017
Embed
What would you like to do?
mesen with dlib
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