Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,7 @@ background_image_paths = [
|
|
| 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)
|
|
@@ -55,6 +56,81 @@ class GenderClassifier:
|
|
| 55 |
|
| 56 |
return predicted_label
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Function to classify beard style
|
| 59 |
class BeardClassifier:
|
| 60 |
def __init__(self, model_path, class_names):
|
|
@@ -77,7 +153,10 @@ class BeardClassifier:
|
|
| 77 |
return image
|
| 78 |
|
| 79 |
def load_model(self, model_path):
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def classify_beard(self, image):
|
| 83 |
input_image = self.preprocess_image(image)
|
|
@@ -110,7 +189,10 @@ class BeardColorClassifier:
|
|
| 110 |
return image
|
| 111 |
|
| 112 |
def load_model(self, model_path):
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
def classify_beard_color(self, image):
|
| 116 |
input_image = self.preprocess_image(image)
|
|
@@ -121,30 +203,6 @@ class BeardColorClassifier:
|
|
| 121 |
predicted_label = self.class_names[predicted_class]
|
| 122 |
return predicted_label
|
| 123 |
|
| 124 |
-
def dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 125 |
-
placeholder_image = Image.open(placeholder_image_path)
|
| 126 |
-
target_size = (x, y)
|
| 127 |
-
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 128 |
-
placeholder_array = np.array(placeholder_image)
|
| 129 |
-
placeholder_width, placeholder_height = placeholder_image.size
|
| 130 |
-
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 131 |
-
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 132 |
-
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 133 |
-
background_array = np.array(background_image)
|
| 134 |
-
|
| 135 |
-
# Function to overlay a beard on a background image
|
| 136 |
-
def process_image_Beard(background_image, x, placeholder_image_path, x_coordinate, y_coordinate):
|
| 137 |
-
placeholder_image = Image.open(placeholder_image_path)
|
| 138 |
-
target_size = (x, x)
|
| 139 |
-
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 140 |
-
placeholder_array = np.array(placeholder_image)
|
| 141 |
-
placeholder_width, placeholder_height = placeholder_image.size
|
| 142 |
-
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 143 |
-
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 144 |
-
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 145 |
-
background_array = np.array(background_image)
|
| 146 |
-
placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 147 |
-
|
| 148 |
# Function to classify hairstyle
|
| 149 |
class HairStyleClassifier:
|
| 150 |
def __init__(self, model_path, class_names):
|
|
@@ -167,7 +225,10 @@ class HairStyleClassifier:
|
|
| 167 |
return image
|
| 168 |
|
| 169 |
def load_model(self, model_path):
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
def classify_hair(self, image):
|
| 173 |
input_image = self.preprocess_image(image)
|
|
@@ -177,16 +238,32 @@ class HairStyleClassifier:
|
|
| 177 |
predicted_class = torch.argmax(probabilities).item()
|
| 178 |
predicted_label = self.class_names[predicted_class]
|
| 179 |
return predicted_label
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
| 187 |
background_array = np.array(background_image)
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
placeholder_image = Image.open(placeholder_image_path)
|
| 191 |
target_size = (x, y)
|
| 192 |
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
|
@@ -196,6 +273,18 @@ def add_womenHair(background_image, x, y, placeholder_image_path, x_coordinate,
|
|
| 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 |
|
|
@@ -214,9 +303,14 @@ def process_image_menHair(background_image, x, y, placeholder_image_path, x_coor
|
|
| 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
|
|
@@ -309,7 +403,22 @@ def generate_funko_figurines(input_image):
|
|
| 309 |
x_coordinate = 90
|
| 310 |
y_coordinate = 50
|
| 311 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
# Convert the resulting image to base64
|
| 314 |
buffered = BytesIO()
|
| 315 |
background_image.save(buffered, format="PNG")
|
|
|
|
| 17 |
"Data/AdobeColorFunko/Outfits/DummyDress3.png"
|
| 18 |
]
|
| 19 |
|
| 20 |
+
|
| 21 |
class GenderClassifier:
|
| 22 |
def __init__(self, model_path, class_names):
|
| 23 |
self.model = models.resnet18(pretrained=False)
|
|
|
|
| 56 |
|
| 57 |
return predicted_label
|
| 58 |
|
| 59 |
+
class WomenHairStyleClassifier:
|
| 60 |
+
def __init__(self, model_path, class_names):
|
| 61 |
+
self.model = models.resnet18(pretrained=False)
|
| 62 |
+
num_ftrs = self.model.fc.in_features
|
| 63 |
+
self.model.fc = nn.Linear(num_ftrs, len(class_names))
|
| 64 |
+
self.load_model(model_path)
|
| 65 |
+
self.model.eval()
|
| 66 |
+
self.data_transforms = transforms.Compose([
|
| 67 |
+
transforms.Resize((224, 224)),
|
| 68 |
+
transforms.ToTensor(),
|
| 69 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 70 |
+
])
|
| 71 |
+
self.class_names = class_names
|
| 72 |
+
|
| 73 |
+
def preprocess_image(self, image_path):
|
| 74 |
+
image = Image.open(image_path).convert("RGB")
|
| 75 |
+
image = self.data_transforms(image)
|
| 76 |
+
image = image.unsqueeze(0)
|
| 77 |
+
return image
|
| 78 |
+
|
| 79 |
+
def load_model(self, model_path):
|
| 80 |
+
if torch.cuda.is_available():
|
| 81 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 82 |
+
else:
|
| 83 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 84 |
+
|
| 85 |
+
def classify_hairStyle(self, image_path):
|
| 86 |
+
input_image = self.preprocess_image(image_path)
|
| 87 |
+
|
| 88 |
+
with torch.no_grad():
|
| 89 |
+
predictions = self.model(input_image)
|
| 90 |
+
|
| 91 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 92 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 93 |
+
predicted_label = self.class_names[predicted_class]
|
| 94 |
+
|
| 95 |
+
return predicted_label
|
| 96 |
+
|
| 97 |
+
class WomenHairColorClassifier:
|
| 98 |
+
def __init__(self, model_path, class_names):
|
| 99 |
+
self.model = models.resnet18(pretrained=False)
|
| 100 |
+
num_ftrs = self.model.fc.in_features
|
| 101 |
+
self.model.fc = nn.Linear(num_ftrs, len(class_names))
|
| 102 |
+
self.load_model(model_path)
|
| 103 |
+
self.model.eval()
|
| 104 |
+
self.data_transforms = transforms.Compose([
|
| 105 |
+
transforms.Resize((224, 224)),
|
| 106 |
+
transforms.ToTensor(),
|
| 107 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 108 |
+
])
|
| 109 |
+
self.class_names = class_names
|
| 110 |
+
|
| 111 |
+
def preprocess_image(self, image_path):
|
| 112 |
+
image = Image.open(image_path).convert("RGB")
|
| 113 |
+
image = self.data_transforms(image)
|
| 114 |
+
image = image.unsqueeze(0)
|
| 115 |
+
return image
|
| 116 |
+
|
| 117 |
+
def load_model(self, model_path):
|
| 118 |
+
if torch.cuda.is_available():
|
| 119 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 120 |
+
else:
|
| 121 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 122 |
+
|
| 123 |
+
def classify_hairColor(self, image_path):
|
| 124 |
+
input_image = self.preprocess_image(image_path)
|
| 125 |
+
|
| 126 |
+
with torch.no_grad():
|
| 127 |
+
predictions = self.model(input_image)
|
| 128 |
+
|
| 129 |
+
probabilities = torch.nn.functional.softmax(predictions[0], dim=0)
|
| 130 |
+
predicted_class = torch.argmax(probabilities).item()
|
| 131 |
+
predicted_label = self.class_names[predicted_class]
|
| 132 |
+
|
| 133 |
+
return predicted_label
|
| 134 |
# Function to classify beard style
|
| 135 |
class BeardClassifier:
|
| 136 |
def __init__(self, model_path, class_names):
|
|
|
|
| 153 |
return image
|
| 154 |
|
| 155 |
def load_model(self, model_path):
|
| 156 |
+
if torch.cuda.is_available():
|
| 157 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 158 |
+
else:
|
| 159 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 160 |
|
| 161 |
def classify_beard(self, image):
|
| 162 |
input_image = self.preprocess_image(image)
|
|
|
|
| 189 |
return image
|
| 190 |
|
| 191 |
def load_model(self, model_path):
|
| 192 |
+
if torch.cuda.is_available():
|
| 193 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 194 |
+
else:
|
| 195 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 196 |
|
| 197 |
def classify_beard_color(self, image):
|
| 198 |
input_image = self.preprocess_image(image)
|
|
|
|
| 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):
|
|
|
|
| 225 |
return image
|
| 226 |
|
| 227 |
def load_model(self, model_path):
|
| 228 |
+
if torch.cuda.is_available():
|
| 229 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 230 |
+
else:
|
| 231 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 232 |
|
| 233 |
def classify_hair(self, image):
|
| 234 |
input_image = self.preprocess_image(image)
|
|
|
|
| 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)
|
| 245 |
+
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 246 |
+
placeholder_array = np.array(placeholder_image)
|
| 247 |
+
placeholder_width, placeholder_height = placeholder_image.size
|
| 248 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 249 |
+
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 250 |
+
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 251 |
background_array = np.array(background_image)
|
| 252 |
|
| 253 |
+
# Function to overlay a beard on a background image
|
| 254 |
+
def process_image_Beard(background_image, x, placeholder_image_path, x_coordinate, y_coordinate):
|
| 255 |
+
placeholder_image = Image.open(placeholder_image_path)
|
| 256 |
+
target_size = (x, x)
|
| 257 |
+
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
| 258 |
+
placeholder_array = np.array(placeholder_image)
|
| 259 |
+
placeholder_width, placeholder_height = placeholder_image.size
|
| 260 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + placeholder_width, y_coordinate + placeholder_height)
|
| 261 |
+
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 262 |
+
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 263 |
+
background_array = np.array(background_image)
|
| 264 |
+
placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 265 |
+
|
| 266 |
+
def process_image_WomanHair(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate):
|
| 267 |
placeholder_image = Image.open(placeholder_image_path)
|
| 268 |
target_size = (x, y)
|
| 269 |
placeholder_image = placeholder_image.resize(target_size, Image.LANCZOS)
|
|
|
|
| 273 |
placeholder_mask = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 274 |
background_image.paste(placeholder_image, region_box, mask=placeholder_mask)
|
| 275 |
background_array = np.array(background_image)
|
| 276 |
+
placeholder_alpha = placeholder_image.split()[3] if placeholder_image.mode == 'RGBA' else None
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def add_eyebrow(background_image, x_coordinate, y_coordinate, eyebrow_image_path):
|
| 280 |
+
eyebrow_image = Image.open(eyebrow_image_path)
|
| 281 |
+
target_size = (200, 200) # Adjust the size as needed
|
| 282 |
+
eyebrow_image = eyebrow_image.resize(target_size, Image.LANCZOS)
|
| 283 |
+
region_box = (x_coordinate, y_coordinate, x_coordinate + eyebrow_image.width, y_coordinate + eyebrow_image.height)
|
| 284 |
+
eyebrow_mask = eyebrow_image.split()[3] if eyebrow_image.mode == 'RGBA' else None
|
| 285 |
+
background_image.paste(eyebrow_image, region_box, mask=eyebrow_mask)
|
| 286 |
+
background_array = np.array(background_image)
|
| 287 |
+
|
| 288 |
|
| 289 |
|
| 290 |
|
|
|
|
| 303 |
|
| 304 |
# Function to generate Funko figurines
|
| 305 |
def generate_funko_figurines(input_image):
|
| 306 |
+
|
| 307 |
+
WomenHairStyle_classifier = WomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
|
| 308 |
+
predicted_WomenHairStyle = WomenHairStyle_classifier.classify_hairStyle(input_image)
|
| 309 |
+
|
| 310 |
+
WomenHairColor_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 311 |
+
predicted_WomenHairColor = WomenHairColor_classifier.classify_hairColor(input_image)
|
| 312 |
# Detect and classify gender
|
| 313 |
+
gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
|
|
|
|
| 314 |
predicted_gender = gender_classifier.classify_gender(input_image)
|
| 315 |
|
| 316 |
# Detect and classify beard style
|
|
|
|
| 403 |
x_coordinate = 90
|
| 404 |
y_coordinate = 50
|
| 405 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 406 |
+
if predicted_WomenHairStyle == 'MediumLength':
|
| 407 |
+
process_image_WomanHair(background_image, 300,460,
|
| 408 |
+
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_WomenHairColor}.png",
|
| 409 |
+
56, 50)
|
| 410 |
+
|
| 411 |
+
if predicted_WomenHairStyle == 'ShortHair':
|
| 412 |
+
process_image_WomanHair(background_image, 270,460,
|
| 413 |
+
f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{predicted_WomenHairColor}.png",
|
| 414 |
+
58, 52)
|
| 415 |
+
|
| 416 |
+
if predicted_WomenHairStyle == 'SidePlait':
|
| 417 |
+
process_image_WomanHair(background_image, 300,450,
|
| 418 |
+
f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{predicted_WomenHairColor}.png",
|
| 419 |
+
54, 56)
|
| 420 |
+
|
| 421 |
+
|
| 422 |
# Convert the resulting image to base64
|
| 423 |
buffered = BytesIO()
|
| 424 |
background_image.save(buffered, format="PNG")
|