def process(input, output, frame_skip):
    detector = dlib.get_frontal_face_detector()
    sp = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    vidcap = cv.VideoCapture(input)

    success, image = vidcap.read()
    image = rotate_image(image, -90)
    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)

    count = 0
    while success:
        image_path = "%s/%04d" % (output, count)
        if count % frame_skip == 0:
            dets = detector(image)
            if (len(dets) > 0):
                d = dets[0]
                shape = sp(image, d)
            else:
                print("face not found")
            dlib.save_face_chip(image,
                                shape,
                                image_path,
                                size=FACE_SIZE,
                                padding=0.25)
        success, image = vidcap.read()
        if success:
            image = rotate_image(image, -90)
            image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        count += 1
Exemple #2
0
 def get_face_descriptor(self, file):
     img = io.imread(str(file))
     detections = self.detector(img, 1)
     if len(detections) > 0:
         d = detections[0]
         shape = self.sp(img, d)
         dlib.save_face_chip(img, shape, str(file)[:-4] + const.file_with_face_suffix)
         return self.facerec.compute_face_descriptor(img, shape)
Exemple #3
0
def face_align(src, dest):
    for label in os.listdir(src):
        label_src = os.path.join(src, label)
        label_dest = os.path.join(dest, label)
        if not os.path.isdir(label_dest):
            os.makedirs(label_dest)
        for file in os.listdir(label_src):
            in_abspath = os.path.join(label_src, file)
            img = dlib.load_rgb_image(in_abspath)
            faces = detector(img, 1)
            for i, face in enumerate(faces):
                shape = sp(img, face)
                out_abspath = os.path.join(label_dest, str(i))
                dlib.save_face_chip(img, shape, out_abspath, size=96)
Exemple #4
0
def process(left, right):
  detector = dlib.get_frontal_face_detector()
  sp = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
  left_img = cv.imread(left)
  left_img = cv.cvtColor(left_img, cv.COLOR_BGR2RGB)
  left_dets = detector(left_img)
  if(len(left_dets) > 0):
    d = left_dets[0]
    shape = sp(left_img, d)
    dlib.save_face_chip(left_img, shape, "left_dlib_chip", size=im_w, padding=0.25)
  else:
    print("left face not found")

  right_img = cv.imread(right)
  right_img = cv.cvtColor(right_img, cv.COLOR_BGR2RGB)
  right_dets = detector(right_img)
  if(len(right_dets) > 0):
    d = right_dets[0]
    shape = sp(right_img, d)
    dlib.save_face_chip(right_img, shape, "right_dlib_chip", size=im_w, padding=0.25)
  else:
    print("right face not found")

  hsv = get_flow("left_dlib_chip.jpg", "right_dlib_chip.jpg")
  img = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
  filename = "optical_flow.png"
  cv.imwrite(filename, img)

  # load YAML and create model
  yaml_file = open('model.yaml', 'r')
  loaded_model_yaml = yaml_file.read()
  yaml_file.close()
  loaded_model = model_from_yaml(loaded_model_yaml)
  # load weights into new model
  loaded_model.load_weights("model.h5")
  print("Loaded model from disk")

  X = np.array([img], dtype=float)
  X /= 255
  sample = X.reshape((-1, im_h, im_w, 3))
  classes = loaded_model.predict(sample)

  if np.argmax(classes) == 1:
    print("live")
  else:
    print("fraud")
Exemple #5
0
    def save_cluster_chips(self, root_dir=None, size=224, padding=0.25):
        if not root_dir:
            root_dir = self.images_root_dir

        cluster_root_dir = os.path.join(root_dir, "face_chip_clusters")
        self.maybe_make_dir(cluster_root_dir)

        for label_idx in list(set(self.labels)):
            label_dir = os.path.join(cluster_root_dir, str(label_idx))
            self.maybe_make_dir(label_dir)

        for i, index in enumerate(self.indices):
            image_file, shape = self.images[index]
            img = self.load_image_file(image_file)
            l = self.labels[index]
            output_folder_path = os.path.join(cluster_root_dir, str(l))
            file_path = os.path.join(output_folder_path, "face_" + str(i))
            dlib.save_face_chip(img, shape, file_path, size=224, padding=0.25)
