Python dlib.shape_predictor() Examples

The following are 30 code examples of dlib.shape_predictor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dlib , or try the search function .
Example #1
Source File: training_script.py    From dlib-minified-models with Apache License 2.0 7 votes vote down vote up
def test(image_path, model_path):
    '''Test the given model by showing the detected landmarks.
        - image_path: the path of an image. Should contain a face.
        - model_path: the path of a shape predictor model.
    '''
    image = cv2.imread(image_path)
    face_detector = dlib.get_frontal_face_detector()
    dets = face_detector(image, 1)
    predictor = dlib.shape_predictor(model_path)

    for d in dets:
      cv2.rectangle(image, (d.left(), d.top()), (d.right(), d.bottom()), 255, 1)
      shape = predictor(image, d)
      for i in range(shape.num_parts):
        p = shape.part(i)
        cv2.circle(image, (p.x, p.y), 2, 255, 1)
        cv2.putText(image, str(i), (p.x + 4, p.y), cv2.FONT_HERSHEY_SIMPLEX, 0.25, (255, 255, 255))

    cv2.imshow("window", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


# uncomment to test
# test("test_image.jpg", "shape_predictor.dat") 
Example #2
Source File: align_dlib.py    From Python-Tensorflow-Face-v2.0 with Apache License 2.0 6 votes vote down vote up
def __init__(self, facePredictor):
        """
        Instantiate an 'AlignDlib' object.
        :param facePredictor: The path to dlib's
        :type facePredictor: str
        """
        assert facePredictor is not None

        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(facePredictor) 
Example #3
Source File: face_detector.py    From portrait_matting with GNU General Public License v3.0 6 votes vote down vote up
def _setup_predictor(self, predictor_path):
        if not os.path.exists(predictor_path):
            # Download predictor file
            url = FaceDetector.predictor_url
            bz2_path = os.path.basename(url)

            logger.info('Download to "%s"', bz2_path)
            urllib.request.urlretrieve(url, bz2_path)

            logger.info('Expand to "%s"', predictor_path)
            data = bz2.BZ2File(bz2_path).read()
            open(predictor_path, 'wb').write(data)

            logger.info('Remove temporary file ("%s")', bz2_path)
            os.remove(bz2_path)

        # Deserialize
        predictor = dlib.shape_predictor(predictor_path)

        return predictor 
Example #4
Source File: predict.py    From facial-expression-recognition-using-cnn with GNU General Public License v3.0 6 votes vote down vote up
def predict(image, model, shape_predictor=None):
    # get landmarks
    if NETWORK.use_landmarks or NETWORK.use_hog_and_landmarks or NETWORK.use_hog_sliding_window_and_landmarks:
        face_rects = [dlib.rectangle(left=0, top=0, right=NETWORK.input_size, bottom=NETWORK.input_size)]
        face_landmarks = np.array([get_landmarks(image, face_rects, shape_predictor)])
        features = face_landmarks
        if NETWORK.use_hog_sliding_window_and_landmarks: 
            hog_features = sliding_hog_windows(image)
            hog_features = np.asarray(hog_features)
            face_landmarks = face_landmarks.flatten()
            features = np.concatenate((face_landmarks, hog_features))
        else:
            hog_features, _ = hog(image, orientations=8, pixels_per_cell=(16, 16),
                                    cells_per_block=(1, 1), visualise=True)
            hog_features = np.asarray(hog_features)
            face_landmarks = face_landmarks.flatten()
            features = np.concatenate((face_landmarks, hog_features))
        tensor_image = image.reshape([-1, NETWORK.input_size, NETWORK.input_size, 1])
        predicted_label = model.predict([tensor_image, features.reshape((1, -1))])
        return get_emotion(predicted_label[0])
    else:
        tensor_image = image.reshape([-1, NETWORK.input_size, NETWORK.input_size, 1])
        predicted_label = model.predict(tensor_image)
        return get_emotion(predicted_label[0])
    return None 
Example #5
Source File: dlib_featurizer.py    From ColumbiaImageSearch with Apache License 2.0 6 votes vote down vote up
def __init__(self, global_conf_in, prefix="DLIBFEAT_"):
    """DLibFeaturizer constructor.

    :param global_conf_in: configuration file or dictionary
    :type global_conf_in: str, dict
    :param prefix: prefix in configuration
    :type prefix: str
    :raise ValueError: if ``pred_path`` or ``rec_path`` is not set in configuration
    """
    super(DLibFeaturizer, self).__init__(global_conf_in, prefix)
    self.set_pp(pp="DLibFeaturizer")
    if self.verbose > 0:
      print("[{}.log] global_conf: {}".format(self.pp, self.global_conf))

    # Get shape predictor
    pred_path = self.get_required_param('pred_path')
    # Test if file exits there
    if not os.path.exists(pred_path):
      # Download file if not
      download_model(www_pred_path, pred_path, pred_bz2_file)
    # Intialize shape predictor
    self.sp = dlib.shape_predictor(str(pred_path))

    # Get recognizer model
    rec_path = self.get_required_param('rec_path')
    # Test if file exits there
    if not os.path.exists(rec_path):
      # Download file if not
      download_model(www_rec_path, rec_path, rec_bz2_file)
    # Initialize recognizer model
    self.facerec = dlib.face_recognition_model_v1(str(rec_path)) 
Example #6
Source File: detector_ssd.py    From deepface with MIT License 6 votes vote down vote up
def __init__(self, specific_model):
        super(FaceDetectorSSD, self).__init__()
        self.specific_model = specific_model
        graph_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            DeepFaceConfs.get()['detector'][self.specific_model]['frozen_graph']
        )
        self.detector = self._load_graph(graph_path)

        self.tensor_image = self.detector.get_tensor_by_name('prefix/image_tensor:0')
        self.tensor_boxes = self.detector.get_tensor_by_name('prefix/detection_boxes:0')
        self.tensor_score = self.detector.get_tensor_by_name('prefix/detection_scores:0')
        self.tensor_class = self.detector.get_tensor_by_name('prefix/detection_classes:0')

        predictor_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            DeepFaceConfs.get()['detector']['dlib']['landmark_detector']
        )
        self.predictor = dlib.shape_predictor(predictor_path)

        config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
        self.session = tf.Session(graph=self.detector, config=config) 
