Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,7 +36,7 @@ def parse_args():
|
|
| 36 |
args = parser.parse_args()
|
| 37 |
return args
|
| 38 |
|
| 39 |
-
def capture_frame_from_webcam(duration=
|
| 40 |
data_transforms = transforms.Compose([
|
| 41 |
transforms.Resize((224, 224)),
|
| 42 |
transforms.ToTensor(),
|
|
@@ -108,21 +108,18 @@ class GenderClassifier:
|
|
| 108 |
|
| 109 |
return predicted_label
|
| 110 |
|
| 111 |
-
def classify_from_frames(self,
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 124 |
-
final_prediction = max(set(predictions), key=predictions.count)
|
| 125 |
-
return final_prediction
|
| 126 |
|
| 127 |
|
| 128 |
class WomenHairStyleClassifier:
|
|
@@ -162,16 +159,18 @@ class WomenHairStyleClassifier:
|
|
| 162 |
predicted_label = self.class_names[predicted_class]
|
| 163 |
|
| 164 |
return predicted_label
|
| 165 |
-
def classify_from_frames(self,
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
| 175 |
|
| 176 |
# Return a single prediction for the entire video
|
| 177 |
# You can choose to use the majority vote or any other method to determine the final prediction
|
|
@@ -217,21 +216,18 @@ class WomenHairColorClassifier:
|
|
| 217 |
|
| 218 |
return predicted_label
|
| 219 |
|
| 220 |
-
def classify_from_frames(self,
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 233 |
-
final_prediction = max(set(predictions), key=predictions.count)
|
| 234 |
-
return final_prediction
|
| 235 |
|
| 236 |
# Function to classify beard style
|
| 237 |
class BeardClassifier:
|
|
@@ -269,21 +265,18 @@ class BeardClassifier:
|
|
| 269 |
predicted_label = self.class_names[predicted_class]
|
| 270 |
return predicted_label
|
| 271 |
|
| 272 |
-
def classify_from_frames(self,
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 285 |
-
final_prediction = max(set(predictions), key=predictions.count)
|
| 286 |
-
return final_prediction
|
| 287 |
|
| 288 |
|
| 289 |
# Function to classify beard color
|
|
@@ -322,21 +315,18 @@ class BeardColorClassifier:
|
|
| 322 |
predicted_label = self.class_names[predicted_class]
|
| 323 |
return predicted_label
|
| 324 |
|
| 325 |
-
def classify_from_frames(self,
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 338 |
-
final_prediction = max(set(predictions), key=predictions.count)
|
| 339 |
-
return final_prediction
|
| 340 |
|
| 341 |
|
| 342 |
|
|
@@ -376,22 +366,18 @@ class HairStyleClassifier:
|
|
| 376 |
predicted_label = self.class_names[predicted_class]
|
| 377 |
return predicted_label
|
| 378 |
|
| 379 |
-
def classify_from_frames(self,
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 392 |
-
final_prediction = max(set(predictions), key=predictions.count)
|
| 393 |
-
return final_prediction
|
| 394 |
-
|
| 395 |
|
| 396 |
class MenHairColorClassifier:
|
| 397 |
def __init__(self, model_path, class_names):
|
|
@@ -428,21 +414,18 @@ class MenHairColorClassifier:
|
|
| 428 |
predicted_label = self.class_names[predicted_class]
|
| 429 |
return predicted_label
|
| 430 |
|
| 431 |
-
def classify_from_frames(self,
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
# You can choose to use the majority vote or any other method to determine the final prediction
|
| 444 |
-
final_prediction = max(set(predictions), key=predictions.count)
|
| 445 |
-
return final_prediction
|
| 446 |
|
| 447 |
|
| 448 |
|
|
@@ -645,32 +628,120 @@ def generate_funko_figurines(input_image):
|
|
| 645 |
|
| 646 |
def Igenerate_funko_figurines():
|
| 647 |
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
|
|
|
| 651 |
# Initialize variables to store frames and track time
|
| 652 |
-
#frames = []
|
| 653 |
-
#start_time = time.time()
|
| 654 |
-
frames = capture_frame_from_webcam(duration=5)
|
| 655 |
-
# Continuously capture frames for the specified duration
|
| 656 |
-
#while (time.time() - start_time) < captured_duration:
|
| 657 |
-
# frames.extend(capture_frame_from_webcam())
|
| 658 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
# Detect and classify gender
|
| 660 |
gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
|
| 661 |
-
predicted_gender = gender_classifier.classify_from_frames(frames)
|
| 662 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
-
#
|
| 665 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
|
| 667 |
if predicted_gender == 'Male':
|
|
|
|
|
|
|
| 668 |
background_image_paths = male_background_image_paths
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
if predicted_gender == 'Female':
|
| 670 |
background_image_paths = female_background_image_paths
|
|
|
|
|
|
|
| 671 |
|
| 672 |
-
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
x_coordinate = 90
|
| 675 |
y_coordinate = 50
|
| 676 |
add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
|
|
@@ -682,21 +753,7 @@ def Igenerate_funko_figurines():
|
|
| 682 |
x_coordinate = 90
|
| 683 |
y_coordinate = 50
|
| 684 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 685 |
-
# Detect and classify beard style
|
| 686 |
-
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 687 |
-
predicted_style_label = beard_classifier.classify_from_frames(frames)
|
| 688 |
|
| 689 |
-
# Detect and classify beard color
|
| 690 |
-
beard_color_classifier = BeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 691 |
-
predicted_color_label = beard_color_classifier.classify_from_frames(frames)
|
| 692 |
-
|
| 693 |
-
# Classify hairstyle
|
| 694 |
-
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 695 |
-
predicted_hairStyle_label = hair_style_classifier.classify_from_frames(frames)
|
| 696 |
-
|
| 697 |
-
#classify menHairColor
|
| 698 |
-
menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 699 |
-
predicted_menhairColor_label = menhair_color_classifier.classify_from_frames(frames)
|
| 700 |
if predicted_style_label == 'Bandholz':
|
| 701 |
process_image_Beard(background_image, 320,
|
| 702 |
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
|
@@ -730,24 +787,24 @@ def Igenerate_funko_figurines():
|
|
| 730 |
# Add other conditions for different beard styles
|
| 731 |
|
| 732 |
# Overlay hairstyle
|
| 733 |
-
if
|
| 734 |
process_image_menHair(background_image, 336, 420,
|
| 735 |
-
f"Data/AdobeColorFunko/MenHairstyle/Afro/{
|
| 736 |
41, 76)
|
| 737 |
|
| 738 |
-
if
|
| 739 |
process_image_menHair(background_image, 305, 420,
|
| 740 |
-
f"Data/AdobeColorFunko/MenHairstyle/Puff/{
|
| 741 |
56, 68)
|
| 742 |
|
| 743 |
-
if
|
| 744 |
process_image_menHair(background_image, 310, 420,
|
| 745 |
-
f"Data/AdobeColorFunko/MenHairstyle/Spike/{
|
| 746 |
52, 70)
|
| 747 |
|
| 748 |
-
if
|
| 749 |
process_image_menHair(background_image, 310, 420,
|
| 750 |
-
f"Data/AdobeColorFunko/MenHairstyle/Bald/{
|
| 751 |
67, 120)
|
| 752 |
|
| 753 |
|
|
@@ -758,29 +815,25 @@ def Igenerate_funko_figurines():
|
|
| 758 |
x_coordinate = 90
|
| 759 |
y_coordinate = 50
|
| 760 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 761 |
-
|
| 762 |
-
predicted_WomenHairStyle = WomenHairStyle_classifier.classify_from_frames(frames)
|
| 763 |
-
|
| 764 |
-
WomenHairColor_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 765 |
-
predicted_WomenHairColor = WomenHairColor_classifier.classify_from_frames(frames)
|
| 766 |
-
if predicted_WomenHairStyle == 'MediumLength':
|
| 767 |
process_image_WomanHair(background_image, 300,460,
|
| 768 |
-
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{
|
| 769 |
56, 50)
|
| 770 |
|
| 771 |
-
if
|
| 772 |
process_image_WomanHair(background_image, 270,460,
|
| 773 |
-
f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{
|
| 774 |
61, 49)
|
| 775 |
|
| 776 |
-
if
|
| 777 |
process_image_WomanHair(background_image, 300,450,
|
| 778 |
-
f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{
|
| 779 |
54, 56)
|
| 780 |
|
| 781 |
|
| 782 |
# Convert the resulting image to base64
|
| 783 |
buffered = BytesIO()
|
|
|
|
| 784 |
background_image.save(buffered, format="PNG")
|
| 785 |
#base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 786 |
final_images.append(background_image)
|
|
@@ -795,7 +848,7 @@ with gr.Blocks() as demo:
|
|
| 795 |
# Funko POP! Figure Creation
|
| 796 |
Enabling Streamlined Automation with Artificial Intelligence
|
| 797 |
""")
|
| 798 |
-
imageComponent = gr.Image(type="filepath", height=
|
| 799 |
with gr.Row():
|
| 800 |
MyOutputs = [gr.Image(type="pil", label="Generated Image " + str(i + 1), height=300, width=200) for i in range(3)]
|
| 801 |
submitButton = gr.Button(value="Submit")
|
|
|
|
| 36 |
args = parser.parse_args()
|
| 37 |
return args
|
| 38 |
|
| 39 |
+
def capture_frame_from_webcam(duration=5):
|
| 40 |
data_transforms = transforms.Compose([
|
| 41 |
transforms.Resize((224, 224)),
|
| 42 |
transforms.ToTensor(),
|
|
|
|
| 108 |
|
| 109 |
return predicted_label
|
| 110 |
|
| 111 |
+
def classify_from_frames(self, image, image_type):
|
| 112 |
+
input_image = None
|
| 113 |
+
if image_type == True:
|
| 114 |
+
input_image = self.preprocess_image(image)
|
| 115 |
+
else:
|
| 116 |
+
input_image = image.unsqueeze(0)
|
| 117 |
+
with torch.no_grad():
|
| 118 |
+
predictions = self.model(input_image)
|
| 119 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 120 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 121 |
+
predicted_label = self.class_names[predicted_class]
|
| 122 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
class WomenHairStyleClassifier:
|
|
|
|
| 159 |
predicted_label = self.class_names[predicted_class]
|
| 160 |
|
| 161 |
return predicted_label
|
| 162 |
+
def classify_from_frames(self, image, image_type):
|
| 163 |
+
input_image = None
|
| 164 |
+
if image_type == True:
|
| 165 |
+
input_image = self.preprocess_image(image)
|
| 166 |
+
else:
|
| 167 |
+
input_image = image.unsqueeze(0)
|
| 168 |
+
with torch.no_grad():
|
| 169 |
+
predictions = self.model(input_image)
|
| 170 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 171 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 172 |
+
predicted_label = self.class_names[predicted_class]
|
| 173 |
+
return predicted_label
|
| 174 |
|
| 175 |
# Return a single prediction for the entire video
|
| 176 |
# You can choose to use the majority vote or any other method to determine the final prediction
|
|
|
|
| 216 |
|
| 217 |
return predicted_label
|
| 218 |
|
| 219 |
+
def classify_from_frames(self, image, image_type):
|
| 220 |
+
input_image = None
|
| 221 |
+
if image_type == True:
|
| 222 |
+
input_image = self.preprocess_image(image)
|
| 223 |
+
else:
|
| 224 |
+
input_image = image.unsqueeze(0)
|
| 225 |
+
with torch.no_grad():
|
| 226 |
+
predictions = self.model(input_image)
|
| 227 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 228 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 229 |
+
predicted_label = self.class_names[predicted_class]
|
| 230 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
# Function to classify beard style
|
| 233 |
class BeardClassifier:
|
|
|
|
| 265 |
predicted_label = self.class_names[predicted_class]
|
| 266 |
return predicted_label
|
| 267 |
|
| 268 |
+
def classify_from_frames(self, image, image_type):
|
| 269 |
+
input_image = None
|
| 270 |
+
if image_type == True:
|
| 271 |
+
input_image = self.preprocess_image(image)
|
| 272 |
+
else:
|
| 273 |
+
input_image = image.unsqueeze(0)
|
| 274 |
+
with torch.no_grad():
|
| 275 |
+
predictions = self.model(input_image)
|
| 276 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 277 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 278 |
+
predicted_label = self.class_names[predicted_class]
|
| 279 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
|
| 282 |
# Function to classify beard color
|
|
|
|
| 315 |
predicted_label = self.class_names[predicted_class]
|
| 316 |
return predicted_label
|
| 317 |
|
| 318 |
+
def classify_from_frames(self, image, image_type):
|
| 319 |
+
input_image = None
|
| 320 |
+
if image_type == True:
|
| 321 |
+
input_image = self.preprocess_image(image)
|
| 322 |
+
else:
|
| 323 |
+
input_image = image.unsqueeze(0)
|
| 324 |
+
with torch.no_grad():
|
| 325 |
+
predictions = self.model(input_image)
|
| 326 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 327 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 328 |
+
predicted_label = self.class_names[predicted_class]
|
| 329 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
|
| 332 |
|
|
|
|
| 366 |
predicted_label = self.class_names[predicted_class]
|
| 367 |
return predicted_label
|
| 368 |
|
| 369 |
+
def classify_from_frames(self, image, image_type):
|
| 370 |
+
input_image = None
|
| 371 |
+
if image_type == True:
|
| 372 |
+
input_image = self.preprocess_image(image)
|
| 373 |
+
else:
|
| 374 |
+
input_image = image.unsqueeze(0)
|
| 375 |
+
with torch.no_grad():
|
| 376 |
+
predictions = self.model(input_image)
|
| 377 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 378 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 379 |
+
predicted_label = self.class_names[predicted_class]
|
| 380 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
class MenHairColorClassifier:
|
| 383 |
def __init__(self, model_path, class_names):
|
|
|
|
| 414 |
predicted_label = self.class_names[predicted_class]
|
| 415 |
return predicted_label
|
| 416 |
|
| 417 |
+
def classify_from_frames(self, image, image_type):
|
| 418 |
+
input_image = None
|
| 419 |
+
if image_type == True:
|
| 420 |
+
input_image = self.preprocess_image(image)
|
| 421 |
+
else:
|
| 422 |
+
input_image = image.unsqueeze(0)
|
| 423 |
+
with torch.no_grad():
|
| 424 |
+
predictions = self.model(input_image)
|
| 425 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 426 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 427 |
+
predicted_label = self.class_names[predicted_class]
|
| 428 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
|
| 431 |
|
|
|
|
| 628 |
|
| 629 |
def Igenerate_funko_figurines():
|
| 630 |
|
| 631 |
+
predicted_women_hairstyle = None
|
| 632 |
+
predicted_women_haircolor = None
|
| 633 |
+
predicted_gender = None
|
| 634 |
+
predicted_style_label = None
|
| 635 |
+
predicted_color_label = None
|
| 636 |
+
predicted_hairstyle_label = None
|
| 637 |
+
predicted_menhaircolor_label = None
|
| 638 |
+
background_image_paths = None
|
| 639 |
|
| 640 |
+
# Capture video from the webcam for 7 seconds
|
| 641 |
# Initialize variables to store frames and track time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
|
| 643 |
+
|
| 644 |
+
# Classify women hairstyle
|
| 645 |
+
women_hairstyle_classifier = WomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
|
| 646 |
+
|
| 647 |
+
# Classify women hair color
|
| 648 |
+
women_hair_color_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 649 |
+
|
| 650 |
# Detect and classify gender
|
| 651 |
gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
|
|
|
|
| 652 |
|
| 653 |
+
# Detect and classify beard style
|
| 654 |
+
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 655 |
+
|
| 656 |
+
# Detect and classify beard color
|
| 657 |
+
beard_color_classifier = BeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 658 |
|
| 659 |
+
# Classify hairstyle
|
| 660 |
+
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 661 |
+
|
| 662 |
+
#classify menHairColor
|
| 663 |
+
menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 664 |
+
|
| 665 |
+
def predict_male_features_from_frames(frame):
|
| 666 |
+
return [
|
| 667 |
+
beard_classifier.classify_from_frames(image=frame,image_type=False),
|
| 668 |
+
beard_color_classifier.classify_from_frames(image=frame,image_type=False),
|
| 669 |
+
hair_style_classifier.classify_from_frames(image=frame,image_type=False),
|
| 670 |
+
menhair_color_classifier.classify_from_frames(image=frame,image_type=False)
|
| 671 |
+
]
|
| 672 |
+
|
| 673 |
+
def predict_female_features_from_frames(frame):
|
| 674 |
+
return [
|
| 675 |
+
women_hairstyle_classifier.classify_from_frames(image=frame,image_type=False),
|
| 676 |
+
women_hair_color_classifier.classify_from_frames(image=frame,image_type=False),
|
| 677 |
+
]
|
| 678 |
+
|
| 679 |
+
def predict_gender_from_frames(frame):
|
| 680 |
+
return gender_classifier.classify_from_frames(image=frame,image_type=False)
|
| 681 |
+
|
| 682 |
+
|
| 683 |
+
|
| 684 |
+
print("Capturing frames")
|
| 685 |
+
frames = capture_frame_from_webcam(duration=7)
|
| 686 |
+
print("Frames captured")
|
| 687 |
+
print("Predictions started")
|
| 688 |
+
# time counting
|
| 689 |
+
gp_start = time.time()
|
| 690 |
+
gender_predictions = map(predict_gender_from_frames, frames)
|
| 691 |
+
gender_counter = Counter(gender_predictions)
|
| 692 |
+
predicted_gender = gender_counter.most_common(1)[0][0]
|
| 693 |
+
# time counting
|
| 694 |
+
gp_end = time.time()
|
| 695 |
+
print(f'Predicted Gender: {predicted_gender} and it took {round(gp_end - gp_start)}s')
|
| 696 |
|
| 697 |
if predicted_gender == 'Male':
|
| 698 |
+
# time counting
|
| 699 |
+
mp_start = time.time()
|
| 700 |
background_image_paths = male_background_image_paths
|
| 701 |
+
|
| 702 |
+
facial_feature_predictions = map(predict_male_features_from_frames, frames)
|
| 703 |
+
beard_style_counter = Counter()
|
| 704 |
+
beard_color_counter = Counter()
|
| 705 |
+
hair_style_label_counter = Counter()
|
| 706 |
+
menhair_color_counter = Counter()
|
| 707 |
+
|
| 708 |
+
|
| 709 |
+
for pred in facial_feature_predictions:
|
| 710 |
+
beard_style_counter[pred[0]] += 1
|
| 711 |
+
beard_color_counter[pred[1]] += 1
|
| 712 |
+
hair_style_label_counter[pred[2]] += 1
|
| 713 |
+
menhair_color_counter[pred[3]] += 1
|
| 714 |
+
|
| 715 |
+
predicted_style_label = beard_style_counter.most_common(1)[0][0]
|
| 716 |
+
predicted_color_label = beard_color_counter.most_common(1)[0][0]
|
| 717 |
+
predicted_hairstyle_label = hair_style_label_counter.most_common(1)[0][0]
|
| 718 |
+
predicted_menhaircolor_label = menhair_color_counter.most_common(1)[0][0]
|
| 719 |
+
# time counting
|
| 720 |
+
mp_end = time.time()
|
| 721 |
+
|
| 722 |
+
|
| 723 |
+
print("Predictions are:\n")
|
| 724 |
+
print(predicted_style_label,predicted_color_label,predicted_hairstyle_label,predicted_menhaircolor_label)
|
| 725 |
+
print(f'\nand it took {round(mp_end - mp_start)}s')
|
| 726 |
+
|
| 727 |
if predicted_gender == 'Female':
|
| 728 |
background_image_paths = female_background_image_paths
|
| 729 |
+
women_hairstyle_counter = women_haircolor_counter = Counter()
|
| 730 |
+
facial_feature_predictions = map(predict_female_features_from_frames, frames)
|
| 731 |
|
| 732 |
+
for pred in facial_feature_predictions:
|
| 733 |
+
women_hairstyle_counter[pred[0]] += 1
|
| 734 |
+
women_haircolor_counter[pred[1]] += 1
|
| 735 |
+
|
| 736 |
+
predicted_women_hairstyle = women_hairstyle_counter.most_common(1)[0][0]
|
| 737 |
+
predicted_women_haircolor = women_haircolor_counter.most_common(1)[0][0]
|
| 738 |
+
|
| 739 |
+
# Process background images and apply beard style and color along with hair style and color
|
| 740 |
+
final_images = []
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
for path in background_image_paths:
|
| 744 |
+
background_image = Image.open(path)
|
| 745 |
x_coordinate = 90
|
| 746 |
y_coordinate = 50
|
| 747 |
add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
|
|
|
|
| 753 |
x_coordinate = 90
|
| 754 |
y_coordinate = 50
|
| 755 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
|
|
|
|
|
|
|
|
|
| 756 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
if predicted_style_label == 'Bandholz':
|
| 758 |
process_image_Beard(background_image, 320,
|
| 759 |
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
|
|
|
| 787 |
# Add other conditions for different beard styles
|
| 788 |
|
| 789 |
# Overlay hairstyle
|
| 790 |
+
if predicted_hairstyle_label == 'Afro':
|
| 791 |
process_image_menHair(background_image, 336, 420,
|
| 792 |
+
f"Data/AdobeColorFunko/MenHairstyle/Afro/{predicted_menhaircolor_label}.png",
|
| 793 |
41, 76)
|
| 794 |
|
| 795 |
+
if predicted_hairstyle_label == 'Puff':
|
| 796 |
process_image_menHair(background_image, 305, 420,
|
| 797 |
+
f"Data/AdobeColorFunko/MenHairstyle/Puff/{predicted_menhaircolor_label}.png",
|
| 798 |
56, 68)
|
| 799 |
|
| 800 |
+
if predicted_hairstyle_label == 'Spike':
|
| 801 |
process_image_menHair(background_image, 310, 420,
|
| 802 |
+
f"Data/AdobeColorFunko/MenHairstyle/Spike/{predicted_menhaircolor_label}.png",
|
| 803 |
52, 70)
|
| 804 |
|
| 805 |
+
if predicted_hairstyle_label == 'Bald':
|
| 806 |
process_image_menHair(background_image, 310, 420,
|
| 807 |
+
f"Data/AdobeColorFunko/MenHairstyle/Bald/{predicted_menhaircolor_label}.png",
|
| 808 |
67, 120)
|
| 809 |
|
| 810 |
|
|
|
|
| 815 |
x_coordinate = 90
|
| 816 |
y_coordinate = 50
|
| 817 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 818 |
+
if predicted_women_hairstyle == 'MediumLength':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
process_image_WomanHair(background_image, 300,460,
|
| 820 |
+
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_women_haircolor}.png",
|
| 821 |
56, 50)
|
| 822 |
|
| 823 |
+
if predicted_women_hairstyle == 'ShortHair':
|
| 824 |
process_image_WomanHair(background_image, 270,460,
|
| 825 |
+
f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{predicted_women_haircolor}.png",
|
| 826 |
61, 49)
|
| 827 |
|
| 828 |
+
if predicted_women_hairstyle == 'SidePlait':
|
| 829 |
process_image_WomanHair(background_image, 300,450,
|
| 830 |
+
f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{predicted_women_haircolor}.png",
|
| 831 |
54, 56)
|
| 832 |
|
| 833 |
|
| 834 |
# Convert the resulting image to base64
|
| 835 |
buffered = BytesIO()
|
| 836 |
+
# background_image.resize((200,400))
|
| 837 |
background_image.save(buffered, format="PNG")
|
| 838 |
#base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 839 |
final_images.append(background_image)
|
|
|
|
| 848 |
# Funko POP! Figure Creation
|
| 849 |
Enabling Streamlined Automation with Artificial Intelligence
|
| 850 |
""")
|
| 851 |
+
imageComponent = gr.Image(type="filepath", height=250, width=250)
|
| 852 |
with gr.Row():
|
| 853 |
MyOutputs = [gr.Image(type="pil", label="Generated Image " + str(i + 1), height=300, width=200) for i in range(3)]
|
| 854 |
submitButton = gr.Button(value="Submit")
|