CoolFace
Apppublic

Akjava/mediapipe-68-points-facial-landmark

sourceHugging Facemitupdated 2y agoView on Hugging Face
13likes
mp_utils.py94 linesDownload Raw Back to root
1import math2def calculate_distance(p1, p2):3  """4 5  """6  return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)7def to_int_points(points):8    ints=[]9    for pt in points:10        #print(pt)11        value = [int(pt[0]),int(pt[1])]12        #print(value)13        ints.append(value)14    return ints15 16debug = False17def divide_line_to_points(points,divided): # return divided + 118    total_length = 019    line_length_list = []20    for i in range(len(points)-1):21        pt_length = calculate_distance(points[i],points[i+1])22        total_length += pt_length23        line_length_list.append(pt_length)24     25    splited_length = total_length/divided26 27    def get_new_point(index,lerp):28        pt1 = points[index]29        pt2 = points[index+1]30        diff = [pt2[0] - pt1[0], pt2[1]-pt1[1]]31        new_point = [pt1[0]+diff[0]*lerp,pt1[1]+diff[1]*lerp]32        if debug:33          print(f"pt1 ={pt1}  pt2 ={pt2} diff={diff} new_point={new_point}")34 35        return new_point36 37    if debug:38      print(f"{total_length} splitted = {splited_length} line-length-list = {len(line_length_list)}")39    splited_points=[points[0]]40    for i in range(1,divided):41        need_length = splited_length*i42        if debug:43          print(f"{i} need length = {need_length}")44        current_length = 045        for j in range(len(line_length_list)):46            line_length = line_length_list[j]47            current_length+=line_length48            if current_length>need_length:49                if debug:50                  print(f"over need length index = {j} current={current_length}")51                diff = current_length - need_length52            53                lerp_point = 1.0 - (diff/line_length)54                if debug:55                  print(f"over = {diff} lerp ={lerp_point}")56                new_point = get_new_point(j,lerp_point)57                58                splited_points.append(new_point)59                break60        61    splited_points.append(points[-1]) # last one62    splited_points=to_int_points(splited_points)    63         64    if debug:65      print(f"sp={len(splited_points)}")66    return splited_points67 68def points_to_bbox(points):69  x1=float('inf')70  x2=071  y1=float('inf')72  y2=073  for point in points:74    if point[0]<x1:75      x1=point[0]76    if point[0]>x2:77      x2=point[0]78    if point[1]<y1:79      y1=point[1]80    if point[1]>y2:81      y2=point[1]82  return [x1,y1,x2-x1,y2-y1]83 84def expand_bbox(bbox,left=5,top=5,right=5,bottom=5):85   left_pixel = bbox[2]*(float(left)/100)86   top_pixel = bbox[3]*(float(top)/100)87   right_pixel = bbox[2]*(float(right)/100)88   bottom_pixel = bbox[3]*(float(bottom)/100)89   new_box = list(bbox)90   new_box[0] -=left_pixel91   new_box[1] -=top_pixel92   new_box[2] +=left_pixel+right_pixel93   new_box[3] +=top_pixel+bottom_pixel94   return new_box