def crop_and_align_images(image_path, output_path, landmark_model_path):
    """Method to crop and align each image in img_path and output into processed_img_path"""
    if os.path.exists(output_path):  # if output directory exists
        print(output_path + " exists, skipping")
        return

    os.makedirs(output_path, exist_ok=True)

    # Create face detector and landmark detector
    detector = dlib.get_frontal_face_detector()
    sp = dlib.shape_predictor(landmark_model_path)
    for image_name in tqdm(os.listdir(image_path)):  # for each image
        # Load image and process
        img = dlib.load_rgb_image(join(image_path, image_name))
        image_name = image_name.split(".")
        detections = detector(img, 1)

        for index in range(len(detections)):
            detection = detections[index]
            landmarks = sp(img, detection)
            processed_img = dlib.get_face_chip(img,
                                               landmarks,
                                               size=CROP_SIZE,
                                               padding=CROP_PADDING)
            dlib.save_image(
                processed_img,
                join(
                    output_path, "{}_{}.{}".format(image_name[0], str(index),
                                                   image_name[1])))
Ejemplo n.º 2
0
def aligned_face_cropper(indexedFileNames, crpFileDir, imgRes, scope):
    # 'indexedFileNames'는 총 분량으로부터 할당받아 온 원본 파일들의 이름과, 그 할당분의 순번(index), 'crpFileDir'은 얼굴 부분만 자른 것을 저장할 경로.
    # 'imgRes'는 이미지를 저장할 해상도(다수 선택 가능)에 대한 Parameter, 'scope'는 이미지 내에서 인물 부분이 차지하는 범위를 설정하는 Parameter 값이다.
    from tqdm import tqdm
    import matplotlib.pyplot as plt
    import dlib
    import time

    cnt = 0  # 출력돼 나온 총 파일의 수를 계수할 변수를 0으로 초기화하며 선언한다.
    coreNum, fileNames = indexedFileNames
    # 'indexedFileNames'은 멀티프로세싱을 위해 총 작업량으로부터 분할받은 부분과 그 index 정보로,
    # index 부분을 'coreNum'으로, 할당받은 자료 부분을 'fileNames'로 각각 Un-packaging.
    # 'coreNum'이라고 해서 실제 CPU 내에서 해당 Core의 물리적인 할당 번호를 의미하는 것은 아니다!!
    # 중복의 방지와 진행률 표시 시의 구분을 위한 단순한 코드 상의 넘버링일 뿐이다.

    timeFlag = time.strftime('%m%d-%H%M%S', time.localtime(time.time()))   # 모듈 작동 당시의 시각을 기억해 둠.(파일명의 중복 방지에 활용하기 위함)
    shapePrd = dlib.shape_predictor('D:/HSH/Model/shape_predictor_68_face_landmarks.dat')   # 학습된 형태 예측기의 모델 파일을 불러옴.(!!!!! Github 용량 제한으로 포함돼있지 않음 !!!!!)
    faceDtcr = dlib.get_frontal_face_detector()   # 얼굴 정면을 탐지하는 dlib의 Method를 변수화.
    faceDirs = []   # 출력한 파일을 다른 함수 등에 활용할 수 있도록, 작업을 통해 출력된 파일의 이름들을 저장할 배열 선언.

    for fileName in tqdm(fileNames, desc="@Core {} - Aligning & Cropping".format(coreNum)):
        # 특정 폴더 내 사진들의 이름 리스트인 'fileNames'의 내용에 따라 이미지를 불러온 후, 컨테이너에 적재.
        if plt.imread(fileName).shape[0] < 600:
            continue   # 읽어 올 이미지의 크기가 정사각형 한 차원 기준 600픽셀 미만이라면, 작업을 진행하지 않고 다음 파일로 넘어가도록 처리하였다.
        imgLoaded = dlib.load_rgb_image(fileName)   # 파일을 RGB Color 이미지로 불러옴.
        dtcdFaces = faceDtcr(imgLoaded, 1)   # 불러온 이미지에서 얼굴(들)을 탐지해 변수에 저장.
        objects = dlib.full_object_detections()    # 탐지해 낸 랜드마크 포인트들을 담을 변수를 선언.

        for dtcdFace in dtcdFaces:  # 찾은 얼굴 부분 안에서 모델을 이용해 랜드마크 포인트들을 탐지.
            if dtcdFace.right() - dtcdFace.left() < 200:
                continue   # 탐지해 낸 얼굴 부분의 한 차원 기준 크기가 200픽셀 미만이면, 작업을 진행하지 않고 다음 파일로 넘어간다.

            shape = shapePrd(imgLoaded, dtcdFace)
            objects.append(shape)  # 찾아낸 랜드마크 포인트들의 정보를 앞서 선언해 둔 변수에 적재.

        for res in imgRes:
            # 이미지를 저장할 해상도를 복수 선택할 수 있게 해 뒀으므로, 그 선택지를 입력한 수 만큼 반복해 작동한다.
            saveCnt = 0   # 저장한 파일을 계수할 변수 선언.
            # 'cropFileDir'은 Crop해 온 파일들이 저장될 포괄적 폴더의 경로, 'res'는 그 폴더 안에 생성될 또 다른 폴더의 이름이자 저장될 해상도이다.
            dir = '{}{}/'.format(crpFileDir, str(res))
            try:
                # 탐지한 포인트 정보와 주어진 Parameter 값들을 가지고 얼굴을 바르게 정렬한 후 잘라낸 다음, 적재한다.
                # 개별 파일에 대한 작업 도중 오류가 발생할 시(얼굴을 탐지하지 못했다거나 등의 경우),
                # 그 오류로 인해 전체 과정이 중단돼버리는 대신, 오류를 무시하고 다음 파일로 그냥 넘어갈 수 있도록 Exception 처리를 하였다.
                # 즉, 특정 몇몇 파일로부터 얼굴을 찾지 못했더라도 이를 그냥 무시하고, 다른 파일에서라도 얼굴을 찾아냈다면 그 것들은 온전히 가져올 수 있도록 한 것이다.
                face = dlib.get_face_chips(imgLoaded, objects, size=res, padding=scope)
                # 앞서 추출해 낸 얼굴을 Parameter값(해상도, 인물 부분의 표현 범위)에 따라 .png 파일로 출력.
                # 동일 인물의 중복 출력을 방지하기 위해 한 사진 당 얼굴 하나씩만을 출력하도록 했다.
                fileDir = '{}{}_Aligned_{}_{}_{}_{}.png'.format(dir, int(scope), timeFlag, coreNum, res, cnt)  # 파일명을 형식에 따라 설정.
                dlib.save_image(face[0], fileDir)  # 'face'배열의 가장 첫 내용만을 지정한 파일명으로 저장.
                faceDirs.append(fileDir)  # 나중 과정에 활용할 수 있도록, 잘라내 저장한 얼굴들에 대한 파일명을 배열에 추가.
                saveCnt += 1  # 저장한 파일의 수를 1 증가시킨다.
            except Exception as e:
                break

        cnt += saveCnt
        # 'saveCnt'는 for Loop의 내부변수이기 때문에, 매 Iteration 시마다 초기화될 것이므로,
        # 회 당 산출된 값을 매 Iteration이 종료될 때마다 전역변수에 더해놓아 줘야 그 수가 의미를 상실하지 않는다.

    return faceDirs, cnt  # 출력한 파일들의 이름들 목록과 그 총 수를 호출측에 반환.
