Пример #1
0
    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()        
def initialize(detector, scale_to=2048):
    global dlib_detectors
    global keras_model
    global is_initialized
    if not is_initialized:
        dlib_cnn_face_detector_path = os.path.join(os.path.dirname(__file__), "mmod_human_face_detector.dat")
        if not os.path.exists(dlib_cnn_face_detector_path):
            raise Exception ("Error: Unable to find %s, reinstall the lib !" % (dlib_cnn_face_detector_path) )
        
        if detector == 'cnn' or detector == "all":
            dlib_cnn_face_detector = dlib.cnn_face_detection_model_v1(dlib_cnn_face_detector_path)            
            #DLIB and TF competiting for VRAM, so dlib must do first allocation to prevent OOM error 
            dlib_cnn_face_detector ( np.zeros ( (scale_to, scale_to, 3), dtype=np.uint8), 0 ) 
            dlib_detectors.append(dlib_cnn_face_detector)
        
        if detector == "hog" or detector == "all":
            dlib_face_detector = dlib.get_frontal_face_detector()
            dlib_face_detector ( np.zeros ( (scale_to, scale_to, 3), dtype=np.uint8), 0 )
            dlib_detectors.append(dlib_face_detector)        
    
        keras_model_path = os.path.join( os.path.dirname(__file__) , "2DFAN-4.h5" )
        if not os.path.exists(keras_model_path):
            print ("Error: Unable to find %s, reinstall the lib !" % (keras_model_path) )
        else:
            print ("Info: initializing keras model...")
            keras_model = keras.models.load_model (keras_model_path, custom_objects={'TorchBatchNorm2D': TorchBatchNorm2D} ) 
            
        is_initialized = True
Пример #3
0
    def create_detector(self, verbose, detector):
        """ Add the requested detectors """
        if self.initialized:
            return

        self.verbose = verbose

        if detector == "dlib-cnn" or detector == "dlib-all":
            if self.verbose:
                print("Adding DLib - CNN detector")
            self.detectors.append(dlib.cnn_face_detection_model_v1(
                self.data_path))

        if detector == "dlib-hog" or detector == "dlib-all":
            if self.verbose:
                print("Adding DLib - HOG detector")
            self.detectors.append(dlib.get_frontal_face_detector())

        self.initialized = True