Example #7
Source File: facial_feature_detector.py    From EmotionClassifier with GNU General Public License v3.0 6 votes vote down vote up
def get_landmarks(img, resource_dir, verbose=False):
    # if not automatically downloaded, get it from:
    # http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2
    predictor_path = resource_dir + "/dlib_models/shape_predictor_68_face_landmarks.dat"
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(predictor_path)

    lmarks = []
    dets = detector(img, 1)
    if verbose:
        print("Number of faces detected: {}".format(len(dets)))
    shapes = []
    for k, det in enumerate(dets):
        shape = predictor(img, det)
        shapes.append(shape)
        xy = _shape_to_np(shape)
        lmarks.append(xy)

    lmarks = np.asarray(lmarks, dtype='float32')
    # display_landmarks(img, dets, shapes)
    return lmarks 
Example #8
Source File: preprocessing.py    From face-identification-tpe with MIT License 5 votes vote down vote up
def __init__(self,
                 dlib_predictor_path,
                 face_template_path):
        self.predictor = dlib.shape_predictor(dlib_predictor_path)
        self.face_template = np.load(face_template_path) 
Example #9
Source File: face_utilities.py    From Heart-rate-measurement-using-camera with Apache License 2.0 5 votes vote down vote up
def get_landmarks(self, frame, type):
        '''
        Get all facial landmarks in a face 
        
        Args:
            frame (cv2 image): the original frame. In RGB format.
            type (str): 5 or 68 facial landmarks
        
        Outputs:
            shape (array): facial landmarks' co-ords in format of of tuples (x,y)
        '''
        if self.predictor is None:
            print("[INFO] load " + type + " facial landmarks model ...")
            self.predictor = dlib.shape_predictor("../shape_predictor_" + type + "_face_landmarks.dat")
            print("[INFO] Load model - DONE!")
        
        if frame is None:
            return None, None
        # all face will be resized to a fix size, e.g width = 200
        #face = imutils.resize(face, width=200)
        # face must be gray
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        rects = self.face_detection(frame)
        
        if len(rects)<0 or len(rects)==0:
            return None, None
            
        shape = self.predictor(gray, rects[0])
        shape = face_utils.shape_to_np(shape)
        
        # in shape, there are 68 pairs of (x, y) carrying coords of 68 points.
        # to draw landmarks, use: for (x, y) in shape: cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
        
        return shape, rects 