Exemple #6
0
def landmark(filepath):

    filepath = filepath.replace("\\", "/")

    predictor_path = "samples/models/shape_predictor_5_face_landmarks.dat"
    face_rec_model_path = "samples/models/dlib_face_recognition_resnet_model_v1.dat"
    files_dir = file_dir(filepath) + '/'

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(predictor_path)
    facerec = dlib.face_recognition_model_v1(face_rec_model_path)

    print("Processing file: {}".format(filepath))
    img = io.imread(filepath)

    # 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, 1)
    print("Number of faces detected: {}".format(len(dets)))

    locations = []
    for l in dets:
        locations.append([l.left(), l.right(), l.top(), l.bottom()])
    SaveImage(filepath, locations)

    ##faces = dlib.full_object_detections()

    for k, d in enumerate(dets):
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        ##faces.append(shape)
        face_descriptor = facerec.compute_face_descriptor(img, shape)

        filenames = nsfile(1)
        dlib.save_face_chip(img, shape, files_dir + filenames[0], 150, 0.25)

        fps = []
        for p in face_descriptor:
            fps.append(p)
        SaveFace(filenames[0] + '.jpg', fps, filepath)
def do_alignment(img, bbox):
    fh, temp_file = tempfile.mkstemp('.jpg')
    os.close(fh)
    temp_file_no_ext = ".".join(temp_file.rsplit('.')[:-1])

    d = dlib.rectangle(bbox.x, bbox.y, bbox.x + bbox.w, bbox.y + bbox.h)

    # num_pts = len(source_pts)
    num_pts = 5
    try:
        landmark_predictor = landmark_predictors[str(num_pts)]
    except KeyError:
        raise Exception("Incorrect number of landmarks")

    detection_object = landmark_predictor(img, d)

    chip_size = 150
    border = 0.2
    dlib.save_face_chip(img, detection_object, temp_file_no_ext, chip_size,
                        border)

    # Playing with OpenCVs geometric transforms - they don't work out of the box
    # due to faces not being a plane.
    # sample_idx = np.random.choice(source_pts.shape[0], 4, replace=False)
    # sample_source_pts = source_pts[sample_idx, :]
    # sample_target_pts = target_pts[sample_idx, :]

    # M = cv2.getPerspectiveTransform(sample_source_pts, sample_target_pts)
    # dst_img = cv2.warpPerspective(img, M, (300, 300))

    # H = cv2.findHomography(source_pts, target_pts, cv2.CV_RANSAC)
    # dst_img = cv2.warpPerspective(img, H, (300, 300))

    aligned_img = cv2.cvtColor(ioimg.imread(temp_file), cv2.COLOR_RGB2BGR)
    os.remove(temp_file)

    ret, buf = cv2.imencode('.jpg', aligned_img)
    return buf.tobytes()
Exemple #8
0
import sys
import os
import dlib
from glob import glob
import shutil as st

predictor_path = 'shape_predictor_68_face_landmarks.dat'
face_rec_model_path = 'dlib_face_recognition_resnet_model_v1.dat'
faces_folder_path = '../UTKface_inthewild_frontalization2/'
save_folder_path = '../UTKface_inthewild_frontalization3/'

detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)

file_names = glob(faces_folder_path + '/*jpg')
for i in range(len(file_names)):
    file_name = file_names[i]
    print(file_name)
    img = dlib.load_rgb_image(file_name)
    dets = detector(img, 1)
    if len(dets) is 1:
        for k, d in enumerate(dets):
            shape = sp(img, d)

            dlib.save_face_chip(img, shape, "thumbnail", size=260, padding=0.1)
            st.copy2("thumbnail.jpg",
                     save_folder_path + file_name.split('\\')[1])
