Python dlib.cnn_face_detection_model_v1() Examples

The following are 12 code examples of dlib.cnn_face_detection_model_v1(). 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: dlib_detector.py    From VTuber_Unity with MIT License 5 votes vote down vote up
def __init__(self, device, path_to_detector=None, verbose=False):
        super().__init__(device, verbose)

        base_path = os.path.join(appdata_dir('face_alignment'), "data")

        # Initialise the face detector
        if 'cuda' in device:
            if path_to_detector is None:
                path_to_detector = os.path.join(
                    base_path, "mmod_human_face_detector.dat")

                if not os.path.isfile(path_to_detector):
                    print("Downloading the face detection CNN. Please wait...")

                    path_to_temp_detector = os.path.join(
                        base_path, "mmod_human_face_detector.dat.download")

                    if os.path.isfile(path_to_temp_detector):
                        os.remove(os.path.join(path_to_temp_detector))

                    request_file.urlretrieve(
                        "https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
                        os.path.join(path_to_temp_detector))

                    os.rename(os.path.join(path_to_temp_detector), os.path.join(path_to_detector))

            self.face_detector = dlib.cnn_face_detection_model_v1(path_to_detector)
        else:
            self.face_detector = dlib.get_frontal_face_detector() 
Example #2
Source File: prnet.py    From LipReading with MIT License 5 votes vote down vote up
def __init__(self, is_dlib=False):

    # resolution of input and output image size.
    self.resolution_inp = 256
    self.resolution_op = 256

    # ---- load detectors
    if is_dlib:
      import dlib
      detector_path = _util.getRelWeightsPath('dlib', 'mmod_human_face_detector.dat')
      self.face_detector = dlib.cnn_face_detection_model_v1(detector_path)

    # ---- load PRN
    self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
    prn_path = _util.getRelWeightsPath('prnet', 'net/256_256_resfcn256_weight')
    assert os.path.isfile(prn_path + '.data-00000-of-00001'), "please download PRN trained model first."
    self.pos_predictor.restore(prn_path)

    # uv file: 2 x 68
    self.uv_kpt_ind = np.loadtxt(_util.getRelWeightsPath('prnet', 'uv', 'uv_kpt_ind.txt')).astype(np.int32)
    #  get kpt: get valid vertices in the pos map
    self.face_ind = np.loadtxt(_util.getRelWeightsPath('prnet', 'uv', 'face_ind.txt')).astype(np.int32)
    # ntri x 3.
    self.triangles = np.loadtxt(_util.getRelWeightsPath('prnet', 'uv', 'triangles.txt')).astype(np.int32)

    self.uv_coords = self.generate_uv_coords()

    # Cache Position map.
    self.pos = None 