Example #10
Source File: faceswapper.py    From FaceSwapper with Apache License 2.0 5 votes vote down vote up
def __init__(self,heads_list=[],predictor_path="./data/shape_predictor_68_face_landmarks.dat"):
        '''
        head_list:
            头(背景和发型)来源图片的路径的字符串列表,根据此列表在实例化时载入多个头像资源,
            并获得面部识别点坐标,以字典形式存储,键名为文件名
        predictor_path:
            dlib资源的路径
        '''
        #五官等标记点
        self.PREDICTOR_PATH = predictor_path
        self.FACE_POINTS = list(range(17, 68))
        self.MOUTH_POINTS = list(range(48, 61))
        self.RIGHT_BROW_POINTS = list(range(17, 22))
        self.LEFT_BROW_POINTS = list(range(22, 27))
        self.RIGHT_EYE_POINTS = list(range(36, 42))
        self.LEFT_EYE_POINTS = list(range(42, 48))
        self.NOSE_POINTS = list(range(27, 35))
        self.JAW_POINTS = list(range(0, 17))

        # 人脸的完整标记点
        self.ALIGN_POINTS = (self.LEFT_BROW_POINTS + self.RIGHT_EYE_POINTS + self.LEFT_EYE_POINTS +
                                       self.RIGHT_BROW_POINTS + self.NOSE_POINTS + self.MOUTH_POINTS)
        
        # 来自第二张图(脸)的标记点,眼、眉、鼻子、嘴,这一部分标记点将覆盖第一张图的对应标记点
        self.OVERLAY_POINTS = [self.LEFT_EYE_POINTS + self.RIGHT_EYE_POINTS + self.LEFT_BROW_POINTS + self.RIGHT_BROW_POINTS,
            self.NOSE_POINTS + self.MOUTH_POINTS]
        
        # 颜色校正参数
        self.COLOUR_CORRECT_BLUR_FRAC = 0.6
        
        #人脸定位、特征提取器,来自dlib
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(self.PREDICTOR_PATH)
        
        #头像资源
        self.heads={}
        if heads_list:
            self.load_heads(heads_list) 