Exemple #9
0
    def clustering(self,
                   image_list=None,
                   output_folder_path=configs_clustering_output_folder,
                   shape_predictor_path=config_shape_predictor_path,
                   recognition_model_path=config_recognition_model_path):
        """
        人脸聚类函数:使用聚类分析方式进行人脸识别,可以在某一群人中认出特定的人
        首先假设最大的群集将包含照片集中的普通人照片
        然后提取人脸图像保存150x150格式的最大聚类中
        TODO: 可以保存所有大于2的聚类到文件夹中
        这里图片不能使用黑白的,否则报错:
        RuntimeError: Unsupported image type, must be RGB image.
        """
        if not os.path.isdir(output_folder_path):
            os.makedirs(output_folder_path)

        detector = dlib.get_frontal_face_detector()
        shape_predictor = dlib.shape_predictor(shape_predictor_path)
        recognition_model = dlib.face_recognition_model_v1(
            recognition_model_path)

        descriptors = []
        images = []

        # 找到所有人脸并为每个人脸计算出128维人脸描述器
        for i in image_list:
            print('正在处理图片: {}'.format(i))
            img = io.imread(i)
            dets = detector(img, 1)
            num_faces = len(dets)
            if num_faces == 0:
                print("没有找到人脸,文件路径{}".format(i))
                continue
            print('检测到的人脸数: {}'.format(num_faces))

            for k, d in enumerate(dets):
                # 得到的人脸特征点/部分在矩形框d中
                shape = shape_predictor(img, d)
                # 计算128维向量描述的人脸形状
                face_descriptor = recognition_model.compute_face_descriptor(
                    img, shape)
                descriptors.append(face_descriptor)
                images.append((img, shape))

        # 对人脸进行聚类
        labels = dlib.chinese_whispers_clustering(descriptors, 0.5)
        num_classes = len(set(labels))
        print("聚类的数量: {}".format(num_classes))
        # 找到人脸聚类最多的那个类
        biggest_class = None
        biggest_class_length = 0
        for i in range(0, num_classes):
            class_length = len([label for label in labels if label == i])
            if class_length > biggest_class_length:
                biggest_class_length = class_length
                biggest_class = i
        print("最大聚类的索引号: {}".format(biggest_class))
        print("最大聚类中存储的人脸数: {}".format(biggest_class_length))
        # 生成最大聚类生成索引
        indices = []
        for i, label in enumerate(labels):
            if label == biggest_class:
                indices.append(i)

        print("最大聚类中的图片索引:{}".format(str(indices)))
        # 确认输出字典的存在
        if not os.path.isdir(output_folder_path):
            os.makedirs(output_folder_path)
        # 保存提取出来的脸部
        print('正在保存最大s聚类到脸部文件夹{}'.format(output_folder_path))
        for i, index in enumerate(indices):
            img, shape = images[index]
            file_path = os.path.join(output_folder_path, 'face_' + str(i))
            # 大小(size)和填充(padding)参数默认设置为150x150, 0.25
            dlib.save_face_chip(img, shape, file_path, size=150, padding=0.25)
