import mediapipe as mp from mediapipe.tasks import python from mediapipe.tasks.python import vision from mediapipe.framework.formats import landmark_pb2 from mediapipe import solutions import numpy as np import time import cv2 import argparse import os import math # modified in gradio from mp_constants import * from mp_utils import divide_line_to_points,points_to_bbox,expand_bbox import logging # for share lib,TODO make module #import sys #sys.path.append("C:\\Users\\owner\\Documents\\pythons\\glibvision") from glibvision.glandmark_utils import bbox_to_glandmarks,convert_to_landmark_group_json from glibvision.cv2_utils import draw_bbox,plot_points,set_plot_text def parse_arguments(): """ 引数 """ parser = argparse.ArgumentParser( description="draw 68 points" ) parser.add_argument( "--input_file","-i",required=True,help="Input file" ) parser.add_argument( "--model_path","-m",default="face_landmarker.task",help="model path" ) parser.add_argument( "--save_glandmark","-g",action="store_true",help="save godot-landmark json" ) parser.add_argument( "--save_group_landmark","-landmark",action="store_true",help="save group-landmark json" ) return parser.parse_args() def draw_landmarks_on_image(rgb_image, detection_result,draw_number=True,font_scale=0.5,text_color=(200,200,200),dot_size=3,dot_color=(255,0,0),line_size=1,line_color=(0,0,355),box_size=1,box_color=(200,200,200)): #print(f"dot_size={dot_size},dot_color={dot_color},line_size={line_size},line_color={line_color}") image_width,iamge_height = rgb_image.size face_landmarks_list = detection_result.face_landmarks annotated_image = np.copy(rgb_image) def get_cordinate(index): x=face_landmarks_list[0][index].x y=face_landmarks_list[0][index].y return x,y def get_distance(x1,y1,x2,y2): return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) def get_centers(): center_indices =[ #(POINT_LEFT_HEAD_OUTER,POINT_RIGHT_HEAD_OUTER,POINT_FOREHEAD_TOP), #(POINT_LEFT_HEAD_OUTER,POINT_RIGHT_HEAD_OUTER,POINT_CHIN_BOTTOM), [POINT_NOSE_CENTER_MIDDLE], #[POINT_LOWER_LIP_CENTER_BOTTOM] #(POINT_UPPER_LIP_CENTER_BOTTOM,POINT_LOWER_LIP_CENTER_TOP) ] centers = [] for indices in center_indices: total_x = 0 total_y = 0 for index in indices: x,y = get_cordinate(index) total_x+=x total_y+=y centers.append ((total_x/len(indices),total_y/len(indices))) return centers centers = get_centers() for center in centers: center_x,center_y = center pt = int(center_x*image_width),int(center_y*iamge_height) #cv2.circle(annotated_image,pt,20,(0,0,255),-1) def get_closed_center(x,y): closed = None closed_distance = 0 for center in centers: distance = get_distance(center[0],center[1],x,y) if closed == None: closed = center closed_distance = distance else: if distancex1: x1=x if y>y1: y1=y if x