Example #11
Source File: dlib.py    From thug-memes with MIT License 5 votes vote down vote up
def find_thug_landmarks(self, img_path, show_result=False):
        face_det = dlib.get_frontal_face_detector()

        predictor = dlib.shape_predictor(self._landmarks_model)

        img = cv2.imread(img_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        faces = face_det(gray)

        thug_results = []

        for f in faces:
            predicted = predictor(gray, f)
            landmarks = predicted.parts()

            left_eye = self._center_of_two_points(
                landmarks[LEFT_EYE_CENTER_IDXS[0]],
                landmarks[LEFT_EYE_CENTER_IDXS[1]])

            right_eye = self._center_of_two_points(
                landmarks[RIGHT_EYE_CENTER_IDXS[0]],
                landmarks[RIGHT_EYE_CENTER_IDXS[1]])

            mouth = self._center_of_two_points(landmarks[MOUTH_CENTER_IDXS[0]],
                                               landmarks[MOUTH_CENTER_IDXS[1]])

            thug = ThugLandmarks(l_eye=left_eye, r_eye=right_eye, mouth=mouth)
            thug_results.append(thug)

            if show_result:
                self._draw_result(img, f, landmarks, thug)

        if show_result:
            cv2.imshow('detection_result-{}'.format(timestamp()), img)
            cv2.waitKey(1)

        return thug_results 
Example #12
Source File: align_dlib.py    From insightface with MIT License 5 votes vote down vote up
def __init__(self, facePredictor):
        """
        Instantiate an 'AlignDlib' object.

        :param facePredictor: The path to dlib's
        :type facePredictor: str
        """
        assert facePredictor is not None

        #pylint: disable=no-member
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(facePredictor) 
Example #13
Source File: setup.py    From multimodal-vae-public with MIT License 5 votes vote down vote up
def build_mask_dataset(in_dir, out_dir, model_path):
    """Generate a dataset of segmentation masks from images.

    @param in_dir: string
                   input directory of images.
    @param out_dir: string
                    output directory of images.
    @param model_path: string
                       path to HOG model for facial features.
    """
    # initialize dlib's face detector (HOG-based) and then create
    # the facial landmark predictor
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(model_path)

    image_paths = os.listdir(in_dir)
    n_images = len(image_paths)
    for i, image_path in enumerate(image_paths):
        print('Building face-mask dataset: [%d/%d] images.' % (i + 1, n_images))
        image_full_path = os.path.join(in_dir, image_path)

        image = cv2.imread(image_full_path)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # detect faces in the grayscale image
        rects = detector(gray, 1)
        try:
            rect = rects[0]  # we are only going to use the first one

            # determine the facial landmarks for the face region, then
            # convert the landmark (x, y)-coordinates to a NumPy array
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
            output = visualize_facial_landmarks(image, shape)
            cv2.imwrite(os.path.join(out_dir, image_path), output)
        except:
            # if for some reason no bounding box is found, send blank.
            output = np.ones_like(image) * 255
            cv2.imwrite(os.path.join(out_dir, image_path), output) 
Example #14
Source File: face_detection.py    From Heart-rate-measurement-using-camera with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
        self.fa = face_utils.FaceAligner(self.predictor, desiredFaceWidth=256) 
Example #15
Source File: FeatureExtractor.py    From adviser with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, domain: Domain = ""):
        Service.__init__(self, domain=domain)
        self.module_dir = os.path.dirname(os.path.abspath(__file__))
        # # CLAHE (Contrast Limited Adaptive Histogram Equalization)
        self.CLAHE = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
        # for detecting faces (returns coordinates of rectangle(s) of face area(s))
        self.DETECTOR = dlib.get_frontal_face_detector()
        # facial landmark predictor
        predictor_file = os.path.abspath(os.path.join(self.module_dir, '..', '..', '..', 'resources', 'models', 'video', 'shape_predictor_68_face_landmarks.dat'))
        self.PREDICTOR = dlib.shape_predictor(predictor_file) 
Example #16
Source File: frames.py    From GazeML with MIT License 5 votes vote down vote up
def get_landmarks_predictor():
    """Get a singleton dlib face landmark predictor."""
    global _landmarks_predictor
    if not _landmarks_predictor:
        dat_path = _get_dlib_data_file('shape_predictor_5_face_landmarks.dat')
        # dat_path = _get_dlib_data_file('shape_predictor_68_face_landmarks.dat')
        _landmarks_predictor = dlib.shape_predictor(dat_path)
    return _landmarks_predictor 
Example #17
Source File: FacePoints.py    From PyQt with GNU General Public License v3.0 5 votes vote down vote up
def startCapture(self):
        self.setText("请稍候,正在初始化数据和摄像头。。。")
        try:
            # 检测相关
            self.detector = dlib.get_frontal_face_detector()
            self.predictor = dlib.shape_predictor(
                "Data/shape_predictor_68_face_landmarks.dat")
            cascade_fn = "Data/lbpcascades/lbpcascade_frontalface.xml"
            self.cascade = cv2.CascadeClassifier(cascade_fn)
            if not self.cascade:
                return QMessageBox.critical(self, "错误", cascade_fn + " 无法找到")
            self.cap = cv2.VideoCapture(0)
            if not self.cap or not self.cap.isOpened():
                return QMessageBox.critical(self, "错误", "打开摄像头失败")
            # 开启定时器定时捕获
            self.timer = QTimer(self, timeout=self.onCapture)
            self.timer.start(1000 / self.fps)
        except Exception as e:
            QMessageBox.critical(self, "错误", str(e)) 
Example #18
Source File: face_landmark_detection.py    From deepgaze with MIT License 5 votes vote down vote up
def __init__(self, landmarkPath):
        #Check if the file provided exist
        if(os.path.isfile(landmarkPath)==False):
            raise ValueError('haarCascade: the files specified do not exist.')

        self._predictor = dlib.shape_predictor(landmarkPath)


    ##
    # Find landmarks in the image provided
    # @param inputImg the image where the algorithm will be called
    # 