Exemple #10
0
    def custom_test(self, testing_samples_dir, age, gender):
        if not self.load_checkpoint():
            print("\tFAILED >_<!")
            exit(0)
        else:
            print("\tSUCCESS ^_^")
        predictor_path = 'shape_predictor_68_face_landmarks.dat'
        detector = dlib.get_frontal_face_detector()
        sp = dlib.shape_predictor(predictor_path)

        num_samples = int(np.sqrt(self.size_batch))
        file_names = glob(testing_samples_dir)
        for i in range(len(file_names)):
            file_name = file_names[i]
            img = dlib.load_rgb_image(file_name)
            dets = detector(img, 1)

            if len(dets) is 1:
                for k, d in enumerate(dets):
                    shape = sp(img, d)
                    dlib.save_face_chip(img,
                                        shape,
                                        "test/result/thumbnail",
                                        size=256,
                                        padding=0.1)

                sample_files = []
                for j in range(num_samples):
                    sample_files.append("test/result/thumbnail.jpg")

                sample = [
                    load_image(
                        image_path=sample_file,
                        image_size=self.size_image,
                        image_value_range=self.image_value_range,
                        is_gray=(self.num_input_channels == 1),
                    ) for sample_file in sample_files
                ]
                if self.num_input_channels == 1:
                    images = np.array(sample).astype(np.float32)[:, :, :, None]
                else:
                    images = np.array(sample).astype(np.float32)
                gender_male = np.ones(
                    shape=(num_samples, 2),
                    dtype=np.float32) * self.image_value_range[0]
                gender_female = np.ones(
                    shape=(num_samples, 2),
                    dtype=np.float32) * self.image_value_range[0]
                for i in range(gender_male.shape[0]):
                    gender_male[i, 0] = self.image_value_range[-1]
                    gender_female[i, 1] = self.image_value_range[-1]

                if 0 <= age <= 5:
                    label = 0
                elif 6 <= age <= 10:
                    label = 1
                elif 11 <= age <= 15:
                    label = 2
                elif 16 <= age <= 20:
                    label = 3
                elif 21 <= age <= 30:
                    label = 4
                elif 31 <= age <= 40:
                    label = 5
                elif 41 <= age <= 50:
                    label = 6
                elif 51 <= age <= 60:
                    label = 7
                elif 61 <= age <= 70:
                    label = 8
                else:
                    label = 9

                if gender is 0:
                    self.test(images, gender_male, 'test.png')
                else:
                    self.test(images, gender_female, 'test.png')

                img = cv2.imread(self.save_dir + '/test/test.png')
                a = 128 * label
                b = 128 * (label + 1)
                img0 = img[a:b, a:b]
                cv2.imwrite('test/result/thumbnail.png', img0)
                try:
                    output = faceSwap('test/result/thumbnail.png', file_name)
                    cv2.imwrite(
                        'test/result/' + str(age) + '_' + str(gender) + '_' +
                        file_name.split('\\')[1], output)

                    input = cv2.imread(file_name)
                    cv2.imshow('input', input)
                    cv2.imshow('output', output)
                    cv2.waitKey(0)

                    print('\n\tDone! Results are saved as %s' %
                          'test/result/' + str(age) + '_' + str(gender) + '_' +
                          file_name.split('\\')[1])
                except IndexError as e:
                    print(
                        'Result The face is not normal. Please put good quality pictures in your input.'
                    )
                os.remove('test/result/thumbnail.png')
                os.remove('test/result/thumbnail.jpg')
    if class_length > biggest_class_length:
        biggest_class_length = class_length
        biggest_class = i

print("Biggest cluster id number: {}".format(biggest_class))
print("Number of faces in biggest cluster: {}".format(biggest_class_length))

# Find the indices for the biggest class
indices = []
for i, label in enumerate(labels):
    if label == biggest_class:
        indices.append(i)

print("Indices of images in the biggest cluster: {}".format(str(indices)))

# Ensure output directory exists
if not os.path.isdir(output_folder_path):
    os.makedirs(output_folder_path)

# Save the extracted faces
print("Saving faces in largest cluster to output folder...")
for i, index in enumerate(indices):
    img, shape = images[index]
    file_path = os.path.join(output_folder_path, "face_" + 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)
    
    


Exemple #12
0
    class_length = len([label for label in labels if label == i])
    if class_length > biggest_class_length:
        biggest_class_length = class_length
        biggest_class = i

print("Biggest cluster id number: {}".format(biggest_class))
print("Number of faces in biggest cluster: {}".format(biggest_class_length))

# Find the indices for the biggest class
indices = []
for i, label in enumerate(labels):
    if label == biggest_class:
        indices.append(i)

print("Indices of images in the biggest cluster: {}".format(str(indices)))

# Ensure output directory exists
if not os.path.isdir(output_folder_path):
    os.makedirs(output_folder_path)