Example #3
Source File: api.py    From LipReading with MIT License 5 votes vote down vote up
def __init__(self, is_dlib = False, prefix = '.'):

        # resolution of input and output image size.
        self.resolution_inp = 256
        self.resolution_op = 256

        #---- load detectors
        if is_dlib:
            import dlib
            detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
            self.face_detector = dlib.cnn_face_detection_model_v1(
                    detector_path)

        #---- load PRN 
        self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
        prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
        if not os.path.isfile(prn_path + '.data-00000-of-00001'):
            print(prn_path)
            print("please download PRN trained model first.")
            exit()
        self.pos_predictor.restore(prn_path)

        # uv file
        self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
        self.face_ind = np.loadtxt(prefix + '/Data/uv/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
        self.triangles = np.loadtxt(prefix + '/Data/uv/triangles.txt').astype(np.int32) # ntri x 3
        
        self.uv_coords = self.generate_uv_coords() 
Example #4
Source File: cvision.py    From Efficient-Facial-Feature-Learning-with-Wide-Ensemble-based-Convolutional-Neural-Networks with MIT License 5 votes vote down vote up
def _dlib_face_detection(image):
    """
    Face detection using the CNN implementation from Dlib.

    References:
    Davis E. King. Dlib-ml: A Machine Learning Toolkit. Journal of Machine Learning Research 10, pp. 1755-1758, 2009

    :param image: (ndarray) Raw image
    :return: (ndarray) The coordinates of the detected face
    """
    global _FACE_DETECTOR_DLIB

    face_coordinates = []

    # Verifies if dlib is initialized
    if _FACE_DETECTOR_DLIB is None:
        _FACE_DETECTOR_DLIB = dlib.cnn_face_detection_model_v1('./model/utils/templates/dlib/cnn_face_detector.dat')

    # Calls dlib's face detection method
    faces = _FACE_DETECTOR_DLIB(image)

    # Gets coordinates
    if not (faces is None):
        for face_id, net_output in enumerate(faces):
            xi, xf, yi, yf = (net_output.rect.left(), net_output.rect.right(), net_output.rect.top(), net_output.rect.bottom())
            face_coordinates.append([[xi, yi], [xf, yf]])

    return np.array(face_coordinates) 
Example #5
Source File: roi.py    From pyVSR with GNU General Public License v3.0 5 votes vote down vote up
def _preload_dlib_detector_fitter(self):
        r"""
        Returns the dlib face detector and the landmark fitters (5 and 68 landmarks)
        -------

        """

        if self._gpu is True:
            self._detect = dlib.cnn_face_detection_model_v1(self._detector_path)
        else:
            self._detect = dlib.get_frontal_face_detector()

        self._fitter5 = dlib.shape_predictor(self._predictor5_path)
        self._fitter68 = dlib.shape_predictor(self._predictor68_path) 
Example #6
Source File: dlib_detector.py    From face-alignment with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, device, path_to_detector=None, verbose=False):
        super().__init__(device, verbose)

        print('Warning: this detector is deprecated. Please use a different one, i.e.: S3FD.')
        base_path = os.path.join(appdata_dir('face_alignment'), "data")

        # Initialise the face detector
        if 'cuda' in device:
            if path_to_detector is None:
                path_to_detector = os.path.join(
                    base_path, "mmod_human_face_detector.dat")

                if not os.path.isfile(path_to_detector):
                    print("Downloading the face detection CNN. Please wait...")

                    path_to_temp_detector = os.path.join(
                        base_path, "mmod_human_face_detector.dat.download")

                    if os.path.isfile(path_to_temp_detector):
                        os.remove(os.path.join(path_to_temp_detector))

                    request_file.urlretrieve(
                        "https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
                        os.path.join(path_to_temp_detector))

                    os.rename(os.path.join(path_to_temp_detector), os.path.join(path_to_detector))

            self.face_detector = dlib.cnn_face_detection_model_v1(path_to_detector)
        else:
            self.face_detector = dlib.get_frontal_face_detector() 
Example #7
Source File: dlib_detector.py    From face-alignment-pytorch with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, device, path_to_detector=None, verbose=False):
        super().__init__(device, verbose)

        base_path = os.path.join(appdata_dir('face_alignment'), "data")

        # Initialise the face detector
        if 'cuda' in device:
            if path_to_detector is None:
                path_to_detector = os.path.join(
                    base_path, "mmod_human_face_detector.dat")

                if not os.path.isfile(path_to_detector):
                    print("Downloading the face detection CNN. Please wait...")

                    path_to_temp_detector = os.path.join(
                        base_path, "mmod_human_face_detector.dat.download")

                    if os.path.isfile(path_to_temp_detector):
                        os.remove(os.path.join(path_to_temp_detector))

                    request_file.urlretrieve(
                        "https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
                        os.path.join(path_to_temp_detector))

                    os.rename(os.path.join(path_to_temp_detector), os.path.join(path_to_detector))

            self.face_detector = dlib.cnn_face_detection_model_v1(path_to_detector)
        else:
            self.face_detector = dlib.get_frontal_face_detector() 
