Spaces:
Runtime error

MAPALO LUKASHI commited on
Commit
4adda48
·
1 Parent(s): a386dcb

update app.py

Browse files
Files changed (1) hide show
  1. app.py +302 -0
app.py CHANGED
@@ -1,3 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastai.vision.all import *
2
  import gradio as gr
3
 
 
1
+ from fastai.vision.all import *
2
+ import gradio as gr
3
+ import dlib
4
+ import cv2
5
+ import torch
6
+ import torchvision.transforms as T
7
+ from torchvision import models
8
+ from PIL import Image
9
+ import numpy as np
10
+
11
+ # Load the pre-trained model for skin tone classification
12
+ learn = load_learner('export.pkl')
13
+
14
+ # Load models for facial landmark detection and body segmentation
15
+ detector = dlib.get_frontal_face_detector()
16
+ predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
17
+ segmentation_model = models.segmentation.deeplabv3_resnet50(pretrained=True).eval()
18
+
19
+ # Define preprocessing and postprocessing for segmentation
20
+ preprocess = T.Compose([
21
+ T.Resize(256),
22
+ T.CenterCrop(224),
23
+ T.ToTensor(),
24
+ T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
25
+ ])
26
+
27
+ # Define makeup recommendations mapping
28
+ makeup_recommendations = {
29
+ 'Porcelain': {
30
+ 'Foundation Shade': 'Porcelain',
31
+ 'Lipstick Shade': 'Pink',
32
+ 'Powder Shade': 'Ivory',
33
+ 'Brands': {
34
+ 'Foundation': 'Maybelline Fit Me - 110 Porcelain',
35
+ 'Lipstick': 'MAC Lipstick - Creme Cup',
36
+ 'Powder': 'Laura Mercier Translucent Setting Powder'
37
+ }
38
+ },
39
+ 'Ivory': {
40
+ 'Foundation Shade': 'Ivory',
41
+ 'Lipstick Shade': 'Nude Pink',
42
+ 'Powder Shade': 'Fair',
43
+ 'Brands': {
44
+ 'Foundation': 'L\'Oréal True Match - W2 Light Ivory',
45
+ 'Lipstick': 'Revlon Super Lustrous - Bare It All',
46
+ 'Powder': 'Maybelline Fit Me Matte + Poreless Powder - Ivory'
47
+ }
48
+ },
49
+ 'Light Fair': {
50
+ 'Foundation Shade': 'Light Fair',
51
+ 'Lipstick Shade': 'Coral',
52
+ 'Powder Shade': 'Natural',
53
+ 'Brands': {
54
+ 'Foundation': 'NYX Can\'t Stop Won\'t Stop - Light Fair',
55
+ 'Lipstick': 'NYX Soft Matte Lip Cream - Antwerp',
56
+ 'Powder': 'Rimmel Stay Matte Pressed Powder - Natural'
57
+ }
58
+ },
59
+ 'Fair': {
60
+ 'Foundation Shade': 'Fair',
61
+ 'Lipstick Shade': 'Peach',
62
+ 'Powder Shade': 'Fair Beige',
63
+ 'Brands': {
64
+ 'Foundation': 'Fenty Beauty Pro Filt\'r Soft Matte - 120 Fair',
65
+ 'Lipstick': 'NARS Lipstick - Niagara',
66
+ 'Powder': 'Charlotte Tilbury Airbrush Flawless Finish - Fair'
67
+ }
68
+ },
69
+ 'Beige': {
70
+ 'Foundation Shade': 'Beige',
71
+ 'Lipstick Shade': 'Rose',
72
+ 'Powder Shade': 'Medium Beige',
73
+ 'Brands': {
74
+ 'Foundation': 'Clinique Even Better - CN 28 Ivory',
75
+ 'Lipstick': 'MAC Lipstick - Twig',
76
+ 'Powder': 'MAC Studio Fix Powder Plus - C4 Medium Beige'
77
+ }
78
+ },
79
+ 'Sand': {
80
+ 'Foundation Shade': 'Sand',
81
+ 'Lipstick Shade': 'Nude',
82
+ 'Powder Shade': 'Medium',
83
+ 'Brands': {
84
+ 'Foundation': 'NARS Natural Radiant Longwear - Vallauris',
85
+ 'Lipstick': 'MAC Matte Lipstick - Honeylove',
86
+ 'Powder': 'Bobbi Brown Sheer Finish Pressed Powder - Warm Natural'
87
+ }
88
+ },
89
+ 'Medium': {
90
+ 'Foundation Shade': 'Medium',
91
+ 'Lipstick Shade': 'Berry',
92
+ 'Powder Shade': 'Tan',
93
+ 'Brands': {
94
+ 'Foundation': 'Maybelline Fit Me - 220 Natural Beige',
95
+ 'Lipstick': 'Fenty Beauty Stunna Lip Paint - Uncensored',
96
+ 'Powder': 'BareMinerals Original Loose Powder Foundation - Medium Tan'
97
+ }
98
+ },
99
+ 'Golden': {
100
+ 'Foundation Shade': 'Golden',
101
+ 'Lipstick Shade': 'Terracotta',
102
+ 'Powder Shade': 'Golden',
103
+ 'Brands': {
104
+ 'Foundation': 'Estée Lauder Double Wear - 4W1 Honey Bronze',
105
+ 'Lipstick': 'MAC Lipstick - Chili',
106
+ 'Powder': 'Sephora MicroSmooth Baked Powder Foundation - Golden'
107
+ }
108
+ },
109
+ 'Almond': {
110
+ 'Foundation Shade': 'Almond',
111
+ 'Lipstick Shade': 'Red',
112
+ 'Powder Shade': 'Golden Almond',
113
+ 'Brands': {
114
+ 'Foundation': 'Fenty Beauty Pro Filt\'r Soft Matte - 385',
115
+ 'Lipstick': 'Huda Beauty Power Bullet - El Cinco De Mayo',
116
+ 'Powder': 'Laura Mercier Loose Setting Powder - Translucent Medium Deep'
117
+ }
118
+ },
119
+ 'Honey': {
120
+ 'Foundation Shade': 'Honey',
121
+ 'Lipstick Shade': 'Plum',
122
+ 'Powder Shade': 'Warm Honey',
123
+ 'Brands': {
124
+ 'Foundation': 'NARS Sheer Glow Foundation - Syracuse',
125
+ 'Lipstick': 'NYX Soft Matte Lip Cream - Copenhagen',
126
+ 'Powder': 'Maybelline Fit Me Matte + Poreless Powder - Toffee'
127
+ }
128
+ },
129
+ 'Caramel': {
130
+ 'Foundation Shade': 'Caramel',
131
+ 'Lipstick Shade': 'Mocha',
132
+ 'Powder Shade': 'Warm Caramel',
133
+ 'Brands': {
134
+ 'Foundation': 'Bobbi Brown Skin Long-Wear Weightless Foundation - Golden Honey',
135
+ 'Lipstick': 'MAC Lipstick - Paramount',
136
+ 'Powder': 'Sephora Matte Perfection Powder Foundation - Mocha'
137
+ }
138
+ },
139
+ 'Bronze': {
140
+ 'Foundation Shade': 'Bronze',
141
+ 'Lipstick Shade': 'Brown',
142
+ 'Powder Shade': 'Bronze',
143
+ 'Brands': {
144
+ 'Foundation': 'Fenty Beauty Pro Filt\'r Soft Matte - 420',
145
+ 'Lipstick': 'Huda Beauty Power Bullet - Board Meeting',
146
+ 'Powder': 'Urban Decay Naked Skin Ultra Definition Powder Foundation - Medium Dark'
147
+ }
148
+ },
149
+ 'Sienna': {
150
+ 'Foundation Shade': 'Sienna',
151
+ 'Lipstick Shade': 'Mahogany',
152
+ 'Powder Shade': 'Deep Sienna',
153
+ 'Brands': {
154
+ 'Foundation': 'Estée Lauder Double Wear - 6W1 Sandalwood',
155
+ 'Lipstick': 'MAC Lipstick - Sin',
156
+ 'Powder': 'Laura Mercier Loose Setting Powder - Translucent Medium Deep'
157
+ }
158
+ },
159
+ 'Chestnut': {
160
+ 'Foundation Shade': 'Chestnut',
161
+ 'Lipstick Shade': 'Aubergine',
162
+ 'Powder Shade': 'Chestnut Brown',
163
+ 'Brands': {
164
+ 'Foundation': 'Lancome Teint Idole Ultra Wear - 550 Suede C',
165
+ 'Lipstick': 'Maybelline Color Sensational - Burgundy Blush',
166
+ 'Powder': 'Fenty Beauty Pro Filt\'r Soft Matte Powder Foundation - 445'
167
+ }
168
+ },
169
+ 'Chocolate': {
170
+ 'Foundation Shade': 'Chocolate',
171
+ 'Lipstick Shade': 'Burgundy',
172
+ 'Powder Shade': 'Dark Chocolate',
173
+ 'Brands': {
174
+ 'Foundation': 'MAC Studio Fix Fluid - NW58',
175
+ 'Lipstick': 'Pat McGrath MatteTrance Lipstick - McMenamy',
176
+ 'Powder': 'Fenty Beauty Pro Filt\'r Soft Matte Powder Foundation - 495'
177
+ }
178
+ },
179
+ 'Espresso': {
180
+ 'Foundation Shade': 'Espresso',
181
+ 'Lipstick Shade': 'Crimson',
182
+ 'Powder Shade': 'Espresso Brown',
183
+ 'Brands': {
184
+ 'Foundation': 'NARS Natural Radiant Longwear - New Guinea',
185
+ 'Lipstick': 'MAC Retro Matte Lipstick - Ruby Woo',
186
+ 'Powder': 'NYX Can\'t Stop Won\'t Stop Powder Foundation - Deep Espresso'
187
+ }
188
+ },
189
+ 'Coffee': {
190
+ 'Foundation Shade': 'Coffee',
191
+ 'Lipstick Shade': 'Deep Red',
192
+ 'Powder Shade': 'Coffee Brown',
193
+ 'Brands': {
194
+ 'Foundation': 'Fenty Beauty Pro Filt\'r Soft Matte - 490',
195
+ 'Lipstick': 'Huda Beauty Liquid Matte Lipstick - Famous',
196
+ 'Powder': 'Bobbi Brown Sheer Finish Loose Powder - Coffee'
197
+ }
198
+ },
199
+ 'Ebony': {
200
+ 'Foundation Shade': 'Ebony',
201
+ 'Lipstick Shade': 'Maroon',
202
+ 'Powder Shade': 'Ebony',
203
+ 'Brands': {
204
+ 'Foundation': 'Fenty Beauty Pro Filt\'r Soft Matte - 498',
205
+ 'Lipstick': 'MAC Lipstick - Diva',
206
+ 'Powder': 'Sephora MicroSmooth Baked Powder Foundation - Ebony'
207
+ }
208
+ },
209
+ 'Black': {
210
+ 'Foundation Shade': 'Black',
211
+ 'Lipstick Shade': 'Dark Plum',
212
+ 'Powder Shade': 'Deep Black',
213
+ 'Brands': {
214
+ 'Foundation': 'Black Opal True Color Pore Perfecting - Ebony Brown',
215
+ 'Lipstick': 'MAC Lipstick - Smoked Purple',
216
+ 'Powder': 'Fenty Beauty Pro Filt\'r Soft Matte Powder Foundation - 500'
217
+ }
218
+ }
219
+ }
220
+
221
+
222
+ # Function to get makeup recommendations
223
+ def get_makeup_recommendation(skin_tone):
224
+ return makeup_recommendations.get(skin_tone, {'Foundation Shade': '', 'Lipstick Shade': '', 'Powder Shade': '', 'Brands': {}})
225
+
226
+ # Function to classify skin tone and provide recommendations
227
+ def classify_and_recommend(img):
228
+ pred, _, probs = learn.predict(img)
229
+ result = {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
230
+ top_pred = pred
231
+ recommendation = get_makeup_recommendation(top_pred)
232
+ return result, recommendation
233
+
234
+ # Function to apply AR makeup
235
+ def apply_makeup(image, makeup_style=None):
236
+ image = np.array(image)
237
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
238
+ faces = detector(gray)
239
+ for face in faces:
240
+ landmarks = predictor(gray, face)
241
+ # Example: apply lipstick
242
+ for i in range(48, 61):
243
+ cv2.circle(image, (landmarks.part(i).x, landmarks.part(i).y), 1, (0, 0, 255), -1)
244
+ return Image.fromarray(image)
245
+
246
+ # Function to apply virtual fashion try-on
247
+ def try_fashion(image, attire_image):
248
+ image = np.array(image)
249
+ mask = segment_body(image)
250
+ output_image = overlay_clothing(image, attire_image, mask)
251
+ return output_image
252
+
253
+ def segment_body(image):
254
+ input_tensor = preprocess(image).unsqueeze(0)
255
+ with torch.no_grad():
256
+ output = segmentation_model(input_tensor)['out'][0]
257
+ output_predictions = output.argmax(0).byte().cpu().numpy()
258
+ return output_predictions
259
+
260
+ def overlay_clothing(image, attire, mask):
261
+ attire = attire.resize((image.shape[1], image.shape[0]))
262
+ attire = np.array(attire)
263
+ mask = (mask == 15) # Assuming label 15 is for torso
264
+ image[mask] = attire[mask]
265
+ return Image.fromarray(image)
266
+
267
+ # Main function integrating all features
268
+ def main(image, makeup_image=None, attire_image=None):
269
+ skin_tone_result, recommendation = classify_and_recommend(image)
270
+ if makeup_image:
271
+ image = apply_makeup(image, makeup_image)
272
+ if attire_image:
273
+ image = try_fashion(image, attire_image)
274
+ return image, skin_tone_result, recommendation
275
+
276
+ # Gradio interface
277
+ iface = gr.Interface(
278
+ fn=main,
279
+ inputs=[
280
+ gr.Image(type="pil", label="Upload Your Image"),
281
+ gr.Image(type="pil", optional=True, label="Upload Makeup Style"),
282
+ gr.Image(type="pil", optional=True, label="Upload Attire")
283
+ ],
284
+ outputs=[
285
+ gr.Image(type="pil", label="Output Image"),
286
+ gr.Label(label='Classification Results'),
287
+ gr.JSON(label='Makeup Recommendations')
288
+ ],
289
+ title="Skin Tone Classification, AR Makeup, and Virtual Fashion Try-On",
290
+ description="Upload an image to classify skin tone, try on AR makeup, and virtually try on fashion. You can choose to try any or all features.",
291
+ article="""
292
+ # Welcome to GLINT
293
+ Upload an image to classify your skin tone and receive personalized makeup recommendations. Additionally, try on virtual makeup and fashion!
294
+ """,
295
+ theme="default",
296
+ allow_flagging="never"
297
+ )
298
+
299
+ if __name__ == "__main__":
300
+ iface.launch()
301
+
302
+
303
  from fastai.vision.all import *
304
  import gradio as gr
305