示例#1
0
 def augments(cls,
              image_folder_path,
              dest_folder_path,
              aug_per_image,
              include_base=False):
     """
     :param include_base: Thêm ảnh ban đầu vào thư mục được aug
     :param aug_per_image: Số ảnh aug ra từ ảnh ban đầu
     :param image_folder_path: đường dẫn tới thư mục lưu các hình ban đầu
         image_folder_path:
             label_1:
                 image_1
             label_2:
                 image_2
     :param dest_folder_path: đường dẫn tới thư mục lưu các hình được aug
         des_folder_path
             label_1
                 image_1
                 augmented_image_1_1
                 augmented_image_1_2
             label_2
                 image_2
                 augmented_image_2_1
                 augmented_image_2_2
     :return: None
     """
     FileUtils.remove_dirs(dest_folder_path)
     FileUtils.make_dirs(dest_folder_path)
     dataloaders = cls.data_block.dataloaders(Path(image_folder_path),
                                              bs=cls.batch_size)
     dataloader = dataloaders.train
     vocab = dataloaders.train_ds.vocab
     for aug_index in range(aug_per_image):
         for image_batch, y_batch in dataloader:
             for index, (image, y) in enumerate(zip(image_batch, y_batch)):
                 image = cls.to_pillow_image(image)
                 label = vocab[y]
                 FileUtils.make_dirs(FileUtils.join(dest_folder_path,
                                                    label))
                 image.save(
                     FileUtils.join(
                         dest_folder_path, label,
                         f"{label}_augmented_{aug_index + 1}.jpg"))
     if include_base:
         for label_name in FileUtils.list_top_folders_names(
                 image_folder_path):
             des_label_folder_path = FileUtils.join(dest_folder_path,
                                                    label_name)
             FileUtils.make_dirs(des_label_folder_path)
             for file_name in FileUtils.list_top_files_names(
                     FileUtils.join(image_folder_path, label_name)):
                 source_file_path = FileUtils.join(image_folder_path,
                                                   label_name, file_name)
                 des_file_path = FileUtils.join(des_label_folder_path,
                                                file_name)
                 Image.open(FileUtils.join(source_file_path)).resize(
                     (cls.size, cls.size)).save(des_file_path)
示例#2
0
def extract_faces(portrait_folder_path="portrait", face_folder_path="face"):
    print("extract_faces", "start")
    FileUtils.remove_dirs(face_folder_path)
    FileUtils.make_dirs(face_folder_path)
    for label in tqdm(FileUtils.list_top_folders_names(portrait_folder_path)):
        for file_name in FileUtils.list_top_files_names(FileUtils.join(portrait_folder_path, label)):
            portrait = Image.open(FileUtils.join(portrait_folder_path, label, file_name))
            face = FaceExtracter.extract(portrait)
            if not face:
                print(f"Không tìm thấy khuôn mặt nào trong chân dung {file_name} của {label}")
                continue
            label_face_folder_path = FileUtils.join(face_folder_path, label)
            face_file_path = FileUtils.join(label_face_folder_path, file_name)
            FileUtils.make_dirs(label_face_folder_path)
            face.save(face_file_path)
    print("extract_faces", "completed")
示例#3
0
def train_classifier(data_folder_path="face_augmented"):
    print("train_classifier", "start")
    X = []
    y = []
    print("train_classifier", "embed_faces", "start")
    for label in tqdm(FileUtils.list_top_folders_names(data_folder_path)):
        faces = []
        for file_name in FileUtils.list_top_files_names(FileUtils.join(data_folder_path, label)):
            face = Image.open(FileUtils.join(data_folder_path, label, file_name))
            faces.append(face)
        embeds = ImageEmbedder.embeds(faces)
        X += [embed for embed in embeds]
        y += [label] * len(faces)
    print("train_classifier", "embed_faces", "completed")

    print("train_classifier", "fit_classifier", "start")
    FaceClassifier.fit(numpy.array(X), numpy.array(y))
    print("train_classifier", "fit_classifier", "completed")