|
import os
|
|
import pickle
|
|
import numpy as np
|
|
import imgaug.augmenters as iaa
|
|
from PIL import Image
|
|
import face_recognition
|
|
|
|
def augment_image(image, num_augmented=5):
|
|
"""
|
|
Apply data augmentation to an image.
|
|
|
|
Parameters:
|
|
image (PIL.Image): The image to augment.
|
|
num_augmented (int): Number of augmented images to generate.
|
|
|
|
Returns:
|
|
List[PIL.Image]: List of augmented images.
|
|
"""
|
|
image = np.array(image)
|
|
|
|
|
|
aug = iaa.Sequential([
|
|
iaa.Fliplr(0.5),
|
|
iaa.Affine(rotate=(-25, 25)),
|
|
iaa.AdditiveGaussianNoise(scale=(0, 0.05*255)),
|
|
iaa.Multiply((0.8, 1.2)),
|
|
iaa.GaussianBlur(sigma=(0.0, 1.0))
|
|
])
|
|
|
|
|
|
augmented_images = [Image.fromarray(aug(image=image)) for _ in range(num_augmented)]
|
|
return augmented_images
|
|
|
|
def preprocess_and_save_augmented_encodings(image_dir, output_file, num_augmented=5):
|
|
known_encodings = []
|
|
known_labels = []
|
|
|
|
|
|
image_paths = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
|
|
|
for image_path in image_paths:
|
|
|
|
original_image = Image.open(image_path).convert("RGB")
|
|
|
|
|
|
augmented_images = augment_image(original_image, num_augmented=num_augmented)
|
|
|
|
|
|
images_to_encode = [original_image] + augmented_images
|
|
|
|
for img in images_to_encode:
|
|
img_array = np.array(img)
|
|
|
|
encoding = face_recognition.face_encodings(img_array)[0]
|
|
|
|
|
|
known_encodings.append(encoding)
|
|
known_labels.append(image_path)
|
|
|
|
|
|
data = {"encodings": known_encodings, "labels": known_labels}
|
|
with open(output_file, "wb") as file:
|
|
pickle.dump(data, file)
|
|
|
|
|
|
image_dir = "train"
|
|
output_file = "face_encoding.pkl"
|
|
preprocess_and_save_augmented_encodings(image_dir, output_file, num_augmented=5)
|
|
|