Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,45 @@ background_image_paths = [
|
|
| 16 |
"Data/AdobeColorFunko/Outfits/GlassesDummy.png",
|
| 17 |
"Data/AdobeColorFunko/Outfits/DummyDress3.png"
|
| 18 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Function to classify beard style
|
| 20 |
class BeardClassifier:
|
| 21 |
def __init__(self, model_path, class_names):
|
|
@@ -146,6 +185,18 @@ def add_eyebrow(background_image, x_coordinate, y_coordinate, eyebrow_image_path
|
|
| 146 |
eyebrow_mask = eyebrow_image.split()[3] if eyebrow_image.mode == 'RGBA' else None
|
| 147 |
background_image.paste(eyebrow_image, region_box, mask=eyebrow_mask)
|
| 148 |
background_array = np.array(background_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
# Function to overlay a hairstyle on a background image
|
|
@@ -163,6 +214,11 @@ def process_image_menHair(background_image, x, y, placeholder_image_path, x_coor
|
|
| 163 |
|
| 164 |
# Function to generate Funko figurines
|
| 165 |
def generate_funko_figurines(input_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
# Detect and classify beard style
|
| 167 |
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 168 |
predicted_style_label = beard_classifier.classify_beard(input_image)
|
|
@@ -183,62 +239,77 @@ def generate_funko_figurines(input_image):
|
|
| 183 |
x_coordinate = 90
|
| 184 |
y_coordinate = 50
|
| 185 |
add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
|
| 186 |
-
dummy_eye(background_image, 245, 345, 'Data/AdobeColorFunko/EyezBrowz/MaleEye.png', x_coordinate, y_coordinate)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
# Convert the resulting image to base64
|
| 243 |
buffered = BytesIO()
|
| 244 |
background_image.save(buffered, format="PNG")
|
|
|
|
| 16 |
"Data/AdobeColorFunko/Outfits/GlassesDummy.png",
|
| 17 |
"Data/AdobeColorFunko/Outfits/DummyDress3.png"
|
| 18 |
]
|
| 19 |
+
|
| 20 |
+
class GenderClassifier:
|
| 21 |
+
def __init__(self, model_path, class_names):
|
| 22 |
+
self.model = models.resnet18(pretrained=False)
|
| 23 |
+
num_ftrs = self.model.fc.in_features
|
| 24 |
+
self.model.fc = nn.Linear(num_ftrs, len(class_names))
|
| 25 |
+
self.load_model(model_path)
|
| 26 |
+
self.model.eval()
|
| 27 |
+
self.data_transforms = transforms.Compose([
|
| 28 |
+
transforms.Resize((224, 224)),
|
| 29 |
+
transforms.ToTensor(),
|
| 30 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 31 |
+
])
|
| 32 |
+
self.class_names = class_names
|
| 33 |
+
|
| 34 |
+
def preprocess_image(self, image_path):
|
| 35 |
+
image = Image.open(image_path).convert("RGB")
|
| 36 |
+
image = self.data_transforms(image)
|
| 37 |
+
image = image.unsqueeze(0)
|
| 38 |
+
return image
|
| 39 |
+
|
| 40 |
+
def load_model(self, model_path):
|
| 41 |
+
if torch.cuda.is_available():
|
| 42 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 43 |
+
else:
|
| 44 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 45 |
+
|
| 46 |
+
def classify_gender(self, image_path):
|
| 47 |
+
input_image = self.preprocess_image(image_path)
|
| 48 |
+
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
predictions = self.model(input_image)
|
| 51 |
+
|
| 52 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 53 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 54 |
+
predicted_label = self.class_names[predicted_class]
|
| 55 |
+
|
| 56 |
+
return predicted_label
|
| 57 |
+
|
| 58 |
# Function to classify beard style
|
| 59 |
class BeardClassifier:
|
| 60 |
def __init__(self, model_path, class_names):
|
|
|
|
| 185 |
eyebrow_mask = eyebrow_image.split()[3] if eyebrow_image.mode == 'RGBA' else None
|
| 186 |
background_image.paste(eyebrow_image, region_box, mask=eyebrow_mask)
|
| 187 |
background_array = np.array(background_image)
|
| 188 |
+
|
| 189 |
+
def add_womenHair(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 190 |
+
placeholder_image = Image.open(placeholder_image_path)
|
| 191 |
+
target_size = (x, y)
|
| 192 |
+
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 193 |
+
placeholder_array = np.array(placeholder_image)
|
| 194 |
+
placeholder_width, placeholder_height = placeholder_image.size
|
| 195 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 196 |
+
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 197 |
+
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 198 |
+
background_array = np.array(background_image)
|
| 199 |
+
|
| 200 |
|
| 201 |
|
| 202 |
# Function to overlay a hairstyle on a background image
|
|
|
|
| 214 |
|
| 215 |
# Function to generate Funko figurines
|
| 216 |
def generate_funko_figurines(input_image):
|
| 217 |
+
# Detect and classify gender
|
| 218 |
+
gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt',
|
| 219 |
+
['Female', 'Male'])
|
| 220 |
+
predicted_gender = gender_classifier.classify_gender(input_image)
|
| 221 |
+
|
| 222 |
# Detect and classify beard style
|
| 223 |
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 224 |
predicted_style_label = beard_classifier.classify_beard(input_image)
|
|
|
|
| 239 |
x_coordinate = 90
|
| 240 |
y_coordinate = 50
|
| 241 |
add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
|
| 242 |
+
#dummy_eye(background_image, 245, 345, 'Data/AdobeColorFunko/EyezBrowz/MaleEye.png', x_coordinate, y_coordinate)
|
| 243 |
+
if predicted_gender == 'Male':
|
| 244 |
+
x = 245
|
| 245 |
+
y = 345
|
| 246 |
+
placeholder_image_path = f"Data/AdobeColorFunko/EyezBrowz/{predicted_gender}Eye.png"
|
| 247 |
+
x_coordinate = 90
|
| 248 |
+
y_coordinate = 50
|
| 249 |
+
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 250 |
+
|
| 251 |
+
if predicted_style_label == 'Bandholz':
|
| 252 |
+
process_image_Beard(background_image, 320,
|
| 253 |
+
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
| 254 |
+
50, 142)
|
| 255 |
+
|
| 256 |
+
if predicted_style_label == 'ShortBeard':
|
| 257 |
+
process_image_Beard(background_image, 300,
|
| 258 |
+
f"Data/AdobeColorFunko/Beard/ShortBeard/{predicted_color_label}.png",
|
| 259 |
+
62, 118)
|
| 260 |
+
|
| 261 |
+
if predicted_style_label == 'FullGoatee':
|
| 262 |
+
process_image_Beard(background_image, 230,
|
| 263 |
+
f"Data/AdobeColorFunko/Beard/Goatee/{predicted_color_label}.png",
|
| 264 |
+
96, 168)
|
| 265 |
+
|
| 266 |
+
if predicted_style_label == 'RapIndustryStandards':
|
| 267 |
+
process_image_Beard(background_image, 290,
|
| 268 |
+
f"Data/AdobeColorFunko/Beard/RapIndustry/{predicted_color_label}.png",
|
| 269 |
+
67, 120)
|
| 270 |
+
|
| 271 |
+
if predicted_style_label == 'Moustache':
|
| 272 |
+
process_image_Beard(background_image, 220,
|
| 273 |
+
f"Data/AdobeColorFunko/Beard/Moustache/{predicted_color_label}.png",
|
| 274 |
+
100, 160)
|
| 275 |
+
|
| 276 |
+
if predicted_style_label == 'CleanShave':
|
| 277 |
+
process_image_Beard(background_image, 220,
|
| 278 |
+
f"Data/AdobeColorFunko/Beard/CleanShave/{predicted_color_label}.png",
|
| 279 |
+
100, 160)
|
| 280 |
+
|
| 281 |
+
# Add other conditions for different beard styles
|
| 282 |
+
|
| 283 |
+
# Overlay hairstyle
|
| 284 |
+
if predicted_hairStyle_label == 'Afro':
|
| 285 |
+
process_image_menHair(background_image, 336, 420,
|
| 286 |
+
f"Data/AdobeColorFunko/MenHairstyle/Afro/{predicted_color_label}.png",
|
| 287 |
+
41, 76)
|
| 288 |
+
|
| 289 |
+
if predicted_hairStyle_label == 'Puff':
|
| 290 |
+
process_image_menHair(background_image, 320, 420,
|
| 291 |
+
f"Data/AdobeColorFunko/MenHairstyle/Puff/{predicted_color_label}.png",
|
| 292 |
+
50, 68)
|
| 293 |
+
|
| 294 |
+
if predicted_hairStyle_label == 'Spike':
|
| 295 |
+
process_image_menHair(background_image, 310, 420,
|
| 296 |
+
f"Data/AdobeColorFunko/MenHairstyle/Spike/{predicted_color_label}.png",
|
| 297 |
+
50, 70)
|
| 298 |
+
|
| 299 |
+
if predicted_hairStyle_label == 'Bald':
|
| 300 |
+
process_image_menHair(background_image, 310, 420,
|
| 301 |
+
f"Data/AdobeColorFunko/MenHairstyle/Bald/{predicted_color_label}.png",
|
| 302 |
+
67, 120)
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
if predicted_gender == 'Female':
|
| 306 |
+
x = 245
|
| 307 |
+
y = 345
|
| 308 |
+
placeholder_image_path = f"Data/AdobeColorFunko/EyezBrowz/{predicted_gender}Eye.png"
|
| 309 |
+
x_coordinate = 90
|
| 310 |
+
y_coordinate = 50
|
| 311 |
+
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 312 |
+
add_womenHair(background_image, 300, 450, "Data/AdobeColorFunko/WomenHairstyle/One.png",55,50)
|
| 313 |
# Convert the resulting image to base64
|
| 314 |
buffered = BytesIO()
|
| 315 |
background_image.save(buffered, format="PNG")
|