Akash473 commited on
Commit
84afe49
·
1 Parent(s): 0bebd4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1079 -0
app.py CHANGED
@@ -0,0 +1,1079 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ import base64
3
+
4
+ import numpy as np
5
+ import torch
6
+ import torch.nn as nn
7
+ import torch.optim as optim
8
+ from torchvision import transforms, models
9
+ from PIL import Image
10
+ import gradio as gr
11
+ import cv2
12
+ import time
13
+ from collections import Counter
14
+ from functools import reduce
15
+
16
+ # Combined Code for Beard and Hairstyle Detection and Styling
17
+ frames=[]
18
+ male_background_image_paths = [
19
+ "Data/AdobeColorFunko/Outfits/MenOutfits/DummyDress1.png",
20
+ "Data/AdobeColorFunko/Outfits/MenOutfits/GlassesDummy.png",
21
+ "Data/AdobeColorFunko/Outfits/MenOutfits/DummyDress3.png"
22
+ ]
23
+
24
+ female_background_image_paths = [
25
+ "Data/AdobeColorFunko/Outfits/WomenOutfits/WomenOne.png",
26
+ "Data/AdobeColorFunko/Outfits/WomenOutfits/WomenTwo.png",
27
+ "Data/AdobeColorFunko/Outfits/WomenOutfits/WomenThree.png"
28
+ ]
29
+
30
+
31
+ class GenderClassifier:
32
+ def __init__(self, model_path, class_names):
33
+ self.model = models.resnet18(pretrained=False)
34
+ num_ftrs = self.model.fc.in_features
35
+ self.model.fc = nn.Linear(num_ftrs, len(class_names))
36
+ self.load_model(model_path)
37
+ self.model.eval()
38
+ self.data_transforms = transforms.Compose([
39
+ transforms.Resize((224, 224)),
40
+ transforms.ToTensor(),
41
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
42
+ ])
43
+ self.class_names = class_names
44
+
45
+ def preprocess_image(self, image_path):
46
+ image = Image.open(image_path).convert("RGB")
47
+ image = self.data_transforms(image)
48
+ image = image.unsqueeze(0)
49
+ return image
50
+
51
+ def load_model(self, model_path):
52
+ if torch.cuda.is_available():
53
+ self.model.load_state_dict(torch.load(model_path))
54
+ else:
55
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
56
+
57
+ def classify_gender(self, image_path):
58
+ input_image = self.preprocess_image(image_path)
59
+
60
+ with torch.no_grad():
61
+ predictions = self.model(input_image)
62
+
63
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
64
+ predicted_class = torch.argmax(probabilities).item()
65
+ predicted_label = self.class_names[predicted_class]
66
+
67
+ return predicted_label
68
+
69
+
70
+
71
+ class WomenHairStyleClassifier:
72
+ def __init__(self, model_path, class_names):
73
+ self.model = models.resnet18(pretrained=False)
74
+ num_ftrs = self.model.fc.in_features
75
+ self.model.fc = nn.Linear(num_ftrs, len(class_names))
76
+ self.load_model(model_path)
77
+ self.model.eval()
78
+ self.data_transforms = transforms.Compose([
79
+ transforms.Resize((224, 224)),
80
+ transforms.ToTensor(),
81
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
82
+ ])
83
+ self.class_names = class_names
84
+
85
+ def preprocess_image(self, image_path):
86
+ image = Image.open(image_path).convert("RGB")
87
+ image = self.data_transforms(image)
88
+ image = image.unsqueeze(0)
89
+ return image
90
+
91
+ def load_model(self, model_path):
92
+ if torch.cuda.is_available():
93
+ self.model.load_state_dict(torch.load(model_path))
94
+ else:
95
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
96
+
97
+ def classify_hairStyle(self, image_path):
98
+ input_image = self.preprocess_image(image_path)
99
+
100
+ with torch.no_grad():
101
+ predictions = self.model(input_image)
102
+
103
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
104
+ predicted_class = torch.argmax(probabilities).item()
105
+ predicted_label = self.class_names[predicted_class]
106
+
107
+ return predicted_label
108
+
109
+ class WomenHairColorClassifier:
110
+ def __init__(self, model_path, class_names):
111
+ self.model = models.resnet18(pretrained=False)
112
+ num_ftrs = self.model.fc.in_features
113
+ self.model.fc = nn.Linear(num_ftrs, len(class_names))
114
+ self.load_model(model_path)
115
+ self.model.eval()
116
+ self.data_transforms = transforms.Compose([
117
+ transforms.Resize((224, 224)),
118
+ transforms.ToTensor(),
119
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
120
+ ])
121
+ self.class_names = class_names
122
+
123
+ def preprocess_image(self, image_path):
124
+ image = Image.open(image_path).convert("RGB")
125
+ image = self.data_transforms(image)
126
+ image = image.unsqueeze(0)
127
+ return image
128
+
129
+ def load_model(self, model_path):
130
+ if torch.cuda.is_available():
131
+ self.model.load_state_dict(torch.load(model_path))
132
+ else:
133
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
134
+
135
+ def classify_hairColor(self, image_path):
136
+ input_image = self.preprocess_image(image_path)
137
+
138
+ with torch.no_grad():
139
+ predictions = self.model(input_image)
140
+
141
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
142
+ predicted_class = torch.argmax(probabilities).item()
143
+ predicted_label = self.class_names[predicted_class]
144
+
145
+ return predicted_label
146
+
147
+ # Function to classify beard style
148
+ class BeardClassifier:
149
+ def __init__(self, model_path, class_names):
150
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
151
+ num_ftrs = self.model.fc.in_features
152
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
153
+ self.load_model(model_path)
154
+ self.model.eval()
155
+ self.data_transforms = transforms.Compose([
156
+ transforms.Resize((224, 224)),
157
+ transforms.ToTensor(),
158
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
159
+ ])
160
+ self.class_names = class_names
161
+
162
+ def preprocess_image(self, image):
163
+ image = Image.open(image).convert("RGB")
164
+ image = self.data_transforms(image)
165
+ image = image.unsqueeze(0)
166
+ return image
167
+
168
+ def load_model(self, model_path):
169
+ if torch.cuda.is_available():
170
+ self.model.load_state_dict(torch.load(model_path))
171
+ else:
172
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
173
+
174
+ def classify_beard(self, image):
175
+ input_image = self.preprocess_image(image)
176
+ with torch.no_grad():
177
+ predictions = self.model(input_image)
178
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
179
+ predicted_class = torch.argmax(probabilities).item()
180
+ predicted_label = self.class_names[predicted_class]
181
+ return predicted_label
182
+
183
+
184
+ # Function to classify beard color
185
+ class BeardColorClassifier:
186
+ def __init__(self, model_path, class_names):
187
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
188
+ num_ftrs = self.model.fc.in_features
189
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
190
+ self.load_model(model_path)
191
+ self.model.eval()
192
+ self.data_transforms = transforms.Compose([
193
+ transforms.Resize((224, 224)),
194
+ transforms.ToTensor(),
195
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
196
+ ])
197
+ self.class_names = class_names
198
+
199
+ def preprocess_image(self, image):
200
+ image = Image.open(image).convert("RGB")
201
+ image = self.data_transforms(image)
202
+ image = image.unsqueeze(0)
203
+ return image
204
+
205
+ def load_model(self, model_path):
206
+ if torch.cuda.is_available():
207
+ self.model.load_state_dict(torch.load(model_path))
208
+ else:
209
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
210
+
211
+ def classify_beard_color(self, image):
212
+ input_image = self.preprocess_image(image)
213
+ with torch.no_grad():
214
+ predictions = self.model(input_image)
215
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
216
+ predicted_class = torch.argmax(probabilities).item()
217
+ predicted_label = self.class_names[predicted_class]
218
+ return predicted_label
219
+
220
+
221
+
222
+ # Function to classify hairstyle
223
+ class HairStyleClassifier:
224
+ def __init__(self, model_path, class_names):
225
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
226
+ num_ftrs = self.model.fc.in_features
227
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
228
+ self.load_model(model_path)
229
+ self.model.eval()
230
+ self.data_transforms = transforms.Compose([
231
+ transforms.Resize((224, 224)),
232
+ transforms.ToTensor(),
233
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
234
+ ])
235
+ self.class_names = class_names
236
+
237
+ def preprocess_image(self, image):
238
+ image = Image.open(image).convert("RGB")
239
+ image = self.data_transforms(image)
240
+ image = image.unsqueeze(0)
241
+ return image
242
+
243
+ def load_model(self, model_path):
244
+ if torch.cuda.is_available():
245
+ self.model.load_state_dict(torch.load(model_path))
246
+ else:
247
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
248
+
249
+ def classify_hair(self, image):
250
+ input_image = self.preprocess_image(image)
251
+ with torch.no_grad():
252
+ predictions = self.model(input_image)
253
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
254
+ predicted_class = torch.argmax(probabilities).item()
255
+ predicted_label = self.class_names[predicted_class]
256
+ return predicted_label
257
+
258
+
259
+
260
+ class MenHairColorClassifier:
261
+ def __init__(self, model_path, class_names):
262
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
263
+ num_ftrs = self.model.fc.in_features
264
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
265
+ self.load_model(model_path)
266
+ self.model.eval()
267
+ self.data_transforms = transforms.Compose([
268
+ transforms.Resize((224, 224)),
269
+ transforms.ToTensor(),
270
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
271
+ ])
272
+ self.class_names = class_names
273
+
274
+ def preprocess_image(self, image):
275
+ image = Image.open(image).convert("RGB")
276
+ image = self.data_transforms(image)
277
+ image = image.unsqueeze(0)
278
+ return image
279
+
280
+ def load_model(self, model_path):
281
+ if torch.cuda.is_available():
282
+ self.model.load_state_dict(torch.load(model_path))
283
+ else:
284
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
285
+
286
+ def classify_menHair_color(self, image):
287
+ input_image = self.preprocess_image(image)
288
+ with torch.no_grad():
289
+ predictions = self.model(input_image)
290
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
291
+ predicted_class = torch.argmax(probabilities).item()
292
+ predicted_label = self.class_names[predicted_class]
293
+ return predicted_label
294
+
295
+ class IGenderClassifier:
296
+ def __init__(self, model_path, class_names):
297
+ self.model = models.resnet18(pretrained=False)
298
+ num_ftrs = self.model.fc.in_features
299
+ self.model.fc = nn.Linear(num_ftrs, len(class_names))
300
+ self.load_model(model_path)
301
+ self.model.eval()
302
+ self.data_transforms = transforms.Compose([
303
+ transforms.Resize((224, 224)),
304
+ transforms.ToTensor(),
305
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
306
+ ])
307
+ self.class_names = class_names
308
+
309
+ def preprocess_image(self, image_path):
310
+ image = Image.open(image_path).convert("RGB")
311
+ image = self.data_transforms(image)
312
+ image = image.unsqueeze(0)
313
+ return image
314
+
315
+ def load_model(self, model_path):
316
+ if torch.cuda.is_available():
317
+ self.model.load_state_dict(torch.load(model_path))
318
+ else:
319
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
320
+
321
+ def classify_gender(self, image, image_type):
322
+ input_image = None
323
+ if image_type == True:
324
+ input_image = self.preprocess_image(image)
325
+ else:
326
+ input_image = image.unsqueeze(0)
327
+
328
+ with torch.no_grad():
329
+ predictions = self.model(input_image)
330
+
331
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
332
+ predicted_class = torch.argmax(probabilities).item()
333
+ predicted_label = self.class_names[predicted_class]
334
+
335
+ return predicted_label
336
+
337
+ class IWomenHairStyleClassifier:
338
+ def __init__(self, model_path, class_names):
339
+ self.model = models.resnet18(pretrained=False)
340
+ num_ftrs = self.model.fc.in_features
341
+ self.model.fc = nn.Linear(num_ftrs, len(class_names))
342
+ self.load_model(model_path)
343
+ self.model.eval()
344
+ self.data_transforms = transforms.Compose([
345
+ transforms.Resize((224, 224)),
346
+ transforms.ToTensor(),
347
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
348
+ ])
349
+ self.class_names = class_names
350
+
351
+ def preprocess_image(self, image_path):
352
+ image = Image.open(image_path).convert("RGB")
353
+ image = self.data_transforms(image)
354
+ image = image.unsqueeze(0)
355
+ return image
356
+
357
+ def load_model(self, model_path):
358
+ if torch.cuda.is_available():
359
+ self.model.load_state_dict(torch.load(model_path))
360
+ else:
361
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
362
+
363
+ def classify_hairStyle(self, image, image_type):
364
+
365
+ input_image = None
366
+ if image_type == True:
367
+ input_image = self.preprocess_image(image)
368
+ else:
369
+ input_image = image.unsqueeze(0)
370
+
371
+ with torch.no_grad():
372
+ predictions = self.model(input_image)
373
+
374
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
375
+ predicted_class = torch.argmax(probabilities).item()
376
+ predicted_label = self.class_names[predicted_class]
377
+ return predicted_label
378
+
379
+ class IWomenHairColorClassifier:
380
+ def __init__(self, model_path, class_names):
381
+ self.model = models.resnet18(pretrained=False)
382
+ num_ftrs = self.model.fc.in_features
383
+ self.model.fc = nn.Linear(num_ftrs, len(class_names))
384
+ self.load_model(model_path)
385
+ self.model.eval()
386
+ self.data_transforms = transforms.Compose([
387
+ transforms.Resize((224, 224)),
388
+ transforms.ToTensor(),
389
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
390
+ ])
391
+ self.class_names = class_names
392
+
393
+ def preprocess_image(self, image_path):
394
+ image = Image.open(image_path).convert("RGB")
395
+ image = self.data_transforms(image)
396
+ image = image.unsqueeze(0)
397
+ return image
398
+
399
+ def load_model(self, model_path):
400
+ if torch.cuda.is_available():
401
+ self.model.load_state_dict(torch.load(model_path))
402
+ else:
403
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
404
+
405
+ def classify_hairColor(self, image, image_type):
406
+
407
+ input_image = None
408
+ if image_type == True:
409
+ input_image = self.preprocess_image(image)
410
+ else:
411
+ input_image = image.unsqueeze(0)
412
+
413
+ with torch.no_grad():
414
+ predictions = self.model(input_image)
415
+
416
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
417
+ predicted_class = torch.argmax(probabilities).item()
418
+ predicted_label = self.class_names[predicted_class]
419
+
420
+ return predicted_label
421
+
422
+
423
+ # Function to classify beard style
424
+ class IBeardClassifier:
425
+ def __init__(self, model_path, class_names):
426
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
427
+ num_ftrs = self.model.fc.in_features
428
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
429
+ self.load_model(model_path)
430
+ self.model.eval()
431
+ self.data_transforms = transforms.Compose([
432
+ transforms.Resize((224, 224)),
433
+ transforms.ToTensor(),
434
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
435
+ ])
436
+ self.class_names = class_names
437
+
438
+ def preprocess_image(self, image):
439
+ image = Image.open(image).convert("RGB")
440
+ image = self.data_transforms(image)
441
+ image = image.unsqueeze(0)
442
+ return image
443
+
444
+ def load_model(self, model_path):
445
+ if torch.cuda.is_available():
446
+ self.model.load_state_dict(torch.load(model_path))
447
+ else:
448
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
449
+
450
+ def classify_beard(self, image, image_type):
451
+
452
+ input_image = None
453
+ if image_type == True:
454
+ input_image = self.preprocess_image(image)
455
+ else:
456
+ input_image = image.unsqueeze(0)
457
+ with torch.no_grad():
458
+ predictions = self.model(input_image)
459
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
460
+ predicted_class = torch.argmax(probabilities).item()
461
+ predicted_label = self.class_names[predicted_class]
462
+ return predicted_label
463
+
464
+
465
+ # Function to classify beard color
466
+ class IBeardColorClassifier:
467
+ def __init__(self, model_path, class_names):
468
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
469
+ num_ftrs = self.model.fc.in_features
470
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
471
+ self.load_model(model_path)
472
+ self.model.eval()
473
+ self.data_transforms = transforms.Compose([
474
+ transforms.Resize((224, 224)),
475
+ transforms.ToTensor(),
476
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
477
+ ])
478
+ self.class_names = class_names
479
+
480
+ def preprocess_image(self, image):
481
+ image = Image.open(image).convert("RGB")
482
+ image = self.data_transforms(image)
483
+ image = image.unsqueeze(0)
484
+ return image
485
+
486
+ def load_model(self, model_path):
487
+ if torch.cuda.is_available():
488
+ self.model.load_state_dict(torch.load(model_path))
489
+ else:
490
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
491
+
492
+ def classify_beard_color(self, image, image_type):
493
+ input_image = None
494
+ if image_type == True:
495
+ input_image = self.preprocess_image(image)
496
+ else:
497
+ input_image = image.unsqueeze(0)
498
+ with torch.no_grad():
499
+ predictions = self.model(input_image)
500
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
501
+ predicted_class = torch.argmax(probabilities).item()
502
+ predicted_label = self.class_names[predicted_class]
503
+ return predicted_label
504
+
505
+
506
+ # Function to classify hairstyle
507
+ class IHairStyleClassifier:
508
+ def __init__(self, model_path, class_names):
509
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
510
+ num_ftrs = self.model.fc.in_features
511
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
512
+ self.load_model(model_path)
513
+ self.model.eval()
514
+ self.data_transforms = transforms.Compose([
515
+ transforms.Resize((224, 224)),
516
+ transforms.ToTensor(),
517
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
518
+ ])
519
+ self.class_names = class_names
520
+
521
+ def preprocess_image(self, image):
522
+ image = Image.open(image).convert("RGB")
523
+ image = self.data_transforms(image)
524
+ image = image.unsqueeze(0)
525
+ return image
526
+
527
+ def load_model(self, model_path):
528
+ if torch.cuda.is_available():
529
+ self.model.load_state_dict(torch.load(model_path))
530
+ else:
531
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
532
+
533
+ def classify_hair(self, image, image_type):
534
+ input_image = None
535
+ if image_type == True:
536
+ input_image = self.preprocess_image(image)
537
+ else:
538
+ input_image = image.unsqueeze(0)
539
+ with torch.no_grad():
540
+ predictions = self.model(input_image)
541
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
542
+ predicted_class = torch.argmax(probabilities).item()
543
+ predicted_label = self.class_names[predicted_class]
544
+ return predicted_label
545
+
546
+
547
+
548
+ # Class to classify Mens haircolor
549
+ class IMenHairColorClassifier:
550
+ def __init__(self, model_path, class_names):
551
+ self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
552
+ num_ftrs = self.model.fc.in_features
553
+ self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
554
+ self.load_model(model_path)
555
+ self.model.eval()
556
+ self.data_transforms = transforms.Compose([
557
+ transforms.Resize((224, 224)),
558
+ transforms.ToTensor(),
559
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
560
+ ])
561
+ self.class_names = class_names
562
+
563
+ def preprocess_image(self, image):
564
+ image = Image.open(image).convert("RGB")
565
+ image = self.data_transforms(image)
566
+ image = image.unsqueeze(0)
567
+ return image
568
+
569
+ def load_model(self, model_path):
570
+ if torch.cuda.is_available():
571
+ self.model.load_state_dict(torch.load(model_path))
572
+ else:
573
+ self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
574
+
575
+ def classify_menHair_color(self, image, image_type):
576
+ input_image = None
577
+ if image_type == True:
578
+ input_image = self.preprocess_image(image)
579
+ else:
580
+ input_image = image.unsqueeze(0)
581
+ with torch.no_grad():
582
+ predictions = self.model(input_image)
583
+ probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
584
+ predicted_class = torch.argmax(probabilities).item()
585
+ predicted_label = self.class_names[predicted_class]
586
+ return predicted_label
587
+
588
+
589
+
590
+
591
+
592
+ def dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
593
+ placeholder_image = Image.open(placeholder_image_path)
594
+ target_size = (x, y)
595
+ placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
596
+ placeholder_array = np.array(placeholder_image)
597
+ placeholder_width, placeholder_height = placeholder_image.size
598
+ region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
599
+ placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
600
+ background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
601
+ background_array = np.array(background_image)
602
+
603
+ # Function to overlay a beard on a background image
604
+ def process_image_Beard(background_image, x, placeholder_image_path, x_coordinate, y_coordinate):
605
+ placeholder_image = Image.open(placeholder_image_path)
606
+ target_size = (x, x)
607
+ placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
608
+ placeholder_array = np.array(placeholder_image)
609
+ placeholder_width, placeholder_height = placeholder_image.size
610
+ region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
611
+ placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
612
+ background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
613
+ background_array = np.array(background_image)
614
+ placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
615
+
616
+ def process_image_WomanHair(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
617
+ placeholder_image = Image.open(placeholder_image_path)
618
+ target_size = (x, y)
619
+ placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
620
+ placeholder_array = np.array(placeholder_image)
621
+ placeholder_width, placeholder_height = placeholder_image.size
622
+ region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
623
+ placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
624
+ background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
625
+ background_array = np.array(background_image)
626
+ placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
627
+
628
+
629
+ def add_eyebrow(background_image, x_coordinate, y_coordinate, eyebrow_image_path):
630
+ eyebrow_image = Image.open(eyebrow_image_path)
631
+ target_size = (200, 200) # Adjust the size as needed
632
+ eyebrow_image = eyebrow_image.resize(target_size, Image.LANCZOS)
633
+ region_box = (x_coordinate, y_coordinate, x_coordinate + eyebrow_image.width, y_coordinate + eyebrow_image.height)
634
+ eyebrow_mask = eyebrow_image.split()[3] if eyebrow_image.mode == 'RGBA' else None
635
+ background_image.paste(eyebrow_image, region_box, mask=eyebrow_mask)
636
+ background_array = np.array(background_image)
637
+
638
+
639
+
640
+
641
+ # Function to overlay a hairstyle on a background image
642
+ def process_image_menHair(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
643
+ placeholder_image = Image.open(placeholder_image_path)
644
+ target_size = (x, y)
645
+ placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
646
+ placeholder_array = np.array(placeholder_image)
647
+ placeholder_width, placeholder_height = placeholder_image.size
648
+ region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
649
+ placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
650
+ background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
651
+ background_array = np.array(background_image)
652
+ placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
653
+
654
+ def capture_frame_from_webcam(duration=7):
655
+ data_transforms = transforms.Compose([
656
+ transforms.Resize((224, 224)),
657
+ transforms.ToTensor(),
658
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
659
+ ])
660
+
661
+ cap = cv2.VideoCapture(0) # Open the default webcam (usually ID 0)
662
+
663
+ frames = []
664
+ start_time = time.time()
665
+
666
+ while (time.time() - start_time) < duration:
667
+ ret, frame = cap.read()
668
+ if not ret:
669
+ break
670
+
671
+ # Preprocess the frame and store it in the list
672
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert to RGB format
673
+ frame_pil = Image.fromarray(frame) # Convert to PIL Image
674
+ frame_tensor = data_transforms(frame_pil) # Preprocess
675
+ frames.append(frame_tensor)
676
+
677
+ # Display the video stream to the user
678
+ cv2.imshow("Video Capture", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
679
+ cv2.waitKey(1) # Adjust the delay (milliseconds) as needed for display
680
+
681
+ cap.release()
682
+ cv2.destroyAllWindows() # Close the video stream window
683
+
684
+ return frames
685
+
686
+
687
+
688
+ # Function to generate Funko figurines
689
+ def generate_funko_figurines(input_image):
690
+ # Detect and classify gender
691
+ gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
692
+ predicted_gender = gender_classifier.classify_gender(input_image)
693
+ # Process background images and apply beard style and color along with hair style and color
694
+ final_images = []
695
+
696
+ if predicted_gender == 'Male':
697
+ background_image_paths = male_background_image_paths
698
+ if predicted_gender == 'Female':
699
+ background_image_paths = female_background_image_paths
700
+
701
+ for background_image_paths in background_image_paths:
702
+ background_image = Image.open(background_image_paths)
703
+ x_coordinate = 90
704
+ y_coordinate = 50
705
+ add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
706
+ #dummy_eye(background_image, 245, 345, 'Data/AdobeColorFunko/EyezBrowz/MaleEye.png', x_coordinate, y_coordinate)
707
+ if predicted_gender == 'Male':
708
+ x = 245
709
+ y = 345
710
+ placeholder_image_path = f"Data/AdobeColorFunko/EyezBrowz/{predicted_gender}Eye.png"
711
+ x_coordinate = 90
712
+ y_coordinate = 50
713
+ dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
714
+ # Detect and classify beard style
715
+ beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
716
+ predicted_style_label = beard_classifier.classify_beard(input_image)
717
+
718
+ # Detect and classify beard color
719
+ beard_color_classifier = BeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
720
+ predicted_color_label = beard_color_classifier.classify_beard_color(input_image)
721
+
722
+ # Classify hairstyle
723
+ hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
724
+ predicted_hairStyle_label = hair_style_classifier.classify_hair(input_image)
725
+
726
+ #classify menHairColor
727
+ menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
728
+ predicted_menhairColor_label = menhair_color_classifier.classify_menHair_color(input_image)
729
+
730
+ if predicted_style_label == 'Bandholz':
731
+ process_image_Beard(background_image, 320,
732
+ f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
733
+ 50, 142)
734
+
735
+ if predicted_style_label == 'ShortBeard':
736
+ process_image_Beard(background_image, 300,
737
+ f"Data/AdobeColorFunko/Beard/ShortBeard/{predicted_color_label}.png",
738
+ 62, 118)
739
+
740
+ if predicted_style_label == 'FullGoatee':
741
+ process_image_Beard(background_image, 230,
742
+ f"Data/AdobeColorFunko/Beard/Goatee/{predicted_color_label}.png",
743
+ 96, 168)
744
+
745
+ if predicted_style_label == 'RapIndustryStandards':
746
+ process_image_Beard(background_image, 290,
747
+ f"Data/AdobeColorFunko/Beard/RapIndustry/{predicted_color_label}.png",
748
+ 67, 120)
749
+
750
+ if predicted_style_label == 'Moustache':
751
+ process_image_Beard(background_image, 220,
752
+ f"Data/AdobeColorFunko/Beard/Moustache/{predicted_color_label}.png",
753
+ 100, 160)
754
+
755
+ if predicted_style_label == 'CleanShave':
756
+ process_image_Beard(background_image, 220,
757
+ f"Data/AdobeColorFunko/Beard/CleanShave/{predicted_color_label}.png",
758
+ 100, 160)
759
+
760
+ # Add other conditions for different beard styles
761
+
762
+ # Overlay hairstyle
763
+ if predicted_hairStyle_label == 'Afro':
764
+ process_image_menHair(background_image, 336, 420,
765
+ f"Data/AdobeColorFunko/MenHairstyle/Afro/{predicted_menhairColor_label}.png",
766
+ 41, 76)
767
+
768
+ if predicted_hairStyle_label == 'Puff':
769
+ process_image_menHair(background_image, 305, 420,
770
+ f"Data/AdobeColorFunko/MenHairstyle/Puff/{predicted_menhairColor_label}.png",
771
+ 56, 68)
772
+
773
+ if predicted_hairStyle_label == 'Spike':
774
+ process_image_menHair(background_image, 310, 420,
775
+ f"Data/AdobeColorFunko/MenHairstyle/Spike/{predicted_menhairColor_label}.png",
776
+ 52, 70)
777
+
778
+ if predicted_hairStyle_label == 'Bald':
779
+ process_image_menHair(background_image, 310, 420,
780
+ f"Data/AdobeColorFunko/MenHairstyle/Bald/{predicted_menhairColor_label}.png",
781
+ 67, 120)
782
+
783
+
784
+ if predicted_gender == 'Female':
785
+ x = 245
786
+ y = 345
787
+ placeholder_image_path = f"Data/AdobeColorFunko/EyezBrowz/{predicted_gender}Eye.png"
788
+ x_coordinate = 90
789
+ y_coordinate = 50
790
+ dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
791
+ WomenHairStyle_classifier = WomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
792
+ predicted_WomenHairStyle = WomenHairStyle_classifier.classify_hairStyle(input_image)
793
+
794
+ WomenHairColor_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
795
+ predicted_WomenHairColor = WomenHairColor_classifier.classify_hairColor(input_image)
796
+ if predicted_WomenHairStyle == 'MediumLength':
797
+ process_image_WomanHair(background_image, 300,460,
798
+ f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_WomenHairColor}.png",
799
+ 56, 50)
800
+
801
+ if predicted_WomenHairStyle == 'ShortHair':
802
+ process_image_WomanHair(background_image, 270,460,
803
+ f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{predicted_WomenHairColor}.png",
804
+ 61, 49)
805
+
806
+ if predicted_WomenHairStyle == 'SidePlait':
807
+ process_image_WomanHair(background_image, 300,450,
808
+ f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{predicted_WomenHairColor}.png",
809
+ 54, 56)
810
+
811
+
812
+ # Convert the resulting image to base64
813
+ buffered = BytesIO()
814
+ background_image.save(buffered, format="PNG")
815
+ #base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
816
+ final_images.append(background_image)
817
+
818
+ return final_images
819
+
820
+ def Igenerate_funko_figurines(image_input):
821
+
822
+ predicted_women_hairstyle = None
823
+ predicted_women_haircolor = None
824
+ predicted_gender = None
825
+ predicted_style_label = None
826
+ predicted_color_label = None
827
+ predicted_hairstyle_label = None
828
+ predicted_menhaircolor_label = None
829
+ background_image_paths = None
830
+
831
+ # Capture video from the webcam for 7 seconds
832
+ # Initialize variables to store frames and track time
833
+
834
+ print("Capturing frames")
835
+ frames = capture_frame_from_webcam(duration=7)
836
+ print("Frames captured")
837
+
838
+ # Classify women hairstyle
839
+ women_hairstyle_classifier = IWomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
840
+
841
+ # Classify women hair color
842
+ women_hair_color_classifier = IWomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
843
+
844
+ # Detect and classify gender
845
+ gender_classifier = IGenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
846
+
847
+ # Detect and classify beard style
848
+ beard_classifier = IBeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
849
+
850
+ # Detect and classify beard color
851
+ beard_color_classifier = IBeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
852
+
853
+ # Classify hairstyle
854
+ hair_style_classifier = IHairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
855
+
856
+ #classify menHairColor
857
+ menhair_color_classifier = IMenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
858
+
859
+ def predict_male_features_from_frames(frame):
860
+ return [
861
+ beard_classifier.classify_beard(image=frame,image_type=False),
862
+ beard_color_classifier.classify_beard_color(image=frame,image_type=False),
863
+ hair_style_classifier.classify_hair(image=frame,image_type=False),
864
+ menhair_color_classifier.classify_menHair_color(image=frame,image_type=False)
865
+ ]
866
+
867
+ def predict_female_features_from_frames(frame):
868
+ return [
869
+ women_hairstyle_classifier.classify_hairStyle(image=frame,image_type=False),
870
+ women_hair_color_classifier.classify_hairColor(image=frame,image_type=False),
871
+ ]
872
+
873
+ def predict_gender_from_frames(frame):
874
+ return gender_classifier.classify_gender(image=frame,image_type=False)
875
+
876
+
877
+ if image_input == True:
878
+
879
+ predicted_women_hairstyle = women_hairstyle_classifier.classify_hairStyle(image="<input as image>",image_type=image_input)
880
+ # Predict women hair color
881
+ predicted_women_haircolor = women_hair_color_classifier.classify_hairColor(image="<input as image>",image_type=image_input)
882
+ # Predict Gender
883
+ predicted_gender = gender_classifier.classify_gender(image="<input as image>",image_type=image_input)
884
+ # Predict beard style
885
+ predicted_style_label = beard_classifier.classify_beard(image="<input as image>",image_type=image_input)
886
+ # Predict beard color
887
+ predicted_color_label = beard_color_classifier.classify_beard_color(image="<input as image>",image_type=image_input)
888
+ # Classify hairstyle
889
+ predicted_hairstyle_label = hair_style_classifier.classify_hair(image="<input as image>",image_type=image_input)
890
+ #classify menHairColor
891
+ predicted_menhaircolor_label = menhair_color_classifier.classify_menHair_color(image="<input as image>",image_type=image_input)
892
+
893
+ if predicted_gender == 'Male':
894
+ background_image_paths = male_background_image_paths
895
+ if predicted_gender == 'Female':
896
+ background_image_paths = female_background_image_paths
897
+
898
+ else:
899
+ print("Predictions started")
900
+ # time counting
901
+ gp_start = time.time()
902
+ gender_predictions = map(predict_gender_from_frames, frames)
903
+ gender_counter = Counter(gender_predictions)
904
+ predicted_gender = gender_counter.most_common(1)[0][0]
905
+ # time counting
906
+ gp_end = time.time()
907
+ print(f'Predicted Gender: {predicted_gender} and it took {round(gp_end - gp_start)}s')
908
+
909
+ if predicted_gender == 'Male':
910
+ # time counting
911
+ mp_start = time.time()
912
+ background_image_paths = male_background_image_paths
913
+
914
+ facial_feature_predictions = map(predict_male_features_from_frames, frames)
915
+ beard_style_counter = Counter()
916
+ beard_color_counter = Counter()
917
+ hair_style_label_counter = Counter()
918
+ menhair_color_counter = Counter()
919
+
920
+
921
+ for pred in facial_feature_predictions:
922
+ beard_style_counter[pred[0]] += 1
923
+ beard_color_counter[pred[1]] += 1
924
+ hair_style_label_counter[pred[2]] += 1
925
+ menhair_color_counter[pred[3]] += 1
926
+
927
+ predicted_style_label = beard_style_counter.most_common(1)[0][0]
928
+ predicted_color_label = beard_color_counter.most_common(1)[0][0]
929
+ predicted_hairstyle_label = hair_style_label_counter.most_common(1)[0][0]
930
+ predicted_menhaircolor_label = menhair_color_counter.most_common(1)[0][0]
931
+ # time counting
932
+ mp_end = time.time()
933
+
934
+
935
+ print("Predictions are:\n")
936
+ print(predicted_style_label,predicted_color_label,predicted_hairstyle_label,predicted_menhaircolor_label)
937
+ print(f'\nand it took {round(mp_end - mp_start)}s')
938
+
939
+ if predicted_gender == 'Female':
940
+ background_image_paths = female_background_image_paths
941
+ women_hairstyle_counter = women_haircolor_counter = Counter()
942
+ facial_feature_predictions = map(predict_female_features_from_frames, frames)
943
+
944
+ for pred in facial_feature_predictions:
945
+ women_hairstyle_counter[pred[0]] += 1
946
+ women_haircolor_counter[pred[1]] += 1
947
+
948
+ predicted_women_hairstyle = women_hairstyle_counter.most_common(1)[0][0]
949
+ predicted_women_haircolor = women_haircolor_counter.most_common(1)[0][0]
950
+
951
+ # Process background images and apply beard style and color along with hair style and color
952
+ final_images = []
953
+
954
+
955
+ for path in background_image_paths:
956
+ background_image = Image.open(path)
957
+ x_coordinate = 90
958
+ y_coordinate = 50
959
+ add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
960
+ #dummy_eye(background_image, 245, 345, 'Data/AdobeColorFunko/EyezBrowz/MaleEye.png', x_coordinate, y_coordinate)
961
+ if predicted_gender == 'Male':
962
+ x = 245
963
+ y = 345
964
+ placeholder_image_path = f"Data/AdobeColorFunko/EyezBrowz/{predicted_gender}Eye.png"
965
+ x_coordinate = 90
966
+ y_coordinate = 50
967
+ dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
968
+
969
+ if predicted_style_label == 'Bandholz':
970
+ process_image_Beard(background_image, 320,
971
+ f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
972
+ 50, 142)
973
+
974
+ if predicted_style_label == 'ShortBeard':
975
+ process_image_Beard(background_image, 300,
976
+ f"Data/AdobeColorFunko/Beard/ShortBeard/{predicted_color_label}.png",
977
+ 62, 118)
978
+
979
+ if predicted_style_label == 'FullGoatee':
980
+ process_image_Beard(background_image, 230,
981
+ f"Data/AdobeColorFunko/Beard/Goatee/{predicted_color_label}.png",
982
+ 96, 168)
983
+
984
+ if predicted_style_label == 'RapIndustryStandards':
985
+ process_image_Beard(background_image, 290,
986
+ f"Data/AdobeColorFunko/Beard/RapIndustry/{predicted_color_label}.png",
987
+ 67, 120)
988
+
989
+ if predicted_style_label == 'Moustache':
990
+ process_image_Beard(background_image, 220,
991
+ f"Data/AdobeColorFunko/Beard/Moustache/{predicted_color_label}.png",
992
+ 100, 160)
993
+
994
+ if predicted_style_label == 'CleanShave':
995
+ process_image_Beard(background_image, 220,
996
+ f"Data/AdobeColorFunko/Beard/CleanShave/{predicted_color_label}.png",
997
+ 100, 160)
998
+
999
+ # Add other conditions for different beard styles
1000
+
1001
+ # Overlay hairstyle
1002
+ if predicted_hairstyle_label == 'Afro':
1003
+ process_image_menHair(background_image, 336, 420,
1004
+ f"Data/AdobeColorFunko/MenHairstyle/Afro/{predicted_menhaircolor_label}.png",
1005
+ 41, 76)
1006
+
1007
+ if predicted_hairstyle_label == 'Puff':
1008
+ process_image_menHair(background_image, 305, 420,
1009
+ f"Data/AdobeColorFunko/MenHairstyle/Puff/{predicted_menhaircolor_label}.png",
1010
+ 56, 68)
1011
+
1012
+ if predicted_hairstyle_label == 'Spike':
1013
+ process_image_menHair(background_image, 310, 420,
1014
+ f"Data/AdobeColorFunko/MenHairstyle/Spike/{predicted_menhaircolor_label}.png",
1015
+ 52, 70)
1016
+
1017
+ if predicted_hairstyle_label == 'Bald':
1018
+ process_image_menHair(background_image, 310, 420,
1019
+ f"Data/AdobeColorFunko/MenHairstyle/Bald/{predicted_menhaircolor_label}.png",
1020
+ 67, 120)
1021
+
1022
+
1023
+ if predicted_gender == 'Female':
1024
+ x = 245
1025
+ y = 345
1026
+ placeholder_image_path = f"Data/AdobeColorFunko/EyezBrowz/{predicted_gender}Eye.png"
1027
+ x_coordinate = 90
1028
+ y_coordinate = 50
1029
+ dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
1030
+ if predicted_women_hairstyle == 'MediumLength':
1031
+ process_image_WomanHair(background_image, 300,460,
1032
+ f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_women_haircolor}.png",
1033
+ 56, 50)
1034
+
1035
+ if predicted_women_hairstyle == 'ShortHair':
1036
+ process_image_WomanHair(background_image, 270,460,
1037
+ f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{predicted_women_haircolor}.png",
1038
+ 61, 49)
1039
+
1040
+ if predicted_women_hairstyle == 'SidePlait':
1041
+ process_image_WomanHair(background_image, 300,450,
1042
+ f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{predicted_women_haircolor}.png",
1043
+ 54, 56)
1044
+
1045
+
1046
+ # Convert the resulting image to base64
1047
+ buffered = BytesIO()
1048
+ background_image.save(buffered, format="PNG")
1049
+ #base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
1050
+ final_images.append(background_image)
1051
+
1052
+ return final_images
1053
+
1054
+
1055
+
1056
+
1057
+ with gr.Blocks() as demo:
1058
+ gr.Markdown(
1059
+ """
1060
+ # Funko POP! Figure Creation
1061
+ Enabling Streamlined Automation with Artificial Intelligence
1062
+ """)
1063
+ imageComponent = gr.Image(type="filepath").style(height=300, width=300)
1064
+ #MyOutputs=[gr.Image(type="pil", label="Generated Image " + str(i + 1)) for i in range(3)]
1065
+ with gr.Row():
1066
+ MyOutputs = [gr.Image(type="pil", label="Generated Image " + str(i + 1)).style(height=300, width=300) for i in range(3)]
1067
+ submitButton = gr.Button(value="Submit")
1068
+ submitButton.click(generate_funko_figurines, inputs=imageComponent, outputs=MyOutputs)
1069
+ #title="Funko Figurine Generator",
1070
+ #description="Generate personalized Funko figurines with different styles and backgrounds.",
1071
+ RecordButton=gr.Button(value="Generate My Custom Funko POP")
1072
+ RecordButton.click(Igenerate_funko_figurines,outputs=MyOutputs)
1073
+
1074
+
1075
+
1076
+
1077
+
1078
+ if __name__ == "__main__":
1079
+ demo.launch()