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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# Combined Code for Beard and Hairstyle Detection and Styling
|
| 9 |
+
|
| 10 |
+
# Function to classify beard style
|
| 11 |
+
class BeardClassifier:
|
| 12 |
+
def __init__(self, model_path, class_names):
|
| 13 |
+
self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
|
| 14 |
+
num_ftrs = self.model.fc.in_features
|
| 15 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
|
| 16 |
+
self.load_model(model_path)
|
| 17 |
+
self.model.eval()
|
| 18 |
+
self.data_transforms = torch.nn.Sequential(
|
| 19 |
+
torch.nn.Resize((224, 224)),
|
| 20 |
+
torch.nn.ToTensor(),
|
| 21 |
+
torch.nn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 22 |
+
)
|
| 23 |
+
self.class_names = class_names
|
| 24 |
+
|
| 25 |
+
def preprocess_image(self, image):
|
| 26 |
+
image = Image.open(image).convert("RGB")
|
| 27 |
+
image = self.data_transforms(image)
|
| 28 |
+
image = image.unsqueeze(0)
|
| 29 |
+
return image
|
| 30 |
+
|
| 31 |
+
def load_model(self, model_path):
|
| 32 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 33 |
+
|
| 34 |
+
def classify_beard(self, image):
|
| 35 |
+
input_image = self.preprocess_image(image)
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
predictions = self.model(input_image)
|
| 38 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 39 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 40 |
+
predicted_label = self.class_names[predicted_class]
|
| 41 |
+
return predicted_label
|
| 42 |
+
|
| 43 |
+
# Function to classify beard color
|
| 44 |
+
class BeardColorClassifier:
|
| 45 |
+
def __init__(self, model_path, class_names):
|
| 46 |
+
self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
|
| 47 |
+
num_ftrs = self.model.fc.in_features
|
| 48 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
|
| 49 |
+
self.load_model(model_path)
|
| 50 |
+
self.model.eval()
|
| 51 |
+
self.data_transforms = torch.nn.Sequential(
|
| 52 |
+
torch.nn.Resize((224, 224)),
|
| 53 |
+
torch.nn.ToTensor(),
|
| 54 |
+
torch.nn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 55 |
+
)
|
| 56 |
+
self.class_names = class_names
|
| 57 |
+
|
| 58 |
+
def preprocess_image(self, image):
|
| 59 |
+
image = Image.open(image).convert("RGB")
|
| 60 |
+
image = self.data_transforms(image)
|
| 61 |
+
image = image.unsqueeze(0)
|
| 62 |
+
return image
|
| 63 |
+
|
| 64 |
+
def load_model(self, model_path):
|
| 65 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 66 |
+
|
| 67 |
+
def classify_beard_color(self, image):
|
| 68 |
+
input_image = self.preprocess_image(image)
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
predictions = self.model(input_image)
|
| 71 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 72 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 73 |
+
predicted_label = self.class_names[predicted_class]
|
| 74 |
+
return predicted_label
|
| 75 |
+
|
| 76 |
+
def dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 77 |
+
placeholder_image = Image.open(placeholder_image_path)
|
| 78 |
+
target_size = (x, y)
|
| 79 |
+
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 80 |
+
placeholder_array = np.array(placeholder_image)
|
| 81 |
+
placeholder_width, placeholder_height = placeholder_image.size
|
| 82 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 83 |
+
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 84 |
+
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 85 |
+
background_array = np.array(background_image)
|
| 86 |
+
|
| 87 |
+
# Function to overlay a beard on a background image
|
| 88 |
+
def process_image_Beard(background_image, x, placeholder_image_path, x_coordinate, y_coordinate):
|
| 89 |
+
placeholder_image = Image.open(placeholder_image_path)
|
| 90 |
+
target_size = (x, x)
|
| 91 |
+
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 92 |
+
placeholder_array = np.array(placeholder_image)
|
| 93 |
+
placeholder_width, placeholder_height = placeholder_image.size
|
| 94 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 95 |
+
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 96 |
+
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 97 |
+
background_array = np.array(background_image)
|
| 98 |
+
placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 99 |
+
|
| 100 |
+
# Function to classify hairstyle
|
| 101 |
+
class HairStyleClassifier:
|
| 102 |
+
def __init__(self, model_path, class_names):
|
| 103 |
+
self.model = torch.hub.load('pytorch/vision', 'resnet18', pretrained=False)
|
| 104 |
+
num_ftrs = self.model.fc.in_features
|
| 105 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
|
| 106 |
+
self.load_model(model_path)
|
| 107 |
+
self.model.eval()
|
| 108 |
+
self.data_transforms = torch.nn.Sequential(
|
| 109 |
+
torch.nn.Resize((224, 224)),
|
| 110 |
+
torch.nn.ToTensor(),
|
| 111 |
+
torch.nn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 112 |
+
)
|
| 113 |
+
self.class_names = class_names
|
| 114 |
+
|
| 115 |
+
def preprocess_image(self, image):
|
| 116 |
+
image = Image.open(image).convert("RGB")
|
| 117 |
+
image = self.data_transforms(image)
|
| 118 |
+
image = image.unsqueeze(0)
|
| 119 |
+
return image
|
| 120 |
+
|
| 121 |
+
def load_model(self, model_path):
|
| 122 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 123 |
+
|
| 124 |
+
def classify_hair(self, image):
|
| 125 |
+
input_image = self.preprocess_image(image)
|
| 126 |
+
with torch.no_grad():
|
| 127 |
+
predictions = self.model(input_image)
|
| 128 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 129 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 130 |
+
predicted_label = self.class_names[predicted_class]
|
| 131 |
+
return predicted_label
|
| 132 |
+
|
| 133 |
+
# Function to overlay a hairstyle on a background image
|
| 134 |
+
def process_image_menHair(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 135 |
+
placeholder_image = Image.open(placeholder_image_path)
|
| 136 |
+
target_size = (x, y)
|
| 137 |
+
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 138 |
+
placeholder_array = np.array(placeholder_image)
|
| 139 |
+
placeholder_width, placeholder_height = placeholder_image.size
|
| 140 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 141 |
+
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 142 |
+
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 143 |
+
background_array = np.array(background_image)
|
| 144 |
+
placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 145 |
+
|
| 146 |
+
# Function to generate Funko figurines
|
| 147 |
+
def generate_funko_figurines(input_image, background_image_paths):
|
| 148 |
+
# Detect and classify beard style
|
| 149 |
+
beard_classifier = BeardClassifier('path_to_beard_style_model', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 150 |
+
predicted_style_label = beard_classifier.classify_beard(input_image)
|
| 151 |
+
|
| 152 |
+
# Detect and classify beard color
|
| 153 |
+
beard_color_classifier = BeardColorClassifier('path_to_beard_color_model', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 154 |
+
predicted_color_label = beard_color_classifier.classify_beard_color(input_image)
|
| 155 |
+
|
| 156 |
+
# Classify hairstyle
|
| 157 |
+
hair_style_classifier = HairStyleClassifier('path_to_hairstyle_model', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 158 |
+
predicted_hairStyle_label = hair_style_classifier.classify_hair(input_image)
|
| 159 |
+
|
| 160 |
+
# Process background images and apply beard style and color along with hair style and color
|
| 161 |
+
final_images = []
|
| 162 |
+
|
| 163 |
+
for background_image_path in background_image_paths:
|
| 164 |
+
background_image = Image.open(background_image_path)
|
| 165 |
+
x_coordinate = 90
|
| 166 |
+
y_coordinate = 50
|
| 167 |
+
dummy_eye(background_image, 245, 345, 'path_to_eye_image', x_coordinate, y_coordinate)
|
| 168 |
+
|
| 169 |
+
if predicted_style_label == 'Bandholz':
|
| 170 |
+
process_image_Beard(background_image, 320, 'path_to_beard_image', 50, 142)
|
| 171 |
+
|
| 172 |
+
# Add other conditions for different beard styles
|
| 173 |
+
|
| 174 |
+
# Overlay hairstyle
|
| 175 |
+
if predicted_hairStyle_label == 'Afro':
|
| 176 |
+
process_image_menHair(background_image, 336, 420, 'path_to_hairstyle_image', 41, 76)
|
| 177 |
+
|
| 178 |
+
# Add other conditions for different hairstyles
|
| 179 |
+
|
| 180 |
+
# Convert the resulting image to base64
|
| 181 |
+
buffered = BytesIO()
|
| 182 |
+
background_image.save(buffered, format="PNG")
|
| 183 |
+
base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 184 |
+
final_images.append(base64_image)
|
| 185 |
+
|
| 186 |
+
return final_images
|
| 187 |
+
|
| 188 |
+
# Define Gradio input components
|
| 189 |
+
input_image = gr.inputs.Image(type="pil", label="Upload your image")
|
| 190 |
+
background_images = [gr.inputs.Image(type="pil", label="Background Image " + str(i + 1)) for i in range(3)]
|
| 191 |
+
|
| 192 |
+
# Create Gradio interface
|
| 193 |
+
gr.Interface(
|
| 194 |
+
fn=generate_funko_figurines,
|
| 195 |
+
inputs=[input_image] + background_images,
|
| 196 |
+
outputs=gr.outputs.Image(type="base64", label="Generated Image"),
|
| 197 |
+
title="Funko Figurine Generator",
|
| 198 |
+
description="Generate personalized Funko figurines with different styles and backgrounds.",
|
| 199 |
+
).launch()
|