Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,7 +65,22 @@ class GenderClassifier:
|
|
| 65 |
predicted_label = self.class_names[predicted_class]
|
| 66 |
|
| 67 |
return predicted_label
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
class WomenHairStyleClassifier:
|
|
@@ -105,6 +120,22 @@ class WomenHairStyleClassifier:
|
|
| 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):
|
|
@@ -144,6 +175,22 @@ class WomenHairColorClassifier:
|
|
| 144 |
|
| 145 |
return predicted_label
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Function to classify beard style
|
| 148 |
class BeardClassifier:
|
| 149 |
def __init__(self, model_path, class_names):
|
|
@@ -180,6 +227,22 @@ class BeardClassifier:
|
|
| 180 |
predicted_label = self.class_names[predicted_class]
|
| 181 |
return predicted_label
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
# Function to classify beard color
|
| 185 |
class BeardColorClassifier:
|
|
@@ -217,6 +280,22 @@ class BeardColorClassifier:
|
|
| 217 |
predicted_label = self.class_names[predicted_class]
|
| 218 |
return predicted_label
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
|
| 222 |
# Function to classify hairstyle
|
|
@@ -255,6 +334,21 @@ class HairStyleClassifier:
|
|
| 255 |
predicted_label = self.class_names[predicted_class]
|
| 256 |
return predicted_label
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
|
| 260 |
class MenHairColorClassifier:
|
|
@@ -292,299 +386,21 @@ class MenHairColorClassifier:
|
|
| 292 |
predicted_label = self.class_names[predicted_class]
|
| 293 |
return predicted_label
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 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 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 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 |
|
|
@@ -842,8 +658,8 @@ def Igenerate_funko_figurines():
|
|
| 842 |
# frames.extend(capture_frame_from_webcam())
|
| 843 |
|
| 844 |
# Detect and classify gender
|
| 845 |
-
gender_classifier =
|
| 846 |
-
predicted_gender = gender_classifier.
|
| 847 |
|
| 848 |
|
| 849 |
# Process background images and apply beard style and color along with hair style and color
|
|
@@ -869,19 +685,19 @@ def Igenerate_funko_figurines():
|
|
| 869 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 870 |
# Detect and classify beard style
|
| 871 |
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 872 |
-
predicted_style_label = beard_classifier.
|
| 873 |
|
| 874 |
# Detect and classify beard color
|
| 875 |
beard_color_classifier = BeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 876 |
-
predicted_color_label = beard_color_classifier.
|
| 877 |
|
| 878 |
# Classify hairstyle
|
| 879 |
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 880 |
-
predicted_hairStyle_label = hair_style_classifier.
|
| 881 |
|
| 882 |
#classify menHairColor
|
| 883 |
menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 884 |
-
predicted_menhairColor_label = menhair_color_classifier.
|
| 885 |
if predicted_style_label == 'Bandholz':
|
| 886 |
process_image_Beard(background_image, 320,
|
| 887 |
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
|
@@ -944,10 +760,10 @@ def Igenerate_funko_figurines():
|
|
| 944 |
y_coordinate = 50
|
| 945 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 946 |
WomenHairStyle_classifier = WomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
|
| 947 |
-
predicted_WomenHairStyle = WomenHairStyle_classifier.
|
| 948 |
|
| 949 |
WomenHairColor_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 950 |
-
predicted_WomenHairColor = WomenHairColor_classifier.
|
| 951 |
if predicted_WomenHairStyle == 'MediumLength':
|
| 952 |
process_image_WomanHair(background_image, 300,460,
|
| 953 |
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_WomenHairColor}.png",
|
|
|
|
| 65 |
predicted_label = self.class_names[predicted_class]
|
| 66 |
|
| 67 |
return predicted_label
|
| 68 |
+
|
| 69 |
+
def classify_from_frames(self, frames):
|
| 70 |
+
predictions = []
|
| 71 |
+
|
| 72 |
+
for frame in frames:
|
| 73 |
+
with torch.no_grad():
|
| 74 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 75 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 76 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 77 |
+
predicted_label = self.class_names[predicted_class]
|
| 78 |
+
predictions.append(predicted_label)
|
| 79 |
+
|
| 80 |
+
# Return a single prediction for the entire video
|
| 81 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 82 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 83 |
+
return final_prediction
|
| 84 |
|
| 85 |
|
| 86 |
class WomenHairStyleClassifier:
|
|
|
|
| 120 |
predicted_label = self.class_names[predicted_class]
|
| 121 |
|
| 122 |
return predicted_label
|
| 123 |
+
def classify_from_frames(self, frames):
|
| 124 |
+
predictions = []
|
| 125 |
+
|
| 126 |
+
for frame in frames:
|
| 127 |
+
with torch.no_grad():
|
| 128 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 129 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 130 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 131 |
+
predicted_label = self.class_names[predicted_class]
|
| 132 |
+
predictions.append(predicted_label)
|
| 133 |
+
|
| 134 |
+
# Return a single prediction for the entire video
|
| 135 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 136 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 137 |
+
return final_prediction
|
| 138 |
+
|
| 139 |
|
| 140 |
class WomenHairColorClassifier:
|
| 141 |
def __init__(self, model_path, class_names):
|
|
|
|
| 175 |
|
| 176 |
return predicted_label
|
| 177 |
|
| 178 |
+
def classify_from_frames(self, frames):
|
| 179 |
+
predictions = []
|
| 180 |
+
|
| 181 |
+
for frame in frames:
|
| 182 |
+
with torch.no_grad():
|
| 183 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 184 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 185 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 186 |
+
predicted_label = self.class_names[predicted_class]
|
| 187 |
+
predictions.append(predicted_label)
|
| 188 |
+
|
| 189 |
+
# Return a single prediction for the entire video
|
| 190 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 191 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 192 |
+
return final_prediction
|
| 193 |
+
|
| 194 |
# Function to classify beard style
|
| 195 |
class BeardClassifier:
|
| 196 |
def __init__(self, model_path, class_names):
|
|
|
|
| 227 |
predicted_label = self.class_names[predicted_class]
|
| 228 |
return predicted_label
|
| 229 |
|
| 230 |
+
def classify_from_frames(self, frames):
|
| 231 |
+
predictions = []
|
| 232 |
+
|
| 233 |
+
for frame in frames:
|
| 234 |
+
with torch.no_grad():
|
| 235 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 236 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 237 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 238 |
+
predicted_label = self.class_names[predicted_class]
|
| 239 |
+
predictions.append(predicted_label)
|
| 240 |
+
|
| 241 |
+
# Return a single prediction for the entire video
|
| 242 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 243 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 244 |
+
return final_prediction
|
| 245 |
+
|
| 246 |
|
| 247 |
# Function to classify beard color
|
| 248 |
class BeardColorClassifier:
|
|
|
|
| 280 |
predicted_label = self.class_names[predicted_class]
|
| 281 |
return predicted_label
|
| 282 |
|
| 283 |
+
def classify_from_frames(self, frames):
|
| 284 |
+
predictions = []
|
| 285 |
+
|
| 286 |
+
for frame in frames:
|
| 287 |
+
with torch.no_grad():
|
| 288 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 289 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 290 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 291 |
+
predicted_label = self.class_names[predicted_class]
|
| 292 |
+
predictions.append(predicted_label)
|
| 293 |
+
|
| 294 |
+
# Return a single prediction for the entire video
|
| 295 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 296 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 297 |
+
return final_prediction
|
| 298 |
+
|
| 299 |
|
| 300 |
|
| 301 |
# Function to classify hairstyle
|
|
|
|
| 334 |
predicted_label = self.class_names[predicted_class]
|
| 335 |
return predicted_label
|
| 336 |
|
| 337 |
+
def classify_from_frames(self, frames):
|
| 338 |
+
predictions = []
|
| 339 |
+
|
| 340 |
+
for frame in frames:
|
| 341 |
+
with torch.no_grad():
|
| 342 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 343 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 344 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 345 |
+
predicted_label = self.class_names[predicted_class]
|
| 346 |
+
predictions.append(predicted_label)
|
| 347 |
+
|
| 348 |
+
# Return a single prediction for the entire video
|
| 349 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 350 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 351 |
+
return final_prediction
|
| 352 |
|
| 353 |
|
| 354 |
class MenHairColorClassifier:
|
|
|
|
| 386 |
predicted_label = self.class_names[predicted_class]
|
| 387 |
return predicted_label
|
| 388 |
|
| 389 |
+
def classify_from_frames(self, frames):
|
| 390 |
+
predictions = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
+
for frame in frames:
|
| 393 |
+
with torch.no_grad():
|
| 394 |
+
predictions_frame = self.model(frame.unsqueeze(0))
|
| 395 |
+
probabilities = torch.nn.functional.softmax(predictions_frame[0], dim=0)
|
| 396 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 397 |
+
predicted_label = self.class_names[predicted_class]
|
| 398 |
+
predictions.append(predicted_label)
|
| 399 |
|
| 400 |
+
# Return a single prediction for the entire video
|
| 401 |
+
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 402 |
+
final_prediction = max(set(predictions), key=predictions.count)
|
| 403 |
+
return final_prediction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
|
| 406 |
|
|
|
|
| 658 |
# frames.extend(capture_frame_from_webcam())
|
| 659 |
|
| 660 |
# Detect and classify gender
|
| 661 |
+
gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
|
| 662 |
+
predicted_gender = gender_classifier.classify_from_frames(frames)
|
| 663 |
|
| 664 |
|
| 665 |
# Process background images and apply beard style and color along with hair style and color
|
|
|
|
| 685 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 686 |
# Detect and classify beard style
|
| 687 |
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 688 |
+
predicted_style_label = beard_classifier.classify_from_frames(frames)
|
| 689 |
|
| 690 |
# Detect and classify beard color
|
| 691 |
beard_color_classifier = BeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 692 |
+
predicted_color_label = beard_color_classifier.classify_from_frames(frames)
|
| 693 |
|
| 694 |
# Classify hairstyle
|
| 695 |
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 696 |
+
predicted_hairStyle_label = hair_style_classifier.classify_from_frames(frames)
|
| 697 |
|
| 698 |
#classify menHairColor
|
| 699 |
menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 700 |
+
predicted_menhairColor_label = menhair_color_classifier.classify_from_frames(frames)
|
| 701 |
if predicted_style_label == 'Bandholz':
|
| 702 |
process_image_Beard(background_image, 320,
|
| 703 |
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
|
|
|
| 760 |
y_coordinate = 50
|
| 761 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 762 |
WomenHairStyle_classifier = WomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
|
| 763 |
+
predicted_WomenHairStyle = WomenHairStyle_classifier.classify_from_frames(frames)
|
| 764 |
|
| 765 |
WomenHairColor_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 766 |
+
predicted_WomenHairColor = WomenHairColor_classifier.classify_from_frames(frames)
|
| 767 |
if predicted_WomenHairStyle == 'MediumLength':
|
| 768 |
process_image_WomanHair(background_image, 300,460,
|
| 769 |
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_WomenHairColor}.png",
|