Example #19
Source File: align_dlib.py    From Face_Recognition_Client with Apache License 2.0 5 votes vote down vote up
def __init__(self, facePredictor):
        """
        Instantiate an 'AlignDlib' object.
        :param facePredictor: The path to dlib's
        :type facePredictor: str
        """
        assert facePredictor is not None

        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(facePredictor) 
Example #20
Source File: alignface.py    From deepfeatinterp with GNU General Public License v3.0 5 votes vote down vote up
def compute_template(globspec='images/lfw_aegan/*/*.png',image_dims=[400,400],predictor_path='models/shape_predictor_68_face_landmarks.dat',center_crop=None,subsample=1):
  # Credit: http://dlib.net/face_landmark_detection.py.html
  detector=dlib.get_frontal_face_detector()
  predictor=dlib.shape_predictor(predictor_path)

  template=numpy.zeros((68,2),dtype=numpy.float64)
  count=0

  if not center_crop is None:
    center_crop=numpy.asarray(center_crop)
    cy,cx=(numpy.asarray(image_dims)-center_crop)//2

  # compute mean landmark locations
  S=sorted(glob.glob(globspec))
  S=S[::subsample]
  for ipath in S:
    print("Processing file: {}".format(ipath))
    img=(skimage.transform.resize(skimage.io.imread(ipath)/255.0,tuple(image_dims)+(3,),order=2,mode='nearest')*255).clip(0,255).astype(numpy.ubyte)
    if not center_crop is None:
      img=img[cy:cy+center_crop[0],cx:cx+center_crop[0]]

    upsample=0
    dets=detector(img,upsample)
    if len(dets)!=1: continue

    for k,d in enumerate(dets):
      shape=predictor(img, d)
      for i in range(68):
        template[i]+=(shape.part(i).y,shape.part(i).x)
      count+=1
  template/=float(count)
  return template
  # lfw_aegan 400x400 template map
  # [[ 251.58852868  201.50275826]  # 33 where nose meets upper-lip
  #  [ 172.69409809  168.66523086]  # 39 inner-corner of left eye
  #  [ 171.72236076  232.09718129]] # 42 inner-corner or right eye 
Example #21
Source File: detector_dlib.py    From deepface with MIT License 5 votes vote down vote up
def __init__(self):
        super(FaceDetectorDlib, self).__init__()
        self.detector = dlib.get_frontal_face_detector()
        predictor_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            DeepFaceConfs.get()['detector']['dlib']['landmark_detector']
        )
        self.predictor = dlib.shape_predictor(predictor_path)
        self.upsample_scale = DeepFaceConfs.get()['detector']['dlib']['scale'] 
Example #22
Source File: align_dlib.py    From facenet with MIT License 5 votes vote down vote up
def __init__(self, facePredictor):
        """
        Instantiate an 'AlignDlib' object.

        :param facePredictor: The path to dlib's
        :type facePredictor: str
        """
        assert facePredictor is not None

        #pylint: disable=no-member
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(facePredictor) 
Example #23
Source File: landmarks.py    From photo-a-day-aligner with MIT License 5 votes vote down vote up
def __init__(self, predictor_path):
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(str(predictor_path)) 
Example #24
Source File: align_dlib.py    From MaskInsightface with Apache License 2.0 5 votes vote down vote up
def __init__(self, facePredictor):
        """
        Instantiate an 'AlignDlib' object.

        :param facePredictor: The path to dlib's
        :type facePredictor: str
        """
        assert facePredictor is not None

        #pylint: disable=no-member
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(facePredictor) 
Example #25
Source File: AIMakeup.py    From AIMakeup with Apache License 2.0 5 votes vote down vote up
def __init__(self,predictor_path="./data/shape_predictor_68_face_landmarks.dat"):
        self.photo_path=[]
        self.PREDICTOR_PATH = predictor_path
        self.faces={}
        
        #人脸定位、特征提取器,来自dlib
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor(self.PREDICTOR_PATH) 
Example #26
Source File: eval.py    From Age-Gender-Estimate-TF with MIT License 5 votes vote down vote up
def load_image(image_path, shape_predictor):
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(shape_predictor)
    fa = FaceAligner(predictor, desiredFaceWidth=160)
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    # image = imutils.resize(image, width=256)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 2)
    rect_nums = len(rects)
    XY, aligned_images = [], []
    if rect_nums == 0:
        aligned_images.append(image)
        return aligned_images, image, rect_nums, XY
    else:
        for i in range(rect_nums):
            aligned_image = fa.align(image, gray, rects[i])
            aligned_images.append(aligned_image)
            (x, y, w, h) = rect_to_bb(rects[i])
            image = cv2.rectangle(image, (x, y), (x + w, y + h), color=(255, 0, 0), thickness=2)
            XY.append((x, y))
        return np.array(aligned_images), image, rect_nums, XY


