Пример #1
0
    def __init__(self, config=None):
        self.detector = detection.FacialDetector(config)

        # self.encoder = encoding.FacialEncoder(config)
        self.encoder = encoding_arc.EncodingArc(config)

        self.matcher = default.FaceMatcher(config)
def create_faces_dataset(in_dir, out_dir=None, out_file="database.pkl"):
    # Path fixing
    if out_dir is None:
        out_dir = in_dir
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    # Processing pipeline
    config.load_config()
    config.logger_config()
    det = detection.FacialDetector()
    enc = encoding_arc.EncodingArc()

    labels = []
    embeddigs = []

    for label, file_list in files.image_folder_traversal(in_dir):
        logging.info("Adding {} to database".format(label))
        for file_name in file_list:
            image = cv2.imread(file_name)
            if image is None:
                continue
            try:
                # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                faces = det.extract_images(image)
                embedding = enc.predict(faces)
                if embedding.shape[0] > 1:
                    logging.warn(
                        "Multiple faces in image image {}".format(file_name))
                    continue

            except Exception as e:
                logging.warn("Error in image {}".format(file_name))
                logging.warn(e.message)
                # os.remove(file_name)
                # logging.warn("File {} deleted.".format(file_name))
            else:
                logging.debug("Image added: {}".format(file_name))
                labels.append(label)
                embeddigs.append(embedding[0])
    df = pd.DataFrame({"identities": labels, "embeddings": embeddigs})

    out_path = os.path.join(out_dir, out_file)
    df.to_pickle(out_path)
    logging.info("Face embeddings saved to {}".format(out_path))
Пример #3
0
def main(
    detection_method="mtcnn",
    fold_dir=fold_dir,
    image_dir=image_dir,
    out_file=out_dir,
):
    log.setLevel(logging.DEBUG)
    if not path.exists(out_dir):
        os.makedirs(out_dir)
    detector = detection.FacialDetector(detection_method)
    for im_file, out_fd in fddb_traversal(fold_dir, image_dir, out_dir):
        image = cv2.imread(im_file)
        if image is None:
            raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), im_file)

        faces, _ = detector.extract_region(image)
        out_fd.write("{}\n".format(len(faces)))

        for face in faces:
            out_fd.write("{}\n".format(repr(face)))
Пример #4
0
def create_biwi_db(out_path):
    dataset = BiwiDataset()

    # Processing pipeline
    config.load_config()
    # config.logger_config()
    detector = detection.FacialDetector()
    encoder = encoding_arc.EncodingArc()

    labels = []
    embeddings = []

    aux_info = load_identities()

    # Create faces database (MIN_ANGLES)
    for idx in range(len(aux_info)):
        label = aux_info.iden[idx]
        if label in labels:
            continue

        iden = aux_info.folder[idx]
        frame = aux_info.center_frame[idx]
        im_path, (center3D, angle) = dataset[int(iden), frame]

        image = cv2.imread(im_path)

        data = detector.predict(image, extract_image=True)
        face_match = match_detection(data, (center3D, angle))
        embedding = encoder.predict([data[face_match].image])

        labels.append(label)
        embeddings.append(embedding[0])

        if idx % 5 == 4:
            print("\rProgress: {}/{}".format(idx + 1, len(aux_info)), end="")

    df = pd.DataFrame({"identities": labels, "embeddings": embeddings})
    df.to_pickle(out_path)
    print("\rProgress: {}/{}".format(idx + 1, len(aux_info)), end="")
    # logging.info("Face embeddings saved to {}".format(out_path))
    return df
Пример #5
0
image = cv2.cvtColor(cv2.imread(IMAGE_PATH, 1), cv2.COLOR_BGR2RGB)

# RESOLUTION = (1280, 720) # 0.433
RESOLUTION = (640, 480)  # 0.213
METHOD = "mtcnn"

LOOPS = 50

image = cv2.resize(image, RESOLUTION)
# plt.imshow(image)

# In[3]:

conf = config.load_config()

detector = detection.FacialDetector(method=METHOD, conf=conf["DETECTION"])

# encoder = encoding.FacialEncoder(conf)
encoder = encoding_arc.EncodingArc(conf)

# matcher = default.FaceMatcher(conf)
# matcher = svm.SVMMatcher(conf)
# matcher = knn.KNNMatcher(conf)

# In[6]:

# for _ in range(LOOPS):
faces = detector.extract_datum(image)

# In[ ]:
def create_dataset_mtcnn(args):
    sleep(random.random())
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    # src_path, _ = os.path.split(os.path.realpath(__file__))
    # facenet.store_revision_info(src_path, output_dir, " ".join(sys.argv))
    dataset = facenet.get_dataset(args.input_dir)

    print("Loading detector")

    # Create and choose detector
    detector = detection.FacialDetector(args.method)

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(
        output_dir, "bounding_boxes_%05d.txt" % random_key)

    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir,
                                               filename + ".png")
                print(image_path)
                if not os.path.exists(output_filename):
                    # Load images
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = "{}: {}".format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write("%s\n" % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:, :, 0:3]

                        regions, raw_detection = detector.extract_region(img)

                        nrof_faces = len(regions)
                        if nrof_faces > 0:
                            img_size = np.asarray(img.shape)[0:2]
                            if (nrof_faces > 1
                                    and not args.detect_multiple_faces):
                                bounding_box_size = np.array(
                                    [r.size() for r in regions])
                                img_center = img_size / 2
                                offsets = [
                                    r.offset(img_center).flatten()
                                    for r in regions
                                ]
                                offset_dist_squared = np.sum(
                                    np.power(offsets, 2.0), 1)
                                index = np.argmax(
                                    bounding_box_size - offset_dist_squared *
                                    2.0)  # some extra weight on the centering
                                regions = [regions[index]]
                                raw_detection = [raw_detection[index]]

                            # Extract images
                            for i, (reg, raw) in enumerate(
                                    zip(regions, raw_detection)):
                                scaled = detector.extract_images(
                                    img, regions=[reg], raw_detection=[raw])[0]

                                nrof_successfully_aligned += 1
                                filename_base, file_extension = os.path.splitext(
                                    output_filename)
                                if args.detect_multiple_faces:
                                    output_filename_n = "{}_{}{}".format(
                                        filename_base, i, file_extension)
                                else:
                                    output_filename_n = "{}{}".format(
                                        filename_base, file_extension)
                                misc.imsave(output_filename_n, scaled)
                                text_file.write("%s\n" % (repr(reg)))
                        else:
                            print('Unable to align "%s"' % image_path)
                            text_file.write("%s\n" % (output_filename))

    print("Total number of images: %d" % nrof_images_total)
    print("Number of successfully aligned images: %d" %
          nrof_successfully_aligned)