# Save the extracted faces
print("Saving faces in largest cluster to output folder...")
for i, index in enumerate(indices):
    img, shape = images[index]
    file_path = os.path.join(output_folder_path, "face_" + str(i))
    dlib.save_face_chip(img, shape, file_path)
    
    


    suc, frame = v_cap.read()  #reading frames
    frame_id = v_cap.get(1)  #getting frame number
    frames.append(frame)
    ts.append(count)
    count += 1
    dets = detector(
        frame, 1
    )  # 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.
    print("Number of faces detected: {}".format(len(dets)))
    # Now process each face we found.
    for k, d in enumerate(dets):
        # Get the landmarks/parts for the face in box d.
        # Compute the 128D vector that describes the face in img identified by shape.
        shape = sp(frame, d)
        face_descriptor = facerec.compute_face_descriptor(frame, shape)
        ff = np.array(face_descriptor).tolist()
        ptf.insert(ff)
        #print(type(frame))
        #print(shape)
        #print(type(shape))
        now = datetime.datetime.now()
        name = str(now.strftime("%d_%m_%Y")) + ' ' + str(
            now.strftime("%H_%M_%S"))
        #print(name)
        dlib.save_face_chip(frame,
                            shape,
                            './face/face{0}.jpg'.format(name),
                            size=150,
                            padding=0.25)
v_cap.release()
Exemple #14
0
def dlib_face_cluster(pic_path,tmp_path,files_path): 
    global detector,sp,facerec
    predictor_path = files_path + '/shape_predictor_5_face_landmarks.dat'
    face_rec_model_path = files_path + '/dlib_face_recognition_resnet_model_v1.dat'

    faces_folder_path = pic_path
    output_folder_path = tmp_path

    # 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.
    if not 'detector' in globals():
        detector = dlib.get_frontal_face_detector()
    if not 'sp' in globals():
        sp = dlib.shape_predictor(predictor_path)
    if not 'facerec' in globals():    
        facerec = dlib.face_recognition_model_v1(face_rec_model_path)

    raw_image_list = glob.glob(os.path.join(faces_folder_path, "*.jpg"))
    raw_image_list.append(raw_image_list[0])
    counter = 1
    batch_index = 1
    # Now find all the faces and compute 128D face descriptors for each face.
    while(True):
        descriptors = []
        images = []
        while(True):
        #     print("Processing file: {}".format(f))
            try:
                f = raw_image_list.pop(0)
                counter += 1              
            except:
                break
            try:
                img = io.imread(f)
            except:
                print('[Debug] : something wrong at io.imread, {0}'.format(f))
                continue

            # 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.
            try:
                dets = detector(img, 1)
            except:
                print('[Debug] : something wrong at detector, {0}'.format(f))
                continue
        #     print("Number of faces detected: {}".format(len(dets)))

            # Now process each face we found.
            for k, d in enumerate(dets):
                # 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.
                try:
                    face_descriptor = facerec.compute_face_descriptor(img, shape)
                    descriptors.append(face_descriptor)
                    images.append((img, shape))
                except:
                    print('[Debug] : something wrong at facerec.compute_face_descriptor, {0}'.format(f))
                    continue
        
        # Now let's cluster the faces.  
        labels = dlib.chinese_whispers_clustering(descriptors, 0.5)
        num_classes = len(set(labels))
        # print("Number of clusters: {}".format(num_classes))
        # Find biggest class
        biggest_class = None
        biggest_class_length = 0
        for i in range(0, num_classes):
            class_length = len([label for label in labels if label == i])
            if class_length > biggest_class_length:
                biggest_class_length = class_length
                biggest_class = i

        # print("Biggest cluster id number: {}".format(biggest_class))
        # print("Number of faces in biggest cluster: {}".format(biggest_class_length))

        # Find the indices for the biggest class
        indices = []
        for i, label in enumerate(labels):
            if label == biggest_class:
                indices.append(i)

        # print("Indices of images in the biggest cluster: {}".format(str(indices)))

        # Ensure output directory exists
        if not os.path.isdir(output_folder_path):
            os.makedirs(output_folder_path)
        
        # Save the extracted faces
        # print("Saving faces in largest cluster to output folder...")
        for i, index in enumerate(indices):
            img, shape = images[index]
            file_path = os.path.join(output_folder_path,'batch{0}_{1}'.format(batch_index,i))
            dlib.save_face_chip(img, shape, file_path)
            break
        batch_index += 1

        if len(raw_image_list) == 0:
            break   
