AshanGimhana commited on
Commit
e222c3f
1 Parent(s): 4de58ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +230 -0
app.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ os.system("pip install gradio==3.50")
5
+ os.system("pip install dlib==19.24.2")
6
+
7
+ #############################################
8
+
9
+ import torch
10
+ print(f"Is CUDA available: {torch.cuda.is_available()}")
11
+ # True
12
+ print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
13
+
14
+ ###################################################
15
+
16
+
17
+ from argparse import Namespace
18
+ import pprint
19
+ import numpy as np
20
+ from PIL import Image
21
+ import torch
22
+ import torchvision.transforms as transforms
23
+ import cv2
24
+ import dlib
25
+ import matplotlib.pyplot as plt
26
+ import gradio as gr # Importing Gradio as gr
27
+ from tensorflow.keras.preprocessing.image import img_to_array
28
+ from huggingface_hub import hf_hub_download, login
29
+ from datasets.augmentations import AgeTransformer
30
+ from utils.common import tensor2im
31
+ from models.psp import pSp
32
+
33
+ # Huggingface login
34
+ login(token=os.getenv("TOKENKEY"))
35
+
36
+ # Download models from Huggingface
37
+ #age_prototxt = hf_hub_download(repo_id="AshanGimhana/Age_Detection_caffe", filename="age.prototxt")
38
+ #caffe_model = hf_hub_download(repo_id="AshanGimhana/Age_Detection_caffe", filename="dex_imdb_wiki.caffemodel")
39
+ sam_ffhq_aging = hf_hub_download(repo_id="AshanGimhana/Face_Agin_model", filename="sam_ffhq_aging.pt")
40
+
41
+ # If 'mse' is a custom function needed,
42
+ #custom_objects = {'mse': MeanSquaredError()}
43
+ new_age_model = load_model("age_prediction_model.h5")
44
+
45
+ # Age prediction model setup
46
+ age_net = cv2.dnn.readNetFromCaffe(age_prototxt, caffe_model)
47
+
48
+ # Face detection and landmarks predictor setup
49
+ detector = dlib.get_frontal_face_detector()
50
+ predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
51
+
52
+ # Load the pretrained aging model
53
+ EXPERIMENT_TYPE = 'ffhq_aging'
54
+ EXPERIMENT_DATA_ARGS = {
55
+ "ffhq_aging": {
56
+ "model_path": sam_ffhq_aging,
57
+ "transform": transforms.Compose([
58
+ transforms.Resize((256, 256)),
59
+ transforms.ToTensor(),
60
+ transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
61
+ ])
62
+ }
63
+ }
64
+ EXPERIMENT_ARGS = EXPERIMENT_DATA_ARGS[EXPERIMENT_TYPE]
65
+ model_path = EXPERIMENT_ARGS['model_path']
66
+ ckpt = torch.load(model_path, map_location='cpu')
67
+ opts = ckpt['opts']
68
+ pprint.pprint(opts)
69
+ opts['checkpoint_path'] = model_path
70
+ opts = Namespace(**opts)
71
+ net = pSp(opts)
72
+ net.eval()
73
+ net.cuda()
74
+
75
+ print('Model successfully loaded!')
76
+
77
+ def check_image_quality(image):
78
+ # Convert the image to grayscale
79
+ gray_image = np.array(image.convert("L"))
80
+
81
+ # Check for under/over-exposure using histogram
82
+ hist = exposure.histogram(gray_image)
83
+ low_exposure = hist[0][:5].sum() > 0.5 * hist[0].sum() # Significant pixels in dark range
84
+ high_exposure = hist[0][-5:].sum() > 0.5 * hist[0].sum() # Significant pixels in bright range
85
+
86
+ # Check sharpness using Laplacian variance
87
+ sharpness = cv2.Laplacian(np.array(image), cv2.CV_64F).var()
88
+ low_sharpness = sharpness < 70 # Threshold for sharpness
89
+
90
+ # Check overall quality
91
+ if low_exposure or high_exposure or low_sharpness:
92
+ return False # Image quality is insufficient
93
+ return True # Image quality is sufficient
94
+
95
+ # Functions for face and mouth region
96
+ def get_face_region(image):
97
+ gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
98
+ faces = detector(gray)
99
+ if len(faces) > 0:
100
+ return faces[0]
101
+ return None
102
+
103
+ def get_mouth_region(image):
104
+ gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
105
+ faces = detector(gray)
106
+ for face in faces:
107
+ landmarks = predictor(gray, face)
108
+ mouth_points = [(landmarks.part(i).x, landmarks.part(i).y) for i in range(48, 68)]
109
+ return np.array(mouth_points, np.int32)
110
+ return None
111
+
112
+ # Function to predict age
113
+ def get_age(distr):
114
+ # Convert distribution to approximate age by scaling
115
+ age = distr * 4
116
+ return age
117
+
118
+ def predict_age(image):
119
+ image = np.array(image.resize((64, 64)))
120
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
121
+ image = image / 255.0
122
+ image = np.expand_dims(image, axis=0)
123
+
124
+ # Predict age
125
+ val = new_age_model.predict(np.array(image))
126
+ age = val[0][0]
127
+ return int(age)
128
+
129
+ # Function for color correction
130
+ def color_correct(source, target):
131
+ mean_src = np.mean(source, axis=(0, 1))
132
+ std_src = np.std(source, axis=(0, 1))
133
+ mean_tgt = np.mean(target, axis=(0, 1))
134
+ std_tgt = np.std(target, axis=(0, 1))
135
+ src_normalized = (source - mean_src) / std_src
136
+ src_corrected = (src_normalized * std_tgt) + mean_tgt
137
+ return np.clip(src_corrected, 0, 255).astype(np.uint8)
138
+
139
+ # Function to replace teeth
140
+ def replace_teeth(temp_image, aged_image):
141
+ temp_image = np.array(temp_image)
142
+ aged_image = np.array(aged_image)
143
+ temp_mouth = get_mouth_region(temp_image)
144
+ aged_mouth = get_mouth_region(aged_image)
145
+ if temp_mouth is None or aged_mouth is None:
146
+ return aged_image
147
+ temp_mask = np.zeros_like(temp_image)
148
+ cv2.fillConvexPoly(temp_mask, temp_mouth, (255, 255, 255))
149
+ temp_mouth_region = cv2.bitwise_and(temp_image, temp_mask)
150
+ temp_mouth_bbox = cv2.boundingRect(temp_mouth)
151
+ aged_mouth_bbox = cv2.boundingRect(aged_mouth)
152
+ temp_mouth_crop = temp_mouth_region[temp_mouth_bbox[1]:temp_mouth_bbox[1] + temp_mouth_bbox[3], temp_mouth_bbox[0]:temp_mouth_bbox[0] + temp_mouth_bbox[2]]
153
+ temp_mask_crop = temp_mask[temp_mouth_bbox[1]:temp_mouth_bbox[1] + temp_mouth_bbox[3], temp_mouth_bbox[0]:temp_mouth_bbox[0] + temp_mouth_bbox[2]]
154
+ temp_mouth_crop_resized = cv2.resize(temp_mouth_crop, (aged_mouth_bbox[2], aged_mouth_bbox[3]))
155
+ temp_mask_crop_resized = cv2.resize(temp_mask_crop, (aged_mouth_bbox[2], aged_mouth_bbox[3]))
156
+ aged_mouth_crop = aged_image[aged_mouth_bbox[1]:aged_mouth_bbox[1] + aged_mouth_bbox[3], aged_mouth_bbox[0]:aged_mouth_bbox[0] + aged_mouth_bbox[2]]
157
+ temp_mouth_crop_resized = color_correct(temp_mouth_crop_resized, aged_mouth_crop)
158
+ center = (aged_mouth_bbox[0] + aged_mouth_bbox[2] // 2, aged_mouth_bbox[1] + aged_mouth_bbox[3] // 2)
159
+ seamless_teeth = cv2.seamlessClone(temp_mouth_crop_resized, aged_image, temp_mask_crop_resized, center, cv2.NORMAL_CLONE)
160
+ return seamless_teeth
161
+
162
+ # Function to run alignment
163
+ def run_alignment(image):
164
+ from scripts.align_all_parallel import align_face
165
+ temp_image_path = "/tmp/temp_image.jpg"
166
+ image.save(temp_image_path)
167
+ aligned_image = align_face(filepath=temp_image_path, predictor=predictor)
168
+ return aligned_image
169
+
170
+ # Function to apply aging
171
+ def apply_aging(image, target_age):
172
+ img_transforms = EXPERIMENT_DATA_ARGS[EXPERIMENT_TYPE]['transform']
173
+ input_image = img_transforms(image)
174
+ age_transformers = [AgeTransformer(target_age=target_age)]
175
+ results = []
176
+ for age_transformer in age_transformers:
177
+ with torch.no_grad():
178
+ input_image_age = [age_transformer(input_image.cpu()).to('cuda')]
179
+ input_image_age = torch.stack(input_image_age)
180
+ result_tensor = net(input_image_age.float(), randomize_noise=False, resize=False)[0]
181
+ result_image = tensor2im(result_tensor)
182
+ results.append(np.array(result_image))
183
+ final_result = results[0]
184
+ return final_result
185
+
186
+ # Function to process the image
187
+ def process_image(uploaded_image):
188
+ # Loading images for good and bad teeth
189
+ temp_images_good = [Image.open(f"good_teeth/G{i}.JPG") for i in range(1, 4)]
190
+ temp_images_bad = [Image.open(f"bad_teeth/B{i}.jpeg") for i in range(1, 5)]
191
+
192
+ # Predicting the age
193
+ predicted_age = predict_age(uploaded_image)
194
+ target_age = predicted_age + 5
195
+
196
+ # Aligning the face in the uploaded image
197
+ aligned_image = run_alignment(uploaded_image)
198
+
199
+ # Applying aging effect
200
+ aged_image = apply_aging(aligned_image, target_age=target_age)
201
+
202
+ # Randomly selecting teeth images
203
+ good_teeth_image = temp_images_good[np.random.randint(0, len(temp_images_good))]
204
+ bad_teeth_image = temp_images_bad[np.random.randint(0, len(temp_images_bad))]
205
+
206
+ # Replacing teeth in aged image
207
+ aged_image_good_teeth = replace_teeth(good_teeth_image, aged_image)
208
+ aged_image_bad_teeth = replace_teeth(bad_teeth_image, aged_image)
209
+
210
+ return aged_image_good_teeth, aged_image_bad_teeth, predicted_age, target_age
211
+
212
+ # Gradio Interface
213
+ def show_results(uploaded_image):
214
+ # Perform quality check
215
+ if not check_image_quality(uploaded_image):
216
+ return None, None, "Not_Allowed"
217
+
218
+ # If quality is acceptable, continue with processing
219
+ aged_image_good_teeth, aged_image_bad_teeth, predicted_age, target_age = process_image(uploaded_image)
220
+ return aged_image_good_teeth, aged_image_bad_teeth, f"Predicted Age: {predicted_age}, Target Age: {target_age}"
221
+
222
+ iface = gr.Interface(
223
+ fn=show_results,
224
+ inputs=gr.Image(type="pil"),
225
+ outputs=[gr.Image(type="pil"), gr.Image(type="pil"), gr.Textbox()],
226
+ title="Aging Effect with Teeth Replacement",
227
+ description="Upload an image to apply an aging effect. The application will generate two results: one with good teeth and one with bad teeth."
228
+ )
229
+
230
+ iface.launch()