Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -203,6 +203,7 @@ class BeardColorClassifier:
|
|
| 203 |
predicted_label = self.class_names[predicted_class]
|
| 204 |
return predicted_label
|
| 205 |
|
|
|
|
| 206 |
# Function to classify hairstyle
|
| 207 |
class HairStyleClassifier:
|
| 208 |
def __init__(self, model_path, class_names):
|
|
@@ -238,7 +239,43 @@ class HairStyleClassifier:
|
|
| 238 |
predicted_class = torch.argmax(probabilities).item()
|
| 239 |
predicted_label = self.class_names[predicted_class]
|
| 240 |
return predicted_label
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
def dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 243 |
placeholder_image = Image.open(placeholder_image_path)
|
| 244 |
target_size = (x, y)
|
|
@@ -325,6 +362,9 @@ def generate_funko_figurines(input_image):
|
|
| 325 |
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 326 |
predicted_hairStyle_label = hair_style_classifier.classify_hair(input_image)
|
| 327 |
|
|
|
|
|
|
|
|
|
|
| 328 |
# Process background images and apply beard style and color along with hair style and color
|
| 329 |
final_images = []
|
| 330 |
|
|
@@ -377,22 +417,22 @@ def generate_funko_figurines(input_image):
|
|
| 377 |
# Overlay hairstyle
|
| 378 |
if predicted_hairStyle_label == 'Afro':
|
| 379 |
process_image_menHair(background_image, 336, 420,
|
| 380 |
-
f"Data/AdobeColorFunko/MenHairstyle/Afro/{
|
| 381 |
41, 76)
|
| 382 |
|
| 383 |
if predicted_hairStyle_label == 'Puff':
|
| 384 |
process_image_menHair(background_image, 320, 420,
|
| 385 |
-
f"Data/AdobeColorFunko/MenHairstyle/Puff/{
|
| 386 |
50, 68)
|
| 387 |
|
| 388 |
if predicted_hairStyle_label == 'Spike':
|
| 389 |
process_image_menHair(background_image, 310, 420,
|
| 390 |
-
f"Data/AdobeColorFunko/MenHairstyle/Spike/{
|
| 391 |
50, 70)
|
| 392 |
|
| 393 |
if predicted_hairStyle_label == 'Bald':
|
| 394 |
process_image_menHair(background_image, 310, 420,
|
| 395 |
-
f"Data/AdobeColorFunko/MenHairstyle/Bald/{
|
| 396 |
67, 120)
|
| 397 |
|
| 398 |
|
|
|
|
| 203 |
predicted_label = self.class_names[predicted_class]
|
| 204 |
return predicted_label
|
| 205 |
|
| 206 |
+
|
| 207 |
# Function to classify hairstyle
|
| 208 |
class HairStyleClassifier:
|
| 209 |
def __init__(self, model_path, class_names):
|
|
|
|
| 239 |
predicted_class = torch.argmax(probabilities).item()
|
| 240 |
predicted_label = self.class_names[predicted_class]
|
| 241 |
return predicted_label
|
| 242 |
+
|
| 243 |
+
class MenHairColorClassifier:
|
| 244 |
+
def __init__(self, model_path, class_names):
|
| 245 |
+
self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
|
| 246 |
+
num_ftrs = self.model.fc.in_features
|
| 247 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
|
| 248 |
+
self.load_model(model_path)
|
| 249 |
+
self.model.eval()
|
| 250 |
+
self.data_transforms = transforms.Compose([
|
| 251 |
+
transforms.Resize((224, 224)),
|
| 252 |
+
transforms.ToTensor(),
|
| 253 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 254 |
+
])
|
| 255 |
+
self.class_names = class_names
|
| 256 |
+
|
| 257 |
+
def preprocess_image(self, image):
|
| 258 |
+
image = Image.open(image).convert("RGB")
|
| 259 |
+
image = self.data_transforms(image)
|
| 260 |
+
image = image.unsqueeze(0)
|
| 261 |
+
return image
|
| 262 |
+
|
| 263 |
+
def load_model(self, model_path):
|
| 264 |
+
if torch.cuda.is_available():
|
| 265 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 266 |
+
else:
|
| 267 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 268 |
+
|
| 269 |
+
def classify_menHair_color(self, image):
|
| 270 |
+
input_image = self.preprocess_image(image)
|
| 271 |
+
with torch.no_grad():
|
| 272 |
+
predictions = self.model(input_image)
|
| 273 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 274 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 275 |
+
predicted_label = self.class_names[predicted_class]
|
| 276 |
+
return predicted_label
|
| 277 |
+
|
| 278 |
+
|
| 279 |
def dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 280 |
placeholder_image = Image.open(placeholder_image_path)
|
| 281 |
target_size = (x, y)
|
|
|
|
| 362 |
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 363 |
predicted_hairStyle_label = hair_style_classifier.classify_hair(input_image)
|
| 364 |
|
| 365 |
+
#classify menHairColor
|
| 366 |
+
menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 367 |
+
predicted_menhairColor_label = menhair_color_classifier.classify_menHair_color(input_image)
|
| 368 |
# Process background images and apply beard style and color along with hair style and color
|
| 369 |
final_images = []
|
| 370 |
|
|
|
|
| 417 |
# Overlay hairstyle
|
| 418 |
if predicted_hairStyle_label == 'Afro':
|
| 419 |
process_image_menHair(background_image, 336, 420,
|
| 420 |
+
f"Data/AdobeColorFunko/MenHairstyle/Afro/{predicted_menhairColor_label}.png",
|
| 421 |
41, 76)
|
| 422 |
|
| 423 |
if predicted_hairStyle_label == 'Puff':
|
| 424 |
process_image_menHair(background_image, 320, 420,
|
| 425 |
+
f"Data/AdobeColorFunko/MenHairstyle/Puff/{predicted_menhairColor_label}.png",
|
| 426 |
50, 68)
|
| 427 |
|
| 428 |
if predicted_hairStyle_label == 'Spike':
|
| 429 |
process_image_menHair(background_image, 310, 420,
|
| 430 |
+
f"Data/AdobeColorFunko/MenHairstyle/Spike/{predicted_menhairColor_label}.png",
|
| 431 |
50, 70)
|
| 432 |
|
| 433 |
if predicted_hairStyle_label == 'Bald':
|
| 434 |
process_image_menHair(background_image, 310, 420,
|
| 435 |
+
f"Data/AdobeColorFunko/MenHairstyle/Bald/{predicted_menhairColor_label}.png",
|
| 436 |
67, 120)
|
| 437 |
|
| 438 |
|