Пример #4
0
def demo(isAlignment_5=True):
    image_path = "./data/"
    image_file = "test_1988.jpg"
    im_raw = cv2.imread(image_path + image_file).astype('uint8')

    # detector = dlib.get_frontal_face_detector()
    model_path = "./models/mmod_human_face_detector.dat"  # 基于 Maximum-Margin Object Detector 的深度学习人脸检测方案
    detector = dlib.cnn_face_detection_model_v1(model_path)
    im_raw = resize(im_raw, width=1200)
    gray = cv2.cvtColor(im_raw, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 1)

    src_faces = []
    for (i, rect) in enumerate(rects):
        (x, y, w, h) = rect_to_bb(rect.rect)
        detect_face = im_raw[y:y+h,x:x+w]
        src_faces.append(detect_face)
        cv2.rectangle(im_raw, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(im_raw, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    if isAlignment_5:
        faces_aligned = face_alignment_5(im_raw, src_faces)
    else:
        faces_aligned = face_alignment_68(src_faces)
    print("{} method, detect spend {}s ".format(("Alignment_5" if isAlignment_5 else "Alignment_68"), time.time()-startTime))

    cv2.imshow("src", im_raw)
    savePath = "./results/alignment/"
    if not os.path.exists(savePath):
        os.makedirs(savePath)
    if isAlignment_5:
        saveName = "_Align5.jpg"
    else:
        saveName = "_Align68.jpg"
    i = 0
    for face in faces_aligned:
        cv2.imshow("det_{}".format(i), face)
        cv2.imwrite(savePath + image_file[:-4] + "_{}".format(i) + saveName, face)
        i = i + 1
    cv2.waitKey(10)
Пример #5
0
def test_cnn_face_detector():
    """
    You can get the mmod_human_face_detector.dat file from:\n
    http://dlib.net/files/mmod_human_face_detector.dat.bz2
    :return:
    """
    img_path = 'data/running_man.jpg'
    cnn_face_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')

    img = io.imread(img_path)
    # upsample image 1 time, make everything bigger and allow us to detect more faces.
    dets = cnn_face_detector(img, 2)

    img_cv2 = cv2.imread(img_path)
    for i, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(
            i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))
        cv2.rectangle(img_cv2, (d.rect.left(), d.rect.top()),
                      (d.rect.right(), d.rect.bottom()), color=(255, 0, 0), thickness=3)
    cv2.imshow('img', img_cv2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Пример #6
0
    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 = pkg_resources.resource_filename(
                __name__, 'assets/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 = pkg_resources.resource_filename(
            __name__, 'assets/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(
            pkg_resources.resource_filename(
                __name__, 'assets/uv-data/uv_kpt_ind.txt')).astype(
                    np.int32)  # 2 x 68 get kpt
        self.face_ind = np.loadtxt(
            pkg_resources.resource_filename(
                __name__, 'assets/uv-data/face_ind.txt')).astype(
                    np.int32)  # get valid vertices in the pos map
        self.triangles = np.loadtxt(
            pkg_resources.resource_filename(
                __name__,
                'assets/uv-data/triangles.txt')).astype(np.int32)  # ntri x 3

        self.uv_coords = self.generate_uv_coords()
Пример #7
0
def face_detector(img, detector='hog'):
    '''
  Detects faces in images from data

  Args:
  -----------------------------------------
  img: numpy image array
  detector: type of detector (Hog or Cnn) to detect faces

  Returns:
  ------------------------------------------
  Returns co-ordinates of rectangle bounding face in order (y1,y2,x1,x2)

  '''

    if detector.lower() == 'hog':

        hogFaceDetector = dlib.get_frontal_face_detector()
        faceRects = hogFaceDetector(img, 1)
        faceRect = faceRects[0]
        if faceRect.top() > 0 and faceRect.bottom() > 0 and faceRect.left(
        ) > 0 and faceRect.right() > 0:
            return faceRect.top(), faceRect.bottom(), faceRect.left(
            ), faceRect.right()
        else:
            return None

    elif detector.lower() == 'cnn':

        dnnFaceDetector = dlib.cnn_face_detection_model_v1(
            './database/dlib-models/mmod_human_face_detector.dat')
        rects = dnnFaceDetector(img, 1)
        faceRect = rects[0]
        if faceRect.rect.top() > 0 and faceRect.rect.bottom(
        ) > 0 and faceRect.rect.left() > 0 and faceRect.rect.right() > 0:
            return faceRect.rect.top(), faceRect.rect.bottom(
            ), faceRect.rect.left(), faceRect.rect.right()
        else:
            return None
def detect_dog_face(file_path, image):
    # 이미지 파일의 Grayscale화
    image_gs = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    detector = dlib.cnn_face_detection_model_v1('dogHeadDetector.dat')
    predictor = dlib.shape_predictor('landmarkDetector.dat')

    # 강아지 얼굴 인식
    faces = detector(image_gs, upsample_num_times=1)
    if len(faces) == 0:
        print("Fail to detecting face")
        return
    print("Success Face Detection!")
    # TO-DO
    # [76 31 83 83] -> x_pos, y_pos, width, height
    face_count = 1
    for (i, d) in enumerate(faces):
        x1, y1 = d.rect.left(), d.rect.top()
        x2, y2 = d.rect.right(), d.rect.bottom()
        face_image = image[y1:y2, x1:x2]  # y, x
        if face_image.shape[0] > 64:
            face_image = cv2.resize(face_image, (64, 64))
        print(face_image.shape)
        # Save 00_001.jpg -> 00_001_(face_count).jpg

        path = pathlib.Path(file_path)
        directory = str(path.parent.resolve())
        filename = path.stem
        extension = path.suffix
        output_path = os.path.join(directory,
                                   f"{filename}_{face_count:03}-{extension}")
        print(
            f"=================================OUTPUT File(절대 경로) : {output_path}"
        )
        try:
            cv2.imwrite(output_path, face_image)
            face_count = face_count + 1
        except:
            print("Exception occured : {}".format(output_path))
    return
Пример #9
0
    def __init__(self,
                 device,
                 path_to_detector=None,
                 verbose=False,
                 models_dir=None):
        super().__init__(device, verbose, models_dir)

        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()
Пример #10
0
def main():
    args = get_argparser().parse_args()

    # load dataset and extract faces
    detector = dlib.cnn_face_detection_model_v1(args.detector_model)
    train_X, train_y = load_dataset(detector, Path(args.train_dir))
    print(train_X.shape, train_y.shape)

    val_X, val_y = load_dataset(detector, Path(args.val_dir))
    print(val_X.shape, val_y.shape)

    # uncomment this section if intermediate faces output are needed
    # np.savez_compressed('5-celebrity-faces-dataset.npz', train_X, train_y, val_X, val_y)
    # print("Save face embeddings to 5-celebrity-faces-dataset.npz")

    model = dlib.face_recognition_model_v1(args.recog_model)
    print(f"Loaded model from {args.recog_model}")

    # convert faces to embeddings
    embed_train_X = list()
    for face_pixels in train_X:
        embedding = get_embedding(model, face_pixels)
        embed_train_X.append(embedding)
    embed_train_X = np.asarray(embed_train_X)
    print(embed_train_X.shape)

    embed_val_X = list()
    for face_pixels in val_X:
        embedding = get_embedding(model, face_pixels)
        embed_val_X.append(embedding)
    embed_val_X = np.asarray(embed_val_X)
    print(embed_val_X.shape)

    np.savez_compressed('5-celebrity-faces-embeddings.npz', embed_train_X,
                        train_y, embed_val_X, val_y)
    print(
        f"Saved faces embeddings: {train_X.shape}, {train_y.shape}, {embed_val_X.shape}, {val_y.shape}"
    )
Пример #11
0
    def __init__(
            self,
            face_det_model_path,
            predictor_path,
            face_rec_model_path,
            images_root_dir=os.getcwd(),
            debug_level="INFO",
    ):
        basicConfig(level=debug_level)
        self.dlib_gpus = dlib.cuda.get_num_devices()
        info("dlib BLAS Optimization: {}".format(dlib.DLIB_USE_BLAS))
        info("dlib LAPACK Optimization: {}".format(dlib.DLIB_USE_LAPACK))
        info("dlib CUDA Optimization: {}".format(dlib.DLIB_USE_CUDA))
        info("dlib CUDA Devices: {}".format(self.dlib_gpus))

        self.images_root_dir = images_root_dir
        self.images_file_list = []

        stamp = str(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
        self.dest_file_name = os.path.join(self.images_root_dir,
                                           "face_db_" + stamp + ".json")

        try:
            info(face_det_model_path)
            self.detector = dlib.cnn_face_detection_model_v1(
                face_det_model_path)
            self.mmod = True
            info("Using Convolutional Neural Network method.")
        except:
            self.detector = dlib.get_frontal_face_detector()
            self.mmod = False
            info("Using Histogram of Oriented Gradients method.")

        self.sp = dlib.shape_predictor(predictor_path)
        self.shapes = []
        self.facerec = dlib.face_recognition_model_v1(face_rec_model_path)
        self.descriptors = []
        self.images = []
Пример #12
0
def detectface3(filename=None):
    detector = dlib.cnn_face_detection_model_v1(
        'cache/mmod_human_face_detector.dat/mmod_human_face_detector.dat')
    capture = cv.VideoCapture('cache/test.avi')
    fps = capture.get(cv.CAP_PROP_FPS)
    waittime = 1.0 / fps
    faces = None
    frameid = -1
    while True:
        start = time.time()
        ret, frame = capture.read()
        key = cv.waitKey(1)
        if key == 27:
            break
        if ret:
            frameid += 1
            if frameid % 5 == 0:
                gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
                faces = detector(gray, 1)
            elif (frameid - 1) % 5 == 0:
                continue
            for face in faces:
                left = face.rect.left()
                top = face.rect.top()
                right = face.rect.right()
                bottom = face.rect.bottom()
                cv.rectangle(frame, (left, top), (right, bottom), (0, 255, 0),
                             3)
            t = waittime - (time.time() - start)
            print(t)
            if t > 0:
                time.sleep(t)
            cv.imshow('audio', frame)

        else:
            break
    capture.release()
    cv.destroyAllWindows()
Пример #13
0
    def __init__(self, use_cnn_face_detector=False):
        # define landmarks numbers for different regions
        self.left_eyelid = [0] + list(range(17, 22)) + [27, 28]
        self.right_eyelid = [28, 27] + list(range(22, 27)) + [16]
        self.eyelids = [self.left_eyelid, self.right_eyelid]

        self.left_eye = list(range(36, 42))
        self.right_eye = list(range(42, 48))
        self.eyes = [self.left_eye, self.right_eye]

        self.mouth = list(range(48, 60))
        self.teeth = list(range(60, 68))

        self.use_cnn = use_cnn_face_detector
        if self.use_cnn:
            self.detector_weights_path = 'mmod_human_face_detector.dat'
            self.face_detector = dlib.cnn_face_detection_model_v1(
                self.detector_weights_path)
        else:
            self.face_detector = dlib.get_frontal_face_detector()
        self.predictor_weights_path = 'shape_predictor_68_face_landmarks.dat'
        self.landmarks_predictor = dlib.shape_predictor(
            self.predictor_weights_path)
Пример #14
0
def dlib_cnn_extract_faces(image, detector=None):
    # Works very slow on cpu (without resizing)
    # Quality is better than that of dlib frontal detector
    # Foreheads are usually cut

    if detector is None:
        fd_path = os.path.join('dlib_models', 'mmod_human_face_detector.dat')
        dnn_face_detector = dlib.cnn_face_detection_model_v1(fd_path)
        detector = dnn_face_detector

    faces_results = []
    preds = detector(image)
    for pred in preds:
        w_min = pred.rect.left()
        w_max = pred.rect.right()
        h_max = pred.rect.bottom()
        h_min = pred.rect.top()
        faces_results.append(image[h_min:h_max, w_min:w_max, :])

    if len(faces_results) == 0:
        print('No faces detected')

    return faces_results
Пример #15
0
def face_cut(image_path, category, image_id, save_path):
    # print(image_path)
    print("save face into ", save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    # 0表示图片灰度化
    img = cv2.imread(image_path, 0)

    # Dlib预测器
    detector_path = os.path.join('./data/dlib/',
                                 'mmod_human_face_detector.dat')
    print("Load modeling!")
    detector = dlib.cnn_face_detection_model_v1(detector_path)

    # 检测人脸数量
    faces = detector(img, 1)

    for num, face in enumerate(faces):
        cropped = img[face.rect.top() - 50:face.rect.bottom() + 50,
                      face.rect.left() - 50:face.rect.right() + 50]
        cv2.imwrite("%s/%d%d.jpg" % (save_path, image_id, num), cropped)
        print("face save %s finished!" % category)
Пример #16
0
def initialize(detector, scale_to=2048):
    global dlib_detectors
    global keras_model
    global is_initialized
    if not is_initialized:
        dlib_cnn_face_detector_path = os.path.join(
            os.path.dirname(__file__), "mmod_human_face_detector.dat")
        if not os.path.exists(dlib_cnn_face_detector_path):
            raise Exception("Error: Unable to find %s, reinstall the lib !" %
                            (dlib_cnn_face_detector_path))

        if detector == 'cnn' or detector == "all":
            dlib_cnn_face_detector = dlib.cnn_face_detection_model_v1(
                dlib_cnn_face_detector_path)
            #DLIB and TF competiting for VRAM, so dlib must do first allocation to prevent OOM error
            dlib_cnn_face_detector(
                np.zeros((scale_to, scale_to, 3), dtype=np.uint8), 0)
            dlib_detectors.append(dlib_cnn_face_detector)

        if detector == "hog" or detector == "all":
            dlib_face_detector = dlib.get_frontal_face_detector()
            dlib_face_detector(
                np.zeros((scale_to, scale_to, 3), dtype=np.uint8), 0)
            dlib_detectors.append(dlib_face_detector)

        keras_model_path = os.path.join(os.path.dirname(__file__),
                                        "2DFAN-4.h5")
        if not os.path.exists(keras_model_path):
            print("Error: Unable to find %s, reinstall the lib !" %
                  (keras_model_path))
        else:
            print("Info: initializing keras model...")
            keras_model = keras.models.load_model(
                keras_model_path,
                custom_objects={'TorchBatchNorm2D': TorchBatchNorm2D})

        is_initialized = True
Пример #17
0
    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
Пример #18
0
def detect_face(image_path, default_max_size=800,size = 300, padding = 0.25):
    cnn_face_detector = dlib.cnn_face_detection_model_v1('dlib_models/mmod_human_face_detector.dat')
    sp = dlib.shape_predictor('dlib_models/shape_predictor_5_face_landmarks.dat')
    base = 2000  # largest width and height
    img = dlib.load_rgb_image(image_path)

    old_height, old_width, _ = img.shape

    if old_width > old_height:
        new_width, new_height = default_max_size, int(default_max_size * old_height / old_width)
    else:
        new_width, new_height =  int(default_max_size * old_width / old_height), default_max_size
    img = dlib.resize_image(img, rows=new_height, cols=new_width)

    dets = cnn_face_detector(img, 1)
    num_faces = len(dets)
    if num_faces == 0:
        raise AttributeError("Sorry, there were no faces found.")
    # Find the 5 face landmarks we need to do the alignment.
    faces = dlib.full_object_detections()
    for detection in dets:
        rect = detection.rect
        faces.append(sp(img, rect))
    return dlib.get_face_chips(img, faces, size=size, padding = padding)
def align_and_save_dir(src_dir,
                       save_dir,
                       template_path='./pretrain_models/FFHQ_template.npy',
                       template_scale=2,
                       use_cnn_detector=True):
    if use_cnn_detector:
        detector = dlib.cnn_face_detection_model_v1(
            './pretrain_models/mmod_human_face_detector.dat')
    else:
        detector = dlib.get_frontal_face_detector()
    sp = dlib.shape_predictor(
        './pretrain_models/shape_predictor_5_face_landmarks.dat')

    for name in os.listdir(src_dir):
        img_path = os.path.join(src_dir, name)
        img = dlib.load_rgb_image(img_path)

        points = get_points(img, detector, sp)
        if points is not None:
            save_path = os.path.join(save_dir, name)
            align_and_save(img, save_path, points, template_path,
                           template_scale)
        else:
            print('No face detected in', img_path)
Пример #20
0
def eval_diff_img_size():
    szs = {
        "720x1280": (720, 1280, 1),
        "360x640": (360, 640, 1),
        "180x320": (180, 320, 1),
        "90x160": (90, 160, 1)
    }
    cnn_face_detector = dlib.cnn_face_detection_model_v1(args.face_model)

    for sc in [0, 1, 2]:
        for s_name in szs:
            sz = szs[s_name]
            x = np.random.randint(low=0, high=255, size=sz, dtype=np.uint8)

            ts = []
            for i in range(100):
                et = time.time()
                _ = cnn_face_detector(x, sc)
                ts.append(time.time() - et)

            a = str("average for " + s_name + " using scale " + str(sc)).ljust(
                40, ' ')
            b = str(round(np.average(ts), 7)).ljust(5, ' ')
            print a + b
Пример #21
0
def load_image(image_path, shape_predictor):
    # detector = dlib.get_frontal_face_detector()
    detector = dlib.cnn_face_detection_model_v1("./mmod_human_face_detector.dat")
    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)
    start = time.time()
    # rects = detector(gray, 2)
    rects = detector(image, 1)
    
    # print(time.time() - start)
    # print(rects)
    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):
            # print(rects[i])
            start = time.time()
            # print("================", rects[i])
            rec = dlib.rectangle(rects[i].rect.left(),rects[i].rect.top(),rects[i].rect.right(),rects[i].rect.bottom())
            aligned_image = fa.align(image, gray, rec)
            # print(time.time() - start)
            aligned_images.append(aligned_image)
            (x, y, w, h) = rect_to_bb(rec)
            image = cv2.rectangle(image, (x, y), (x + w, y + h), color=(255, 0, 0), thickness=2)
            XY.append((x, y))

            shape = predictor(gray, rec)
            r = numpy.matrix([[p.x, p.y] for p in shape.parts()])
            # print("shape===>", r)
        return np.array(aligned_images), image, rect_nums, XY
Пример #22
0
def get_dst_vectors_from_single_image(file_name):
    data = np.zeros((1, 128))  # 定义一个128维的空向量data
    label = []  # 定义空的list存放人脸的标签
    detector = dlib.cnn_face_detection_model_v1(
        '/home/loheagn/boyasite/main/mmod_human_face_detector.dat')
    sp = dlib.shape_predictor(
        '/home/loheagn/boyasite/main/shape_predictor_68_face_landmarks.dat')
    facerec = dlib.face_recognition_model_v1(
        '/home/loheagn/boyasite/main/dlib_face_recognition_resnet_model_v1.dat'
    )
    img = cv2.imread(file_name)  # 使用opencv读取图像数据
    dets = detector(img, 1)  # 使用检测算子检测人脸,返回的是所有的检测到的人脸区域

    for k, d in enumerate(dets):
        rec = dlib.rectangle(d.rect.left(), d.rect.top(), d.rect.right(),
                             d.rect.bottom())
        shape = sp(img, rec)  # 获取landmark
        face_descriptor = facerec.compute_face_descriptor(
            img, shape)  # 使用resNet获取128维的人脸特征向量
        faceArray = np.array(face_descriptor).reshape(
            (1, 128))  # 转换成numpy中的数据结构
        data = np.concatenate((data, faceArray))  # 拼接到事先准备好的data当中去
        label.append(label)  # 保存标签
    return data[1:, :]
Пример #23
0
    def __init__(self):
        # OpenCV HAAR
        self._faceCascade = cv2.CascadeClassifier(
            'Data/Model/haarcascade_frontalface_default.xml')

        # OpenCV DNN supports 2 networks.
        # 1. FP16 version of the original caffe implementation ( 5.4 MB )
        # 2. 8 bit Quantized version using Tensorflow ( 2.7 MB )
        DNN = "TF"

        if DNN == "CAFFE":
            self._modelFile = "Data/Model/res10_300x300_ssd_iter_140000_fp16.caffemodel"
            self._configFile = "Data/Model/deploy.prototxt"
            self._net = cv2.dnn.readNetFromCaffe(self._configFile,
                                                 self._modelFile)
        else:
            self._modelFile = "Data/Model/opencv_face_detector_uint8.pb"
            self._configFile = "Data/Model/opencv_face_detector.pbtxt"
            self._net = cv2.dnn.readNetFromTensorflow(self._modelFile,
                                                      self._configFile)

        self._conf_threshold = 0.8

        # DLIB HoG
        self._hogFaceDetector = dlib.get_frontal_face_detector()

        # DLIB MMOD
        self._dnnFaceDetector = dlib.cnn_face_detection_model_v1(
            'Data/Model/mmod_human_face_detector.dat')

        # TinyFace
        self._MAX_INPUT_DIM = 5000.0
        self._prob_thresh = float(0.5)
        self._nms_tresh = float(0.1)
        self._lw = int(3)
        self._model = tiny_face_model.Model('Data/Model/hr_res101.weight')
Пример #24
0
    def __init__(self,
                 image=None,
                 video=None,
                 method=None,
                 capture_image=False,
                 capture_video=False,
                 max_width=512,
                 max_height=512,
                 show_faces=False):

        self.METHODS = {
            'cascade': self.cascade,
            'hog': self.hog,
            'cnn': self.cnn,
            'cvlib': self.cvlib,
            'mtcnn': self.mtcnn
        }

        self.max_height = max_height
        self.max_width = max_width
        self.show_faces = show_faces
        self.faces = []
        self.confidences = []

        if method not in self.METHODS:
            raise Exception(f"Method has to be one of {self.METHODS}")

        if method == 'cnn':
            self.dnnFaceDetector = dlib.cnn_face_detection_model_v1(
                CNN_FACE_DETECTION_MODEL)
        elif method == 'hog':
            self.faceDetect = dlib.get_frontal_face_detector()
        elif method == 'cascade':
            self.faceCascade = cv2.CascadeClassifier(HAAR_CASC_PATH)
        elif method == "mtcnn":
            self.mtcnn_detector = MTCNN()
Пример #25
0
def image(arg1, arg2):
    # Load in and convert source image to into grayscale
    img = cv2.imread(arg1, 1)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.resize(gray, (0, 0), fx=0.25, fy=0.25)

    # Ensure the file is opened
    if np.shape(img) == ():
        print("Error opening the image file")
        exit()

    # Detect faces using CNN model
    face_detect = dlib.cnn_face_detection_model_v1(
        'mmod_human_face_detector.dat')
    face = face_detect(gray, 1)

    # Blur faces
    blur_cnn(img, face)

    # Display image with blur
    cv2.imshow("Output", img)
    cv2.imwrite(arg2, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Пример #26
0
import cv2
import dlib
import argparse
import time
import numpy as np
from utils.inference import get_suffix, crop_img, parse_roi_box_from_landmark
import glob
import dlib
import sys
import ntpath

dlib_landmark_model = 'models/shape_predictor_68_face_landmarks.dat'
cnn_face_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')

face_regressor = dlib.shape_predictor(dlib_landmark_model)


def save_img(image, filename, save_path):
    image_name = ntpath.basename(filename)
    wfp_crop = save_path + '/{}'.format(image_name)
    print(wfp_crop)
    cv2.imwrite(wfp_crop, image)
    

def crop_process(image, filename, folder_path, save_path, size=224):

    if image is None:
        print("Could not read input image")
        return False

Пример #27
0
#   Also note that this example requires Numpy which can be installed
#   via the command:
#       pip install numpy

import sys
import dlib

if len(sys.argv) < 3:
    print(
        "Call this program like this:\n"
        "   ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg\n"
        "You can get the mmod_human_face_detector.dat file from:\n"
        "    http://dlib.net/files/mmod_human_face_detector.dat.bz2")
    exit()

cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1])
win = dlib.image_window()

for f in sys.argv[2:]:
    print("Processing file: {}".format(f))
    img = dlib.load_rgb_image(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time.  This will make everything bigger and allow us to detect more
    # faces.
    dets = cnn_face_detector(img, 1)
    '''
    This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects.
    These objects can be accessed by simply iterating over the mmod_rectangles object
    The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score.
    
    It is also possible to pass a list of images to the detector.
Пример #28
0
 def init_dlib(self, detection_path, landmark5_path, landmark68_path):
     """Initialize the dlib detectors and predictors."""
     self.face_detector = dlib.cnn_face_detection_model_v1(detection_path)
     self.shape_predictor_5 = dlib.shape_predictor(landmark5_path)
     self.shape_predictor_68 = dlib.shape_predictor(landmark68_path)
Пример #29
0
    def __init__(self,
                 landmarks_type,
                 network_size=NetworkSize.LARGE,
                 enable_cuda=True,
                 enable_cudnn=True,
                 flip_input=False,
                 use_cnn_face_detector=False):
        self.enable_cuda = enable_cuda
        self.use_cnn_face_detector = use_cnn_face_detector
        self.flip_input = flip_input
        self.landmarks_type = landmarks_type
        base_path = os.path.join(appdata_dir('face_alignment'), "data")

        if not os.path.exists(base_path):
            os.makedirs(base_path)

        if enable_cudnn and self.enable_cuda:
            torch.backends.cudnn.benchmark = True

        # Initialise the face detector
        if self.use_cnn_face_detector:
            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()

        # Initialise the face alignemnt networks
        self.face_alignemnt_net = FAN(int(network_size))
        if landmarks_type == LandmarksType._2D:
            network_name = '2DFAN-' + str(int(network_size)) + '.pth.tar'
        else:
            network_name = '3DFAN-' + str(int(network_size)) + '.pth.tar'
        fan_path = os.path.join(base_path, network_name)

        if not os.path.isfile(fan_path):
            print(
                "Downloading the Face Alignment Network(FAN). Please wait...")

            fan_temp_path = os.path.join(base_path, network_name + '.download')

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

            request_file.urlretrieve(
                "https://www.adrianbulat.com/downloads/python-fan/" +
                network_name, os.path.join(fan_temp_path))

            os.rename(os.path.join(fan_temp_path), os.path.join(fan_path))

        fan_weights = torch.load(fan_path,
                                 map_location=lambda storage, loc: storage)

        self.face_alignemnt_net.load_state_dict(fan_weights)

        if self.enable_cuda:
            self.face_alignemnt_net.cuda()
        self.face_alignemnt_net.eval()

        # Initialiase the depth prediciton network
        if landmarks_type == LandmarksType._3D:
            self.depth_prediciton_net = ResNetDepth()
            depth_model_path = os.path.join(base_path, 'depth.pth.tar')
            if not os.path.isfile(depth_model_path):
                print(
                    "Downloading the Face Alignment depth Network (FAN-D). Please wait..."
                )

                depth_model_temp_path = os.path.join(base_path,
                                                     'depth.pth.tar.download')

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

                request_file.urlretrieve(
                    "https://www.adrianbulat.com/downloads/python-fan/depth.pth.tar",
                    os.path.join(depth_model_temp_path))

                os.rename(os.path.join(depth_model_temp_path),
                          os.path.join(depth_model_path))

            depth_weights = torch.load(
                depth_model_path, map_location=lambda storage, loc: storage)
            depth_dict = {
                k.replace('module.', ''): v
                for k, v in depth_weights['state_dict'].items()
            }
            self.depth_prediciton_net.load_state_dict(depth_dict)

            if self.enable_cuda:
                self.depth_prediciton_net.cuda()
            self.depth_prediciton_net.eval()
Пример #30
0
ImageFile.LOAD_TRUNCATED_IMAGES = True

face_detector = dlib.get_frontal_face_detector()

predictor_68_point_model = face_recognition_models.pose_predictor_model_location(
)
pose_predictor_68_point = dlib.shape_predictor(predictor_68_point_model)

predictor_5_point_model = face_recognition_models.pose_predictor_five_point_model_location(
)
pose_predictor_5_point = dlib.shape_predictor(predictor_5_point_model)

cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location(
)
cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

face_recognition_model = face_recognition_models.face_recognition_model_location(
)
face_encoder = dlib.face_recognition_model_v1(face_recognition_model)


def _rect_to_css(rect):
    """
    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
    """
    return rect.top(), rect.right(), rect.bottom(), rect.left()
Пример #31
0
def show_detection(image, faces):
    """Draws a rectangle over each detected face"""

    # faces contains a list of mmod_rectangle objects
    # The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score
    # Therefore, we iterate over the detected mmod_rectangle objects accessing dlib.rect to draw the rectangle

    for face in faces:
        cv2.rectangle(image, (face.rect.left(), face.rect.top()), (face.rect.right(), face.rect.bottom()), (255, 0, 0),
                      10)
    return image


# Load CNN detector from dlib:
cnn_face_detector = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")

# Load image and convert to grayscale:
img = cv2.imread("test_face_detection.jpg")

# Resize the image to attain reasonable speed:
# img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)

# Detect faces:
rects = cnn_face_detector(img, 0)

# Draw face detections:
img_faces = show_detection(img.copy(), rects)

# Create the dimensions of the figure and set title:
fig = plt.figure(figsize=(10, 5))
Пример #32
0
    print("\tsudo ./install.sh\n")
    sys.exit(1)

# Read config from disk
config = configparser.ConfigParser()
config.read(path + "/../config.ini")

if not os.path.exists(config.get("video", "device_path")):
    print(
        "Camera path is not configured correctly, please edit the 'device_path' config value."
    )
    sys.exit(1)

use_cnn = config.getboolean("core", "use_cnn", fallback=False)
if use_cnn:
    face_detector = dlib.cnn_face_detection_model_v1(
        path + "/../dlib-data/mmod_human_face_detector.dat")
else:
    face_detector = dlib.get_frontal_face_detector()

pose_predictor = dlib.shape_predictor(
    path + "/../dlib-data/shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1(
    path + "/../dlib-data/dlib_face_recognition_resnet_model_v1.dat")

user = builtins.howdy_user
# The permanent file to store the encoded model in
enc_file = path + "/../models/" + user + ".dat"
# Known encodings
encodings = []

# Make the ./models folder if it doesn't already exist
Пример #33
0
        model = net.net(torchvision.models.resnet.BasicBlock, [3, 4, 6, 3], 66)
    elif args.arch == 'ResNet101':
        model = net.net(torchvision.models.resnet.Bottleneck, [3, 4, 23, 3],
                        66)
    elif args.arch == 'ResNet152':
        model = net.net(torchvision.models.resnet.Bottleneck, [3, 8, 36, 3],
                        66)

    else:
        if args.arch != 'ResNet50':
            print('Invalid value for architecture is passed! '
                  'The default value of ResNet50 will be used instead!')
        model = net.net(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66)

    # Dlib face detection model
    cnn_face_detector = dlib.cnn_face_detection_model_v1(args.face_model)

    print('Loading snapshot.')
    # Load snapshot
    saved_state_dict = torch.load(snapshot_path)
    model.load_state_dict(saved_state_dict)

    print('Loading data.')

    transformations = transforms.Compose([
        transforms.Scale(224),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])  #crop center part of image and scale image
Пример #34
0
if DNN=="CAFFE":
    modelFile = "res10_300x300_ssd_iter_140000_fp16.caffemodel"
    configFile = "deploy.prototxt"
    net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
else:
    modelFile = "opencv_face_detector_uint8.pb"
    configFile = "opencv_face_detector.pbtxt"
    net = cv2.dnn.readNetFromTensorflow(modelFile, configFile)

conf_threshold = 0.7

# DLIB HoG
hogFaceDetector = dlib.get_frontal_face_detector()

# DLIB MMOD
dnnFaceDetector = dlib.cnn_face_detection_model_v1("./mmod_human_face_detector.dat")


def detectFaceOpenCVHaar(faceCascade, frame, inHeight=300, inWidth=0):
    frameOpenCVHaar = frame.copy()
    frameHeight = frameOpenCVHaar.shape[0]
    frameWidth = frameOpenCVHaar.shape[1]
    if not inWidth:
        inWidth = int((frameWidth / frameHeight) * inHeight)

    scaleHeight = frameHeight / inHeight
    scaleWidth = frameWidth / inWidth

    frameOpenCVHaarSmall = cv2.resize(frameOpenCVHaar, (inWidth, inHeight))
    frameGray = cv2.cvtColor(frameOpenCVHaarSmall, cv2.COLOR_BGR2GRAY)
Пример #35
0
    def __init__(self, landmarks_type, network_size=NetworkSize.LARGE,
                 enable_cuda=True, enable_cudnn=True, flip_input=False,
                 use_cnn_face_detector=False):
        self.enable_cuda = enable_cuda
        self.use_cnn_face_detector = use_cnn_face_detector
        self.flip_input = flip_input
        self.landmarks_type = landmarks_type
        base_path = os.path.join(appdata_dir('face_alignment'), "data")

        if not os.path.exists(base_path):
            os.makedirs(base_path)

        if enable_cudnn and self.enable_cuda:
            torch.backends.cudnn.benchmark = True

        # Initialise the face detector
        if self.enable_cuda or self.use_cnn_face_detector:
            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...")

                request_file.urlretrieve(
                    "https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
                    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()

        # Initialise the face alignemnt networks
        self.face_alignemnt_net = FAN(int(network_size))
        if landmarks_type == LandmarksType._2D:
            network_name = '2DFAN-' + str(int(network_size)) + '.pth.tar'
        else:
            network_name = '3DFAN-' + str(int(network_size)) + '.pth.tar'
        fan_path = os.path.join(base_path, network_name)

        if not os.path.isfile(fan_path):
            print("Downloading the Face Alignment Network(FAN). Please wait...")

            request_file.urlretrieve(
                "https://www.adrianbulat.com/downloads/python-fan/" +
                network_name, os.path.join(fan_path))

        fan_weights = torch.load(
            fan_path,
            map_location=lambda storage,
            loc: storage)

        self.face_alignemnt_net.load_state_dict(fan_weights)

        if self.enable_cuda:
            self.face_alignemnt_net.cuda()
        self.face_alignemnt_net.eval()

        # Initialiase the depth prediciton network
        if landmarks_type == LandmarksType._3D:
            self.depth_prediciton_net = ResNetDepth()
            depth_model_path = os.path.join(base_path, 'depth.pth.tar')
            if not os.path.isfile(depth_model_path):
                print(
                    "Downloading the Face Alignment depth Network (FAN-D). Please wait...")

                request_file.urlretrieve(
                    "https://www.adrianbulat.com/downloads/python-fan/depth.pth.tar",
                    os.path.join(depth_model_path))

            depth_weights = torch.load(
                depth_model_path,
                map_location=lambda storage,
                loc: storage)
            depth_dict = {
                k.replace('module.', ''): v for k,
                v in depth_weights['state_dict'].items()}
            self.depth_prediciton_net.load_state_dict(depth_dict)

            if self.enable_cuda:
                self.depth_prediciton_net.cuda()
            self.depth_prediciton_net.eval()
Пример #36
0
except:
    print("Please install `face_recognition_models` with this command before using `face_recognition`:")
    print()
    print("pip install git+https://github.com/ageitgey/face_recognition_models")
    quit()

face_detector = dlib.get_frontal_face_detector()

predictor_68_point_model = face_recognition_models.pose_predictor_model_location()
pose_predictor_68_point = dlib.shape_predictor(predictor_68_point_model)

predictor_5_point_model = face_recognition_models.pose_predictor_five_point_model_location()
pose_predictor_5_point = dlib.shape_predictor(predictor_5_point_model)

cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

face_recognition_model = face_recognition_models.face_recognition_model_location()
face_encoder = dlib.face_recognition_model_v1(face_recognition_model)


def _rect_to_css(rect):
    """
    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
    """
    return rect.top(), rect.right(), rect.bottom(), rect.left()

Пример #37
0
#       pip install scikit-image
#   Or downloaded from http://scikit-image.org/download.html.

import sys
import dlib
from skimage import io

if len(sys.argv) < 3:
    print(
        "Call this program like this:\n"
        "   ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg\n"
        "You can get the mmod_human_face_detector.dat file from:\n"
        "    http://dlib.net/files/mmod_human_face_detector.dat.bz2")
    exit()

cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1])
win = dlib.image_window()

for f in sys.argv[2:]:
    print("Processing file: {}".format(f))
    img = io.imread(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time.  This will make everything bigger and allow us to detect more
    # faces.
    dets = cnn_face_detector(img, 1)
    '''
    This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects.
    These objects can be accessed by simply iterating over the mmod_rectangles object
    The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score.
    
    It is also possible to pass a list of images to the detector.