Exemple #15
0
#labels = labels+1
#images_number = len(descriptors)
#print("Number of images : {}".format(images_number))

labels = dlib.chinese_whispers_clustering(descriptors, 0.4)
print("labels: {}".format(labels))
num_classes = len(set(labels))
print("Number of clusters: {}".format(num_classes))

noise = [i == 0 for i in list(labels)].count(True)

print("Number of noise : {}".format(noise))
num_classes = len(set(labels))
print("Number of clusters : {}".format(num_classes))
print("labels: {}".format(labels))
face_dict = {}
for i in range(num_classes):
    face_dict[i] = []

for i in range(len(labels)):
    face_dict[labels[i]].append(images[i])

for key in face_dict.keys():
    file_dir = os.path.join(output_folder, str(key))
    if not os.path.isdir(file_dir):
        os.makedirs(file_dir)

    for index, (image, shape) in enumerate(face_dict[key]):
        file_path = os.path.join(file_dir, 'face_' + str(index))
        dlib.save_face_chip(image, shape, file_path, size=150, padding=0.25)
Exemple #16
0
# It is possible to pass a list of images to the detector.
#     - like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128)
# In this case it will return a mmod_rectangless object.
# This object behaves just like a list of lists and can be iterated over.
# '''
predictor_path = "shape_predictor_68_face_landmarks.dat"
sp = dlib.shape_predictor(predictor_path)
print("Number of faces detected: {}".format(len(dets)))
counter = 0
for faces, prefix in zip(dets, f_prefix):
    img = io.imread(counter)
    for i, d in enumerate(faces):
        f_name = prefix + str(i)
        df.loc[counter] = [
            fids[counter], pids[counter], i, f_name,
            d.rect.left(),
            d.rect.top(),
            d.rect.right(),
            d.rect.bottom(), d.confidence
        ]
        print(
            "Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}"
            .format(i, d.rect.left(), d.rect.top(), d.rect.right(),
                    d.rect.bottom(), d.confidence))
        shape = sp(img, d)
        dlib.save_face_chip(img, shape, dir_det_out + f_name + ".jpg")

    counter += 1

df.to_csv("dnn_face_detections_bb_2.csv")
def process():
    mylogger = loger.getLoger("Whisper", Constants.boltpath + "logs")
    try:
        timenow = Blocker.current_milli_time()
        # Generic models
        video = DatabaseSession.session.query(Videos).first()

        # Some paths

        detector = dlib.get_frontal_face_detector()
        sp = dlib.shape_predictor(
            Constants.predictor_path)  # 128D face descriptor predictor
        facerec = dlib.face_recognition_model_v1(Constants.face_rec_model_path)
        descriptors = []
        images = []

        # Now find all the faces and compute 128D face descriptors for each face.
        for f in glob.glob(os.path.join(Constants.faces_folder_path, "*.png")):
            mylogger.info("Processing file: {}".format(f))
            img = dlib.load_rgb_image(f)

            # 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, 1)
            mylogger.info("Number of faces detected: {}".format(len(dets)))

            # Now process each face we found.
            for k, d in enumerate(dets):
                # 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))

        # Now let's cluster the faces.
        labels = dlib.chinese_whispers_clustering(descriptors,
                                                  Constants.TOLERANCE_WHISPER)
        num_classes = len(set(labels))
        mylogger.info("Number of clusters: {}".format(num_classes))
        if num_classes > 0:
            # Find the indices for the biggest class
            indices = []
            counts = 0
            DatabaseSession.session.query(Wishper).filter(
                Wishper.video == video).delete()
            DatabaseSession.session.commit()
            for i, label in enumerate(labels):
                # Ensure output directory exists
                output_folder_path_real = Constants.output_folder_path + str(
                    label)
                if not os.path.isdir(output_folder_path_real):
                    os.makedirs(output_folder_path_real)

                # Save the extracted faces
                mylogger.info("Saving faces cluster to output folder...")
                img, shape = images[i]
                file_path = os.path.join(output_folder_path_real,
                                         "face_" + str(counts))
                # The size and padding arguments are optional size=300x300 and padding=0.25
                dlib.save_face_chip(img,
                                    shape,
                                    file_path,
                                    size=300,
                                    padding=0.25)
                counts = counts + 1
                whisper = Wishper(alias=file_path,
                                  original_image=file_path,
                                  video=video,
                                  group=str(label),
                                  path=file_path + ".jpg")

                DatabaseSession.session.add(whisper)
                DatabaseSession.session.commit()

    except Exception as inst:
        mylogger.error("Python error.")
        mylogger.error(type(inst))
        mylogger.error(inst)
