songhieng commited on
Commit
8a1296e
·
verified ·
1 Parent(s): 8084d12

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ import numpy as np
4
+ import imgaug.augmenters as iaa
5
+ from PIL import Image
6
+ import face_recognition
7
+ import gradio as gr
8
+
9
+ def augment_image(image, num_augmented=5):
10
+ """
11
+ Apply data augmentation to an image.
12
+
13
+ Parameters:
14
+ image (PIL.Image): The image to augment.
15
+ num_augmented (int): Number of augmented images to generate.
16
+
17
+ Returns:
18
+ List[PIL.Image]: List of augmented images.
19
+ """
20
+ image = np.array(image)
21
+
22
+ # Define a sequence of augmentation techniques
23
+ aug = iaa.Sequential([
24
+ iaa.Fliplr(0.5), # horizontal flips
25
+ iaa.Affine(rotate=(-25, 25)), # rotation
26
+ iaa.AdditiveGaussianNoise(scale=(0, 0.05*255)), # noise
27
+ iaa.Multiply((0.8, 1.2)), # brightness
28
+ iaa.GaussianBlur(sigma=(0.0, 1.0)) # blur
29
+ ])
30
+
31
+ # Generate augmented images
32
+ augmented_images = [Image.fromarray(aug(image=image)) for _ in range(num_augmented)]
33
+ return augmented_images
34
+
35
+ def preprocess_and_save_augmented_encodings(images, num_augmented=5):
36
+ known_encodings = []
37
+ known_labels = []
38
+
39
+ for image in images:
40
+ # Convert uploaded file to PIL Image
41
+ original_image = Image.open(image).convert("RGB") # Ensure the image is in RGB format
42
+
43
+ # Augment the image
44
+ augmented_images = augment_image(original_image, num_augmented=num_augmented)
45
+
46
+ # Include the original image in the list of images to encode
47
+ images_to_encode = [original_image] + augmented_images
48
+
49
+ for img in images_to_encode:
50
+ img_array = np.array(img)
51
+ # Encode the face
52
+ encodings = face_recognition.face_encodings(img_array)
53
+ if encodings: # Check if an encoding was found
54
+ encoding = encodings[0]
55
+ # Store the encoding and the corresponding label
56
+ known_encodings.append(encoding)
57
+ known_labels.append(image.name) # Use the image file name as the label
58
+
59
+ # Save encodings and labels to a file
60
+ output_file = "face_encoding.pkl"
61
+ data = {"encodings": known_encodings, "labels": known_labels}
62
+ with open(output_file, "wb") as file:
63
+ pickle.dump(data, file)
64
+
65
+ return output_file
66
+
67
+ # Gradio interface
68
+ def gradio_interface(images, num_augmented):
69
+ pkl_file = preprocess_and_save_augmented_encodings(images, num_augmented=num_augmented)
70
+ return pkl_file
71
+
72
+ # Set up Gradio interface
73
+ iface = gr.Interface(
74
+ fn=gradio_interface,
75
+ inputs=[
76
+ gr.inputs.File(type="file", label="Upload Images", multiple=True),
77
+ gr.inputs.Slider(minimum=1, maximum=10, default=5, label="Number of Augmented Images")
78
+ ],
79
+ outputs=gr.File(label="Download Encodings (.pkl)"),
80
+ title="Face Encoding with Augmentation",
81
+ description="Upload images, specify the number of augmentations, and download the resulting face encodings in a .pkl file."
82
+ )
83
+
84
+ # Launch the interface
85
+ iface.launch()