Ejemplo n.º 3
0
def detect_face(image_paths, SAVE_DETECTED_AT, 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
    for index, image_path in enumerate(image_paths):
        if index % 1000 == 0:
            print('---%d/%d---' % (index, len(image_paths)))

        img = dlib.load_rgb_image(image_path)
        img = dlib.resize_image(img, 628, 628)
        dets = cnn_face_detector(img, 1)
        num_faces = len(dets)
        if num_faces == 0:
            print(
                "Sorry, there were no faces found in '{}'".format(image_path))
            continue
        # 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))
        images = dlib.get_face_chips(img, faces, size=size, padding=padding)
        for idx, image in enumerate(images):
            img_name = image_path.split("/")[-1]
            path_sp = img_name.split(".")
            face_name = os.path.join(
                SAVE_DETECTED_AT,
                path_sp[0] + "_" + "face" + str(idx) + "." + path_sp[-1])
            dlib.save_image(image, face_name)
Ejemplo n.º 4
0
    def label_face(self, absoluteFile: str, filename: str, label: str):
        if not label:
            return False
        image = dlib.load_rgb_image(absoluteFile)
        start_time = time.clock()
        dets_hog = self.hog_face_detector(image, 1)
        self.step_times.append("HOG + SVM Detection: %.3f s" %
                               (time.clock() - start_time))
        if len(dets_hog) != 1:
            return False
        for i, d in enumerate(dets_hog):
            start_time = time.clock()
            shape = self.predictor(image, d)
            self.step_times.append("Landmark Detection: %.3f s" %
                                   (time.clock() - start_time))

            start_time = time.clock()
            face_descriptor = self.facerec.compute_face_descriptor(
                image, shape)
            self.step_times.append("Face Descriptor Computation: %.3f s" %
                                   (time.clock() - start_time))

            self.face_descriptors[label] = face_descriptor
            draw_rectangle(image, d)
        print("labeled face as " + label)
        processed_file = self.path_to_processed_file(filename, label)
        dlib.save_image(image, processed_file)
Ejemplo n.º 5
0
def create_face_patches(file_name):
    print("Processing file: {}".format(file_name))
    err_flag = False
    img = dlib.load_rgb_image(file_name)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    file_name_ = os.path.splitext(os.path.basename(file_name))[0]
    # Ask the detector to find the bounding boxes of each face. 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 = face_detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = face_predictor(img, d)
        l0 = abs(shape.part(27).y - shape.part(51).y) # distance between point 28 and 52 as reference
        l = [70, 60, 42, 24]
        ratio = []
        pad = [24, 24, 24, 24]
        for l_ in l:
            ratio.append(l_/l0)
        # resize image to 4 image with 4 scales
        for i in range(4):
            resize_img = cv2.resize(img, (round(img.shape[1] * ratio[i]) , round(img.shape[0] * ratio[i])))

            for pid in patches_scale[i]:
                xpatch_points = [round(ratio[i] * shape.part(x-1).x) for x in patches[pid]]
                ypatch_points = [round(ratio[i] * shape.part(x-1).y) for x in patches[pid]]
        
                maxx = max(xpatch_points)
                minx = min(xpatch_points)
                maxy = max(ypatch_points)
                miny = min(ypatch_points) 
                if ((minx < 0) or (miny < 0)):
                    print("train image error should be removed")
                    remove_list.append(file_name_)
                    break
                mid_pointx = round((maxx + minx)/2)
                mid_pointy = round((maxy + miny)/2)

                crop_image = resize_img[mid_pointy-pad[i]:mid_pointy +pad[i],mid_pointx-pad[i]:mid_pointx +pad[i]]
                if (crop_image.shape[0] < 48) or (crop_image.shape[1] < 48):
                    print(file_name_,": crop image fail")
                    print("scale", i,  "pid:", pid, " shape[0]: ", crop_image.shape[0], " shape[1]: ", crop_image.shape[1])
                    remove_list.append(file_name_)
                    err_flag = True
                    break
                if not (os.path.isdir('debug/scale(' + str(i) + ')')):
                    os.mkdir('debug/scale(' + str(i) + ')')
                if not (os.path.isdir('debug/scale(' + str(i) + ')/patch(' + str(pid) + ')')):
                    os.mkdir('debug/scale(' + str(i) + ')/patch(' + str(pid) + ')')
                # right land mark patches are mirrored
                img_input_i = Input_Image(i, pid, crop_image)
                image_input.append(img_input_i)       
                dlib.save_image(crop_image, 'debug/scale(' + str(i) + ')/patch(' + str(pid) + ')' + '/' + file_name_ + '.jpg')
            if err_flag != False:
                break
    return err_flag
Ejemplo n.º 6
0
 def detect_faces(self, absoluteFile: str, filename: str):
     image = dlib.load_rgb_image(absoluteFile)
     start_time = time.clock()
     face_boundaries = self.hog_face_detector(image, 1)
     self.step_times.append("HOG + SVM Detection: %.3f s" %
                            (time.clock() - start_time))
     for i, d in enumerate(face_boundaries):
         draw_rectangle(image, d)
     processed_file = self.path_to_processed_file(filename, "facedetection")
     dlib.save_image(image, processed_file)
Ejemplo n.º 7
0
def draw_landmark(file_name, start_point, end_point):
    print("Processing file: {}".format(file_name))
    img = dlib.load_rgb_image(file_name)
    dets = face_detector(img, 1)
    for k, d in enumerate(dets):
        shape = face_predictor(img, d)
        for i in range(start_point, end_point):
            cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1)
            # Displaying the image 
        #cv2.imshow('image', img) 
    file_name_ = os.path.splitext(os.path.basename(file_name))[0]
    dlib.save_image(img, 'debug/' + file_name_ + "_debug.jpg")