Example #8
Source File: api.py    From PRNet with MIT License 5 votes vote down vote up
def __init__(self, is_dlib = False, prefix = '.'):

        # resolution of input and output image size.
        self.resolution_inp = 256
        self.resolution_op = 256

        #---- load detectors
        if is_dlib:
            import dlib
            detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
            self.face_detector = dlib.cnn_face_detection_model_v1(
                    detector_path)

        #---- load PRN 
        self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
        prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
        if not os.path.isfile(prn_path + '.data-00000-of-00001'):
            print("please download PRN trained model first.")
            exit()
        self.pos_predictor.restore(prn_path)

        # uv file
        self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
        self.face_ind = np.loadtxt(prefix + '/Data/uv-data/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
        self.triangles = np.loadtxt(prefix + '/Data/uv-data/triangles.txt').astype(np.int32) # ntri x 3
        
        self.uv_coords = self.generate_uv_coords() 
Example #9
Source File: api.py    From MaskInsightface with Apache License 2.0 5 votes vote down vote up
def __init__(self, is_dlib = False):

        # resolution of input and output image size.
        self.resolution_inp = 256
        self.resolution_op = 256

        prefix = os.path.dirname(__file__)
        #---- load detectors
        if is_dlib:
            import dlib
            detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
            self.face_detector = dlib.cnn_face_detection_model_v1(
                    detector_path)

        #---- load PRN
        self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
        prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
        if not os.path.isfile(prn_path + '.data-00000-of-00001'):
            print("please download PRN trained model first.")
            exit()
        self.pos_predictor.restore(prn_path)

        # uv file
        self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
        self.face_ind = np.loadtxt(prefix + '/Data/uv-data/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
        self.triangles = np.loadtxt(prefix + '/Data/uv-data/triangles.txt').astype(np.int32) # ntri x 3
        self.canonical_vertices_fan = (np.load(prefix + '/Data/uv-data/canonical_vertices_68_fan.npy'))
        self.canonical_vertices_40 = np.load(prefix + '/Data/uv-data/canonical_vertices_40k.npy') # pos angle with 40k point, add by sai

        self.uv_coords = self.generate_uv_coords() 
Example #10
Source File: api.py    From PRNet-Depth-Generation with MIT License 5 votes vote down vote up
def __init__(self, is_dlib = False, is_opencv = False, prefix = '.'):

        # resolution of input and output image size.
        self.resolution_inp = 256
        self.resolution_op = 256

        #---- load detectors
        if is_dlib:
            import dlib
            detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
            self.face_detector = dlib.cnn_face_detection_model_v1(
                    detector_path)

        if is_opencv:
            import cv2

        #---- load PRN 
        self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
        prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
        if not os.path.isfile(prn_path + '.data-00000-of-00001'):
            print("please download PRN trained model first.")
            exit()
        self.pos_predictor.restore(prn_path)

        # uv file
        self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
        self.face_ind = np.loadtxt(prefix + '/Data/uv-data/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
        self.triangles = np.loadtxt(prefix + '/Data/uv-data/triangles.txt').astype(np.int32) # ntri x 3 
Example #11
Source File: frames.py    From GazeML with MIT License 5 votes vote down vote up
def get_face_detector():
    """Get a singleton dlib face detector."""
    global _face_detector
    if not _face_detector:
        try:
            dat_path = _get_dlib_data_file('mmod_human_face_detector.dat')
            _face_detector = dlib.cnn_face_detection_model_v1(dat_path)
        except:
            xml_path = _get_opencv_xml('lbpcascade_frontalface_improved.xml')
            _face_detector = cv.CascadeClassifier(xml_path)
    return _face_detector 
Example #12
Source File: tools.py    From FALdetector with Apache License 2.0 4 votes vote down vote up
def face_detection(
        img_path,
        verbose=False,
        model_file='utils/dlib_face_detector/mmod_human_face_detector.dat'):
    """
    Detects faces using dlib cnn face detection, and extend the bounding box
    to include the entire face.
    """
    def shrink(img, max_length=2048):
        ow, oh = img.size
        if max_length >= max(ow, oh):
            return img, 1.0

        if ow > oh:
            mult = max_length / ow
        else:
            mult = max_length / oh
        w = int(ow * mult)
        h = int(oh * mult)
        return img.resize((w, h), Image.BILINEAR), mult

    global cnn_face_detector
    if cnn_face_detector is None:
        cnn_face_detector = face_detect_model(model_file)

    img = Image.open(img_path).convert('RGB')
    w, h = img.size
    img_shrinked, mult = shrink(img)

    im = np.asarray(img_shrinked)
    if len(im.shape) != 3 or im.shape[2] != 3:
        return []

    crop_ims = []
    dets = cnn_face_detector(im, 0)
    for k, d in enumerate(dets):
        top = d.rect.top() / mult
        bottom = d.rect.bottom() / mult
        left = d.rect.left() / mult
        right = d.rect.right() / mult

        wid = right - left
        left = max(0, left - wid // 2.5)
        top = max(0, top - wid // 1.5)
        right = min(w - 1, right + wid // 2.5)
        bottom = min(h - 1, bottom + wid // 2.5)

        if d.confidence > 1:
            if verbose:
                print("%d-th face detected: (%d, %d, %d, %d)" %
                      (k, left, top, right, bottom))
            crop_im = img.crop((left, top, right, bottom))
            crop_ims.append((crop_im, (left, top, right, bottom)))

    return crop_ims