|
from PIL import Image,ImageDraw |
|
|
|
def create_color_image(width, height, color=(255,255,255)): |
|
img = Image.new('RGB', (width, height), color) |
|
return img |
|
|
|
def fill_points(image,points,color=(255,255,255)): |
|
draw = ImageDraw.Draw(image) |
|
int_points = [(int(x), int(y)) for x, y in points] |
|
draw.polygon(int_points, fill=color) |
|
return image |
|
|
|
|
|
def minus_point(pt1,pt2): |
|
return [pt1[0]-pt2[0],pt1[1]-pt2[1]] |
|
|
|
def lerp_point(pt1,pt2,pt2_ratio): |
|
return [int(pt1[0]*(1.0-pt2_ratio)+pt2[0]*pt2_ratio),pt1[1]*(1.0-pt2_ratio)+pt2[1]*pt2_ratio] |
|
|
|
def mean_point(points): |
|
xs = 0 |
|
ys = 0 |
|
for pt in points: |
|
xs +=pt[0] |
|
ys +=pt[1] |
|
return [int(xs/len(points)),int(ys/len(points))] |
|
|
|
def get_face_points(face_landmarks_list): |
|
contour_points=get_landmark_points(face_landmarks_list,PARTS_CONTOUR) |
|
left_eyebrow_points=get_landmark_points(face_landmarks_list,PARTS_LEFT_EYEBROW) |
|
|
|
right_eyebrow_points=get_landmark_points(face_landmarks_list,PARTS_RIGHT_EYEBROW) |
|
|
|
nose_points=get_landmark_points(face_landmarks_list,PARTS_NOSE_BRIDGE) |
|
|
|
diff_right = minus_point(contour_points[1],contour_points[0]) |
|
right_minus_corner = minus_point(contour_points[0] , diff_right) |
|
right_contour = lerp_point(right_minus_corner,left_eyebrow_points[0],0.3) |
|
|
|
diff_left = minus_point(contour_points[15],contour_points[16]) |
|
left_minus_corner = minus_point(contour_points[16] , diff_left) |
|
left_contour = lerp_point(left_minus_corner,right_eyebrow_points[-1],0.3) |
|
|
|
middle_face = mean_point([nose_points[0],right_eyebrow_points[0],left_eyebrow_points[-1]]) |
|
return [right_contour]+list(contour_points)+[left_contour,middle_face] |
|
|
|
|
|
def get_innner_mouth_points(face_landmarks_list): |
|
top_points=get_landmark_points(face_landmarks_list,PARTS_UPPER_LIP) |
|
bottom_points=get_landmark_points(face_landmarks_list,PARTS_LOWER_LIP) |
|
return top_points[7:]+bottom_points[7:] |
|
|
|
|
|
PARTS_UPPER_LIP = "top_lip" |
|
PARTS_LOWER_LIP = "bottom_lip" |
|
PARTS_CONTOUR ="chin" |
|
PARTS_LEFT_EYEBROW ="left_eyebrow" |
|
PARTS_RIGHT_EYEBROW ="right_eyebrow" |
|
PARTS_LEFT_EYE ="left_eye" |
|
PARTS_RIGHT_EYE ="right_eye" |
|
PARTS_NOSE_TIP ="nose_tip" |
|
PARTS_NOSE_BRIDGE ="nose_bridge" |
|
|
|
def get_landmark_points(face_landmarks_list,key): |
|
matching_landmark_points = [] |
|
for face_landmarks in face_landmarks_list: |
|
for landmark_name, landmark_points in face_landmarks.items(): |
|
matching_landmark_points = landmark_points.copy() |
|
if landmark_name ==key: |
|
return tuple(matching_landmark_points) |