# def draw_label(image, point, ages, genders, font=cv2.FONT_HERSHEY_COMPLEX, font_scale=1, thickness=1):
#     for i in range(len(point)):
#         label = "{}, {}".format(int(ages[i]), "F" if genders[i] == 0 else "M")
#         size = cv2.getTextSize(label, font, font_scale, thickness)[0]
#         x, y = point[i]
#         # cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
#         cv2.putText(image, label, (x, np.max(y - 5, 0)), font, font_scale, (255, 255, 255), thickness) 
Example #27
Source File: eyes.py    From voice-enabled-chatbot with MIT License 5 votes vote down vote up
def add_landmarks(mat, face, frame):
   predictor = dlib.shape_predictor()
   shape = predictor(mat, face)
   shape = shape_to_np(shape)
   for (x, y) in shape:
      cv2.circle(mat, (x, y), 1, (0, 0, 255), -1) 
Example #28
Source File: alignface.py    From deepfeatinterp with GNU General Public License v3.0 5 votes vote down vote up
def load_face_detector(predictor_path='models/shape_predictor_68_face_landmarks.dat'):
  detector=dlib.get_frontal_face_detector()
  predictor=dlib.shape_predictor(predictor_path)
  return detector,predictor 
Example #29
Source File: alignface.py    From deepfeatinterp with GNU General Public License v3.0 5 votes vote down vote up
def compute_template(globspec='images/lfw_aegan/*/*.png',image_dims=[400,400],predictor_path='models/shape_predictor_68_face_landmarks.dat',center_crop=None,subsample=1):
  # Credit: http://dlib.net/face_landmark_detection.py.html
  detector=dlib.get_frontal_face_detector()
  predictor=dlib.shape_predictor(predictor_path)

  template=numpy.zeros((68,2),dtype=numpy.float64)
  count=0

  if not center_crop is None:
    center_crop=numpy.asarray(center_crop)
    cy,cx=(numpy.asarray(image_dims)-center_crop)//2

  # compute mean landmark locations
  S=sorted(glob.glob(globspec))
  S=S[::subsample]
  for ipath in S:
    print("Processing file: {}".format(ipath))
    img=(skimage.transform.resize(skimage.io.imread(ipath)/255.0,tuple(image_dims)+(3,),order=2,mode='nearest')*255).clip(0,255).astype(numpy.ubyte)
    if not center_crop is None:
      img=img[cy:cy+center_crop[0],cx:cx+center_crop[0]]

    upsample=0
    dets=detector(img,upsample)
    if len(dets)!=1: continue

    for k,d in enumerate(dets):
      shape=predictor(img, d)
      for i in range(68):
        template[i]+=(shape.part(i).y,shape.part(i).x)
      count+=1
  template/=float(count)
  return template
  # lfw_aegan 400x400 template map
  # [[ 251.58852868  201.50275826]  # 33 where nose meets upper-lip
  #  [ 172.69409809  168.66523086]  # 39 inner-corner of left eye
  #  [ 171.72236076  232.09718129]] # 42 inner-corner or right eye 
Example #30
Source File: alignface.py    From deepfeatinterp with GNU General Public License v3.0 5 votes vote down vote up
def load_face_detector(predictor_path='models/shape_predictor_68_face_landmarks.dat'):
  detector=dlib.get_frontal_face_detector()
  predictor=dlib.shape_predictor(predictor_path)
  return detector,predictor