for path in paths:
    img = imread(path)
    dets = detector(img, 1)
    for i, d in enumerate(dets):
        shape = predictor(img, d)
        face_vector = facerec.compute_face_descriptor(img, shape)
        vectors.append(face_vector)
        images.append((img, shape))

# 聚类函数
labels = dlib.chinese_whispers_clustering(vectors, 0.5)
num_classes = len(set(labels))
print('共聚为 %d 类' % num_classes)
biggest_class = Counter(labels).most_common(1)
print(biggest_class)

output_dir = 'most_common'
if not os.path.exists(output_dir):
    os.mkdir(output_dir)
face_id = 1
for i in range(len(images)):
    if labels[i] == biggest_class[0][0]:
        img, shape = images[i]
        # 把人脸切出来
        dlib.save_face_chip(img,
                            shape,
                            output_dir + '/face_%d' % face_id,
                            size=150,
                            padding=0.25)
        face_id += 1
Exemple #19
0
def save_face_chip(option):
    dlib.save_face_chip(option['img'], option['shape'], option['file_path'], size=150, padding=0.25)
Exemple #20
0
def detect_from_webcam(save_video):
    cap = cv2.VideoCapture(0)
    width, height = query_capture(cap)
    do_loop = True

    # face detection/localization
    face_detector_dlib_hog = dlib.get_frontal_face_detector()
    face_detector_dlib_cnn = dlib.cnn_face_detection_model_v1(
        cnn_face_detector_path)
    face_detector_opencv_haarcascade = cv2.CascadeClassifier(cascade_path)

    # landmark prediction
    landmark68_predictor = dlib.shape_predictor(landmark68_predictor_path)
    landmark5_predictor = dlib.shape_predictor(landmark5_predictor_path)

    # face recognition
    facerec = dlib.face_recognition_model_v1(recognition_model_path)

    landmark_predictors = [landmark5_predictor, landmark68_predictor]
    landmark_idx = 0

    num_face_detectors = 3
    face_idx = 0

    last_identity = np.zeros((128, ))

    chip_size = 150
    border = 0.2

    if save_video:
        videowriter = cv2.VideoWriter("test.avi",
                                      cv2.VideoWriter_fourcc(*'DIV4'), 20,
                                      (width, height))
        chip_size = 300
        border = 1.0
        videowriter_aligned = cv2.VideoWriter("test_align.avi",
                                              cv2.VideoWriter_fourcc(*'DIV4'),
                                              20, (chip_size, chip_size))

    fh, temp_file = tempfile.mkstemp('.jpg')
    os.close(fh)
    temp_file_no_ext = ".".join(temp_file.rsplit('.')[:-1])

    while (do_loop):
        # Capture frame-by-frame
        ret, frame = cap.read()

        img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        dets = []
        # face detection
        if face_idx == 0:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_detector_opencv_haarcascade.detectMultiScale(
                gray,
                scaleFactor=1.05,
                minNeighbors=5,
                minSize=(100, 100),
                flags=cv2.CASCADE_SCALE_IMAGE)
            # Convert to array of dlib rectangles
            for (x, y, w, h) in faces:
                dets.append(dlib.rectangle(x, y, x + w, y + h))

        elif face_idx == 1:
            dets = face_detector_dlib_hog(img, 1)
        else:
            cnn_dets = face_detector_dlib_cnn(img, 1)
            for cnn_d in cnn_dets:
                # different return type because it includes confidence, get the rect
                d = cnn_d.rect
                h = d.top() - d.bottom()
                # cnn max margin detector seems to cut off the chin and this confuses landmark predictor,
                # expand height by 10%
                dets.append(
                    dlib.rectangle(d.left(), d.top(), d.right(),
                                   d.bottom() - int(h / 10.0)))

        print("Number of faces detected: {}".format(len(dets)))

        landmarks = [None] * len(dets)
        for i, d in enumerate(dets):
            #print("Detection {}, score: {}, face_type:{}".format(
            #    d, scores[i], idx[i]))
            #print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            #    i, d.left(), d.top(), d.right(), d.bottom()))
            landmark_predictor = landmark_predictors[landmark_idx]
            detection_object = landmark_predictor(img, d)

            # This is a hack to get the aligned face image via the dlib API
            # It writes to a file that we have to read back
            # `compute_face_descriptor` recomputes the alignment and won't accept a differently aligned face
            dlib.save_face_chip(img, detection_object, temp_file_no_ext,
                                chip_size, border)

            aligned_img = cv2.cvtColor(io.imread(temp_file), cv2.COLOR_RGB2BGR)
            if save_video:
                videowriter_aligned.write(aligned_img)
            cv2.imshow("aligned", aligned_img)

            landmarks[i] = detection_object.parts()
            face_descriptor = facerec.compute_face_descriptor(
                img, detection_object, 10)

            # TODO: this currently is just comparing to the last frame. Won't handle multiple faces.
            # To handle multiple faces we need to compare distance to all previous identities, or
            # track the bbox movement
            new_identity = np.matrix(face_descriptor)
            print("Distance to last identity %.4f" %
                  np.linalg.norm(last_identity - new_identity))
            last_identity = new_identity

        render_bounding_boxes(frame, dets)
        render_landmarks(frame, landmarks)

        cv2.imshow("Faces found", frame)

        if save_video:
            videowriter.write(frame)

        key = cv2.waitKey(1) & 0xFF

        if key != -1:
            if key == ord('q'):
                do_loop = False
            elif key == ord('l'):
                landmark_idx += 1
                landmark_idx %= len(landmark_predictors)
            elif key == ord('d'):
                face_idx += 1
                face_idx %= num_face_detectors
    os.remove(temp_file)

    cv2.destroyAllWindows()
    cap.release()
Exemple #21
0
cap.release()
# Cluster the faces.  
labels = dlib.chinese_whispers_clustering(descriptors, 0.5)
num_classes = len(set(labels)) # Total number of clusters
print("Number of clusters: {}".format(num_classes))

for i in range(0, num_classes):
    indices = []
    class_length = len([label for label in labels if label == i])
    for j, label in enumerate(labels):
        if label == i:
            indices.append(j)
    print("Indices of images in the cluster {0} : {1}".format(str(i),str(indices)))
    print("Size of cluster {0} : {1}".format(str(i),str(class_length)))
    output_folder_path = output_folder +'./'+ str(i) # Output folder for each cluster
    os.path.normpath(output_folder_path)
    os.makedirs(output_folder_path)
    
    # Save each face to the respective cluster folder
    print("Saving faces to output folder...")
    for k, index in enumerate(indices):
        img, shape = images[index]
        file_path = os.path.join(output_folder_path,"face_"+str(k)+"_"+str(i))
        dlib.save_face_chip(img, shape, file_path)
        
print("--- %s seconds ---" % (time.time() - start))
    
    


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)