a =jg(@s.ddlZddlZddlZddlmZz ddlZWn(eyXedede Yn0de_ e Z e ZeeZeZeeZeZeeZeZeeZddZdd Zd d Zd d Zd'ddZ d(ddZ!d)ddZ"d*ddZ#d+ddZ$d,ddZ%d-dd Z&d.d"d#Z'd/d%d&Z(dS)0N) ImageFilez\Please install `face_recognition_models` with this command before using `face_recognition`: zCpip install git+https://github.com/ageitgey/face_recognition_modelsTcCs||||fS)z Convert a dlib 'rect' object to a plain tuple in (top, right, bottom, left) order :param rect: a dlib 'rect' object :return: a plain tuple representation of the rect in (top, right, bottom, left) order )toprightbottomleft)rectr>/usr/local/lib/python3.9/site-packages/face_recognition/api.py _rect_to_css sr cCs t|d|d|d|dS)z Convert a tuple in (top, right, bottom, left) order to a dlib `rect` object :param css: plain tuple representation of the rect in (top, right, bottom, left) order :return: a dlib `rect` object r)dlibZ rectangle)cssrrr _css_to_rect*srcCs<t|ddt|d|dt|d|dt|ddfS)aX Make sure a tuple in (top, right, bottom, left) order is within the bounds of the image. :param css: plain tuple representation of the rect in (top, right, bottom, left) order :param image_shape: numpy shape of the image array :return: a trimmed plain tuple representation of the rect in (top, right, bottom, left) order rr r r )maxmin)rZ image_shaperrr _trim_css_to_bounds4srcCs*t|dkrtdStjj||ddS)a Given a list of face encodings, compare them to a known face encoding and get a euclidean distance for each comparison face. The distance tells you how similar the faces are. :param faces: List of face encodings to compare :param face_to_compare: A face encoding to compare against :return: A numpy ndarray with the distance for each face in the same order as the 'faces' array rr )Zaxis)lennpemptyZlinalgZnorm)face_encodingsZface_to_comparerrr face_distance?s  rRGBcCs$tj|}|r||}t|S)a# Loads an image file (.jpg, .png, etc) into a numpy array :param file: image file name or file object to load :param mode: format to convert the image to. Only 'RGB' (8-bit RGB, 3 channels) and 'L' (black and white) are supported. :return: image contents as numpy array )PILZImageopenconvertrarray)filemodeZimrrr load_image_fileNs  r r hogcCs |dkrt||St||SdS)a Returns an array of bounding boxes of human faces in a image :param img: An image (as a numpy array) :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces. :param model: Which face detection model to use. "hog" is less accurate but faster on CPUs. "cnn" is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is "hog". :return: A list of dlib 'rect' objects of found face locations cnnN)cnn_face_detector face_detectorimgnumber_of_times_to_upsamplemodelrrr _raw_face_locations\s  r)cs@|dkr"fddt|dDSfddt||DSdS)a7 Returns an array of bounding boxes of human faces in a image :param img: An image (as a numpy array) :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces. :param model: Which face detection model to use. "hog" is less accurate but faster on CPUs. "cnn" is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is "hog". :return: A list of tuples of found face locations in css (top, right, bottom, left) order r"csg|]}tt|jjqSrrr rshape.0Zfacer&rr wz"face_locations..csg|]}tt|jqSr)rr r+r,r.rr r/yr0N)r)r%rr.r face_locationsls r1cCst|||dS)aa Returns an 2d array of dlib rects of human faces in a image using the cnn face detector :param img: A list of images (each as a numpy array) :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces. :return: A list of dlib 'rect' objects of found face locations ) batch_size)r#)imagesr'r3rrr _raw_face_locations_batched|sr5cs&fdd}t||}tt||S)a Returns an 2d array of bounding boxes of human faces in a image using the cnn face detector If you are using a GPU, this can give you much faster results since the GPU can process batches of images at once. If you aren't using a GPU, you don't need this function. :param images: A list of images (each as a numpy array) :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces. :param batch_size: How many images to include in each GPU processing batch. :return: A list of tuples of found face locations in css (top, right, bottom, left) order csfdd|DS)Ncs"g|]}tt|jdjqS)rr*r,r4rr r/r0zObatch_face_locations..convert_cnn_detections_to_css..r)Z detectionsr6rr convert_cnn_detections_to_csssz;batch_face_locations..convert_cnn_detections_to_css)r5listmap)r4r'r3r7Zraw_detections_batchedrr6r batch_face_locationss  r:largecsD|durt}ndd|D}t|dkr0tfdd|DS)NcSsg|] }t|qSr)rr-Z face_locationrrr r/r0z'_raw_face_landmarks..smallcsg|]}|qSrrr< face_imageZpose_predictorrr r/r0)r)pose_predictor_68_pointpose_predictor_5_point)r?r1r(rr>r _raw_face_landmarkss rBcCsRt|||}dd|D}|dkr0dd|DS|dkrFdd|DStddS) a Given an image, returns a dict of face feature locations (eyes, nose, etc) for each face in the image :param face_image: image to search :param face_locations: Optionally provide a list of face locations to check. :param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster. :return: A list of dicts of face feature locations (eyes, nose, etc) cSsg|]}dd|DqS)cSsg|]}|j|jfqSr)xy)r-prrr r/r0z-face_landmarks...)parts)r-Zlandmarkrrr r/r0z"face_landmarks..r;c Ssg|]}|dd|dd|dd|dd|dd|dd|dd|dd|d g|d g|d g|d g|d g|dd |dg|d g|dg|dg|dg|d gd qS)r$*07@?>=<6CBA) ZchinZ left_eyebrowZ right_eyebrowZ nose_bridgenose_tipleft_eye right_eyeZtop_lipZ bottom_liprr-Zpointsrrr r/s        <Fr=cSs.g|]&}|dg|dd|dddqS)r r)rXrYrZrr[rrr r/s   zFInvalid landmarks model type. Supported models are ['small', 'large'].N)rB ValueError)r?r1r(Z landmarksZlandmarks_as_tuplesrrr face_landmarkss   r^r=cs t||}fdd|DS)aj Given an image, return the 128-dimension face encoding for each face in the image. :param face_image: The image that contains one or more faces :param known_face_locations: Optional - the bounding boxes of each face if you already know them. :param num_jitters: How many times to re-sample the face when calculating encoding. Higher is more accurate, but slower (i.e. 100 is 100x slower) :param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster. :return: A list of 128-dimensional face encodings (one for each face in the image) c s g|]}tt|qSr)rr face_encoderZcompute_face_descriptor)r-Zraw_landmark_setr? num_jittersrr r/r0z"face_encodings..)rB)r?Zknown_face_locationsrar(Z raw_landmarksrr`r rs r333333?cCstt|||kS)a Compare a list of face encodings against a candidate encoding to see if they match. :param known_face_encodings: A list of known face encodings :param face_encoding_to_check: A single face encoding to compare against the list :param tolerance: How much distance between faces to consider it a match. Lower is more strict. 0.6 is typical best performance. :return: A list of True/False values indicating which known_face_encodings match the face encoding to check )r8r)Zknown_face_encodingsZface_encoding_to_checkZ tolerancerrr compare_facess rc)r)r r!)r r!)r r2)r r2)Nr;)Nr;)Nr r=)rb))Z PIL.ImagerrnumpyrrZface_recognition_models ExceptionprintquitZLOAD_TRUNCATED_IMAGESZget_frontal_face_detectorr$Zpose_predictor_model_locationZpredictor_68_point_modelZshape_predictorr@Z(pose_predictor_five_point_model_locationZpredictor_5_point_modelrAZ cnn_face_detector_model_locationZcnn_face_detection_modelZcnn_face_detection_model_v1r#Zface_recognition_model_locationZface_recognition_modelZface_recognition_model_v1r_r rrrr r)r1r5r:rBr^rrcrrrr s@                 #