Ejemplo n.º 8
0
def detect_face(image_paths,
                SAVE_DETECTED_AT,
                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
    rects = []
    for index, image_path in enumerate(image_paths):
        if index % 1000 == 0:
            print('---%d/%d---' % (index, len(image_paths)))

        img = dlib.load_rgb_image(image_path)

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

        dets = cnn_face_detector(img, 1)
        num_faces = len(dets)
        if num_faces == 0:
            print(
                "Sorry, there were no faces found in '{}'".format(image_path))
            continue
        # 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))
            rect_tpl, rect_in_origin = reverse_resized_rect(rect, resize_ratio)
            rects.append(rect_in_origin)
        images = dlib.get_face_chips(img, faces, size=size, padding=padding)
        for idx, image in enumerate(images):
            img_name = image_path.split("/")[-1]
            path_sp = img_name.split(".")
            face_name = os.path.join(
                SAVE_DETECTED_AT,
                path_sp[0] + "_" + "face" + str(idx) + "." + path_sp[-1])
            dlib.save_image(image, face_name)

    return rects
Ejemplo n.º 9
0
 def use_trained_detector(self, absoluteFile: str, filename: str):
     print(type(SimpleHTTPRequestHandler.trained_detector))
     image = dlib.load_rgb_image(absoluteFile)
     start_time = time.clock()
     dets_hog = SimpleHTTPRequestHandler.trained_detector(image, 1)
     self.step_times.append(
         "Using self-trained HOG + SVM Detection: %.3f s" %
         (time.clock() - start_time))
     for i, d in enumerate(dets_hog):
         print("detected object: " + str(d))
         draw_rectangle(image, d)
     processed_file = self.path_to_processed_file(filename, "trainedhog")
     dlib.save_image(image, processed_file)
Ejemplo n.º 10
0
 def landmark_faces(self, absoluteFile: str, filename: str):
     image = dlib.load_rgb_image(absoluteFile)
     start_time = time.clock()
     dets_hog = self.hog_face_detector(image, 1)
     self.step_times.append("HOG + SVM Detection: %.3f s" %
                            (time.clock() - start_time))
     for i, d in enumerate(dets_hog):
         start_time = time.clock()
         shape = self.predictor(image, d)
         self.step_times.append("Landmark Detection: %.3f s" %
                                (time.clock() - start_time))
         draw_marker(image, shape.parts())
     processed_file = self.path_to_processed_file(filename, "facelandmark")
     dlib.save_image(image, processed_file)
Ejemplo n.º 11
0
def save(batches, config, indices=None):
    """Save preprocessed images."""
    indices = indices or {v: 0 for v in ds._category.keys() + ['index']}
    print(indices)
    for Xbatch, ybatch in batches:
        for X, y in zip(Xbatch, ybatch):
            if y == 'neutral':
                indices['index'] += 1
            indices[y] += 1
            filename = '{0:08}_{1}_{2}.jpg'.format(indices['index'], y,
                                                   indices[y])
            destination = os.path.join(config['destination'], filename)
            dlib.save_image(X, destination)
            print(destination)
    return indices
Ejemplo n.º 12
0
def face_check(imagesfp):
    """face_check is used to perform
    a preliminary check on the images in the filepath
    to make sure that there won't be any problems down the line
    in the program. For example, if dlib is unable to detect a
    face in one of the images, it will remove it from the
    filepath so that it is not included in the face average.
    If dlib detects more than one face in an image, it will
    create new images corresponding to each face in the original image
    and then remove the original image, so that an accurate average
    can be found. If no faces are found in any of the images, it will
    exit the program.

    **Parameters**
    imagesfp: str
        The name of the folder containing the images
        to be averaged

    **Returns**
    None
    """
    detector = dlib.get_frontal_face_detector()
    detections = []
    for file in glob.glob(os.path.join(imagesfp, "*")):
        filename = file.split('/')[-1]
        image = dlib.load_rgb_image(file)
        imheight, imwidth, channels = image.shape
        faces = detector(image, 1)
        if not faces:
            print("Dlib was unable to detect a face in '{}'.".format(filename))
            print(
                "'{}' will not be included in the final average face.".format(
                    filename))
            os.remove(file)
        if len(faces) >= 2:
            print("Dlib detected two or more faces in '{}'.".format(filename))
            print("Copying image around each cropped face...")
            for k, d in enumerate(faces):
                img_new = image[max(0, d.top()):min(d.bottom(), imheight),
                                max(0, d.left()):min(d.right(), imwidth)]
                dlib.save_image(
                    img_new,
                    os.path.join(imagesfp, 'newerface_{}.png'.format(k)))
            os.remove(file)
        detections.append(len(faces))
    if all(d == 0 for d in detections):
        print("Dlib was unable to detect a face in any of the images!")
        sys.exit()
Ejemplo n.º 13
0
def preprocess_csv(filename, destination):
    """Extract images/annotations from the csv file.

    Before use it, you should extract images with this function and than
    you are ready to use it.
    """
    with open(filename) as csv_file:
        reader = csv.reader(csv_file, delimiter=',')
        # get headers
        next(reader)
        # get content
        for index, line in enumerate(reader):
            label = _csv_label[line[0]]
            img = _load_img(line[1])
            name = "{0}.{1}.jpg".format(index, label)
            fullname = os.path.join(destination, name)
            print("Save {0}".format(fullname))
            dlib.save_image(img, fullname)
Ejemplo n.º 14
0
def align_and_save(img,
                   save_path,
                   src_points,
                   template_path,
                   template_scale=1):
    out_size = (512, 512)
    reference = np.load(template_path) / template_scale

    ext = os.path.splitext(save_path)
    for idx, spoint in enumerate(src_points):
        tform = trans.SimilarityTransform()
        tform.estimate(spoint, reference)
        M = tform.params[0:2, :]

        crop_img = cv2.warpAffine(img, M, out_size)
        if len(src_points) > 1:
            save_path = ext[0] + '_{}'.format(idx) + ext[1]
        dlib.save_image(crop_img.astype(np.uint8), save_path)
        print('Saving image', save_path)
Ejemplo n.º 15
0
 def save_mean_std_image(self, save_path):
     _save_mean = np.uint8(self.mean_image)
     _save_std = sp.ndarray_to_image(self.std_image)
     if self.mode == 'train':
         dlib.save_image(_save_mean,
                         '{}/mean_image_train.jpg'.format(save_path))
         dlib.save_image(_save_std,
                         '{}/std_image_train.jpg'.format(save_path))
     else:
         dlib.save_image(_save_mean,
                         '{}/mean_image_test.jpg'.format(save_path))
         dlib.save_image(_save_std,
                         '{}/std_image_test.jpg'.format(save_path))
Ejemplo n.º 16
0
    def edheadify(self, image, view=False):
        """Edheadify an image
        image: Path to a portrait image.
            For best result, check out https://unsplash.com/s/photos/portrait for inspiration.
            Output image saved with suffix '_edhead.jpg'.
        view: If True, show output image.
        """
        img = self.preprocess(image)
        dets, scores, _ = self.detector.run(img, 1, -1)
        most_likely = np.argmax(scores)
        d = dets[most_likely]

        print("Left: {} Top: {} Right: {} Bottom: {}".format(
            d.left(), d.top(), d.right(), d.bottom()))

        face_center = [
            round((d.top() + d.bottom()) / 2.0),
            round((d.left() + d.right()) / 2.0)
        ]
        face_center[0] = round(face_center[0] / (1 + TOP_SHIFT))

        padded_width = round((d.right() - d.left()) * (1 + 2 * WIDTH_MARGIN))

        scale = padded_width / overlay.shape[1]
        overlay = dlib.resize_image(overlay, scale)
        height, width, _ = overlay.shape

        overlay_top_left = (max([0, face_center[0] - height // 2]),
                            face_center[1] - width // 2)

        img[overlay_top_left[0]:overlay_top_left[0] + height,
            overlay_top_left[1]:overlay_top_left[1] + width] = overlay

        if view:
            win = dlib.image_window()
            win.clear_overlay()
            win.set_image(img)
            dlib.hit_enter_to_continue()

        dlib.save_image(img, image + '_edhead.jpg')
Ejemplo n.º 17
0
    def recognize_face(self, absoluteFile: str, filename: str):
        if len(self.face_descriptors) == 0:
            return False
        image = dlib.load_rgb_image(absoluteFile)
        start_time = time.clock()
        dets_hog = self.hog_face_detector(image, 1)
        self.step_times.append("HOG + SVM Detection: %.3f s" %
                               (time.clock() - start_time))
        for i, d in enumerate(dets_hog):
            start_time = time.clock()
            shape = self.predictor(image, d)
            self.step_times.append("Landmark Detection: %.3f s" %
                                   (time.clock() - start_time))

            face_chip = dlib.get_face_chip(image, shape)
            chip_file = self.path_to_processed_file(filename, str(i))
            dlib.save_image(face_chip, chip_file)

            start_time = time.clock()
            face_descriptor = self.facerec.compute_face_descriptor(
                image, shape)
            self.step_times.append("Face Descriptor Computation:  %.3f s" %
                                   (time.clock() - start_time))

            draw_rectangle(image, d)
            face_label = "???[" + str(i) + "]"
            for label, known_descriptor in self.face_descriptors.items():
                distance = 0.0
                for i in range(len(face_descriptor)):
                    distance = distance + (
                        face_descriptor[i] - known_descriptor[i]) * (
                            face_descriptor[i] - known_descriptor[i])
                distance = math.sqrt(distance)
                if (distance < 0.6):
                    face_label = label + (" (distance=%.3f)" % distance)
                print("Distance to {} is {}".format(label, distance))
            self.labeled_chips[face_label] = chip_file
        processed_file = self.path_to_processed_file(filename,
                                                     "facerecognition")
        dlib.save_image(image, processed_file)
def preprocess_and_save_image(img, face_detector, landmark_model, output_path,
                              frame_num):
    detections = face_detector(img, 1)  # upscale by 1
    if len(detections) == 0:
        print("{}: face not detected on frame {}, skipping...".format(
            output_path.split("/")[-1], frame_num))
        # Save full image if face not detected
        outpath = join(output_path, "face_not_detected")
        os.makedirs(outpath, exist_ok=True)
        dlib.save_image(img, join(outpath, "{:04d}.png".format(frame_num)))
        return

    # Keep only one face for now
    index = 0
    # for index in range(len(detections)):
    detection = detections[index]
    landmarks = landmark_model(img, detection)
    processed_image = dlib.get_face_chip(img,
                                         landmarks,
                                         size=CROP_SIZE,
                                         padding=CROP_PADDING)
    dlib.save_image(processed_image,
                    join(output_path, "{:04d}.png".format(frame_num)))
Ejemplo n.º 19
0
    def process_img(self, imgPaths, progress_q):
        """
        :param source: source (RGB) image file to be preprocessed
        :param target: target (RGB) preprocessed image file
        """
        failed_names = []
        for imgPath in imgPaths:

            img = dlib.load_rgb_image(self.source_folder + '/' + imgPath)
            dets = self.detector(
                img, 2
            )  # Note: 2 = num jitters, higher is better at finding faces but more expensive
            try:
                shape = self.sp(
                    img, dets[0]
                )  # Note: assume only 1 face present, which is dets[0]
                face_chip = dlib.get_face_chip(img, shape)
                dlib.save_image(face_chip,
                                self.destination_folder + "/" + imgPath)
            except IndexError:
                failed_names.append(imgPath)
            progress_q.put(1)
        progress_q.put(0)
        return failed_names
Ejemplo n.º 20
0
def cluster_images(faces_folder_path,output_folder_path,output_folder_path1,predictor_path,face_rec_model_path,output_folder_path2,json_name):
	# Load all the models we need: a detector to find the faces, a shape predictor
	# to find face landmarks so we can precisely localize the face, and finally the
	# face recognition model.
	from_date = (datetime.datetime.now())
	detector = dlib.get_frontal_face_detector()
	sp = dlib.shape_predictor(predictor_path)
	facerec = dlib.face_recognition_model_v1(face_rec_model_path)
	if os.path.exists(json_name):
		img_dict = json.loads(open(json_name, "r").read())
	else:
		img_dict={}
	descriptors = []
	images = []
	image_names=[]
	# Now find all the faces and compute 128D face descriptors for each face.
	for l,f in enumerate(glob.glob(os.path.join(faces_folder_path, "*"))):
		print("Processing file: {}".format(f))
		print("[INFO] processing image {}/{}".format(l + 1,
			len(os.listdir(faces_folder_path))))
		img = dlib.load_rgb_image(f)
		img_name=f.split(os.path.sep)[-1]
		#print('img_name',img_name)		
		# Ask the detector to find the bounding boxes of each face. 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 = detector(img)
		print("Number of faces detected: {}".format(len(dets)))

		# Now process each face we found.
		for k, d in enumerate(dets):

			#d= dlib.rectangle(d.left()-500, d.top()-500,d.right()+50, d.bottom()+50)
			# Get the landmarks/parts for the face in box d.
			shape = sp(img, d)
			# Compute the 128D vector that describes the face in img identified by
			# shape.  
			face_descriptor = facerec.compute_face_descriptor(img, shape)
			descriptors.append(face_descriptor)
			images.append((img, shape))
			image_names.append(img_name)
	# Now let's cluster the faces.  
	labels = dlib.chinese_whispers_clustering(descriptors, 0.4)
	num_classes = len(set(labels))
	for j in range(0, num_classes):
		indices = []
		for i, label in enumerate(labels):
			if label == j:
				indices.append(i)
		if len(indices) >1 :
		
			# Ensure output directory exists
			if not os.path.isdir(output_folder_path+'/####'+str(j)):
				os.makedirs(output_folder_path+'/####'+str(j))

			if not os.path.isdir(output_folder_path1+'/####'+str(j)):
				os.makedirs(output_folder_path1+'/####'+str(j))
			if not os.path.isdir(output_folder_path2):
				os.makedirs(output_folder_path2)
			name_of_images=[]
			# Save the extracted faces
			for i, index in enumerate(indices):
				name_image=currentDT.strftime("%Y-%m-%d %H:%M:%S")
				img, shape = images[index]
				name_of_images.append(image_names[index])
				file_path = os.path.join(output_folder_path+'/####'+str(j),str(name_image)+str(j)+'-'+str(i))
				# The size and padding arguments are optional with default size=150x150 and padding=0.25
				dlib.save_face_chip(img, shape, file_path, size=150, padding=0.25)
				file_path1 = os.path.join(output_folder_path1+'/####'+str(j), str(name_image)+str(j)+'-'+str(i)+'.jpg')
				#image path must end with one of [.bmp, .png, .dng, .jpg, .jpeg] for dlib.save_image
				dlib.save_image(img,file_path1)
				f1 = open(output_folder_path2+'/####'+str(j)+'.txt', "a")
				f1.write(str(name_image)+str(j)+'-'+str(i)+'.jpg'+'\n')
				f1.close()
			#img_dict.update({'####'+str(j): name_of_images} )
			img_dict['####'+str(j)] = name_of_images
	f = open(json_name, "w")
	f.write(json.dumps(img_dict))
	f.close()
	to_date = (datetime.datetime.now())
	print('clustering inages time',to_date-from_date)
Ejemplo n.º 21
0
import dlib
import cv2

src_dir_path = sys.argv[1]
dest_dir_path = sys.argv[2]

file_exts = ('.jpg', '.JPG', '.jpeg', '.JPEG')
file_names = [file_name for file_name in os.listdir(src_dir_path) if file_name.endswith(file_exts)]
file_paths = [os.path.join(src_dir_path, file_name) for file_name in file_names]

n_files, n_faces = 0, 0
for file_path in file_paths:
	n_files += 1
	print(file_path + '\t' + str(n_files) + ' / ' + str(len(file_paths)))
	try:
		img = dlib.load_rgb_image(file_path)
		dets = dlib.get_frontal_face_detector()(img, 1)
		for i, det in enumerate(dets):
			try:
				cropped_img = img[det.top():det.bottom(), det.left():det.right()]
				dlib.save_image(cropped_img, dest_dir_path + str(n_faces) + '.jpg')
				n_faces += 1
			except:
				print('Invalid face!')
	except:
		print('Invalid img!')
		continue;

print('Number of faces extracted: ' + str(n_faces) + ' face!')

Ejemplo n.º 22
0
                    chip = dlib.get_face_chip(image, shape)
                    chip = cv2.medianBlur(chip, 3)
                    flip_chip = np.fliplr(chip)
                    prefix = "chip_" + label + "_"
                    number = ""
                    if (cnt < 10):
                        number = "0"
                    number = number + str(cnt)
                    filetype = f.split(".")
                    suffix = "." + filetype[len(filetype) - 1]
                    chip_name = prefix + number + "_aug"
                    aug = 0
                    for lev in brightness:
                        #dlib.save_image(chip, os.path.join(dst[i], label, chip_name + "0" + suffix ))
                        dlib.save_image(
                            _augment(chip, random.choice(contrast), lev),
                            os.path.join(dst, label,
                                         chip_name + str(aug) + suffix))
                        dlib.save_image(
                            _augment(flip_chip, random.choice(contrast), lev),
                            os.path.join(dst, label,
                                         chip_name + str(aug + 1) + suffix))
                        aug += 2

                    print("Face successfully extracted!\n")
            #new file
            cnt = cnt + 1

num_label_images.append(cnt)

filename = "info.txt"
info_path = os.path.abspath(os.path.join(os.path.dirname(__file__), filename))
Ejemplo n.º 23
0
def main(config):
    detector_ori = dlib.get_frontal_face_detector()

    # open the image file
    try:
        img = dlib.load_rgb_image(config.input_path)
    except Exception as e:
        print("While processing, " + str(e) + '\n')
        exit()

    # If the resolution is less than 128x128 then skip
    img_height = img.shape[0]
    img_width = img.shape[1]
    if img_height < 128 or img_width < 128:
        print("While processing, image size too small" + '\n')
        exit()

    # find one face that best matches and finalize the image cropping size
    max_object = None

    dets, score, idx = detector_ori.run(img, 1, -1)
    max_confi = 0.6

    if len(dets) == 0:
        print("While processing, face not detected" + '\n')
        exit()

    for i, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            i, d.left(), d.top(), d.right(), d.bottom()))
        if max_confi < score[i]:
            max_confi = score[i]
            max_object = d

    d = max_object

    if d == None:
        print("While processing, face not detected" + '\n')
        exit()
    d_width = int((d.right() - d.left() + 1) // 2)
    d_height = int((d.bottom() - d.top() + 1) // 2)

    crop_top = d.top() - d_height
    crop_bottom = d.bottom() + d_height
    crop_left = d.left() - d_width
    crop_right = d.right() + d_width

    img_out_lenght = min(crop_top, crop_left, img_height - crop_bottom,
                         img_width - crop_right)

    if img_out_lenght < -d_width / 2:
        print("While processing, face image over index" + '\n')
        exit()

    if img_out_lenght < 0:
        crop_top = crop_top - img_out_lenght
        crop_bottom = crop_bottom + img_out_lenght
        crop_left = crop_left - img_out_lenght
        crop_right = crop_right + img_out_lenght

    # Make the cropped and resized image from the original one
    img = img[crop_top:crop_bottom, crop_left:crop_right]
    if img.shape[0] != img.shape[1]:
        final_size = min(img.shape[0], img.shape[1])
        img = img[:final_size, :final_size]
    img = dlib.resize_image(img, config.image_size / img.shape[0])

    dlib.save_image(img, config.save_path)