Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,79 @@
|
|
| 1 |
-
from fastai.vision.all import *
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from colorthief import ColorThief
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import matplotlib.colors as mcolors
|
| 6 |
-
|
| 7 |
-
# Load the model
|
| 8 |
-
learn = load_learner(
|
| 9 |
-
|
| 10 |
-
# Define class names
|
| 11 |
-
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
|
| 12 |
-
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
|
| 13 |
-
|
| 14 |
-
# Define basic color wheel for complementary colors
|
| 15 |
-
color_wheel = {
|
| 16 |
-
'red': 'cyan',
|
| 17 |
-
'green': 'magenta',
|
| 18 |
-
'blue': 'yellow',
|
| 19 |
-
'yellow': 'blue',
|
| 20 |
-
'cyan': 'red',
|
| 21 |
-
'magenta': 'green'
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
# Helper function to get the name of the closest color
|
| 25 |
-
def closest_color(requested_color):
|
| 26 |
-
min_colors = {}
|
| 27 |
-
for key, name in mcolors.CSS4_COLORS.items():
|
| 28 |
-
r_c, g_c, b_c = mcolors.hex2color(name)
|
| 29 |
-
rd = (r_c - requested_color[0]) ** 2
|
| 30 |
-
gd = (g_c - requested_color[1]) ** 2
|
| 31 |
-
bd = (b_c - requested_color[2]) ** 2
|
| 32 |
-
min_colors[(rd + gd + bd)] = key
|
| 33 |
-
return min_colors[min(min_colors.keys())]
|
| 34 |
-
|
| 35 |
-
# Extract dominant color from the image
|
| 36 |
-
def get_dominant_color(image):
|
| 37 |
-
color_thief = ColorThief(image)
|
| 38 |
-
dominant_color = color_thief.get_color(quality=1)
|
| 39 |
-
dominant_color = tuple(c / 255 for c in dominant_color) # Normalize RGB values to [0, 1]
|
| 40 |
-
return closest_color(dominant_color)
|
| 41 |
-
|
| 42 |
-
# Enhanced recommendation logic based on garment type and color
|
| 43 |
-
def get_outfit_recommendation(pred_class, color):
|
| 44 |
-
garment_recommendation = ''
|
| 45 |
-
color_recommendation = color_wheel.get(color, 'color')
|
| 46 |
-
|
| 47 |
-
if pred_class in ['T-shirt/top', 'Pullover', 'Shirt']:
|
| 48 |
-
garment_recommendation = 'Trouser'
|
| 49 |
-
elif pred_class == 'Trouser':
|
| 50 |
-
garment_recommendation = 'T-shirt/top'
|
| 51 |
-
elif pred_class in ['Dress', 'Coat']:
|
| 52 |
-
garment_recommendation = 'Sandal'
|
| 53 |
-
elif pred_class in ['Sandal', 'Sneaker', 'Ankle boot']:
|
| 54 |
-
garment_recommendation = 'Trouser'
|
| 55 |
-
else:
|
| 56 |
-
garment_recommendation = 'T-shirt/top'
|
| 57 |
-
|
| 58 |
-
return garment_recommendation, color_recommendation
|
| 59 |
-
|
| 60 |
-
def predict(image):
|
| 61 |
-
pred_class, pred_idx, outputs = learn.predict(image)
|
| 62 |
-
dominant_color = get_dominant_color(image)
|
| 63 |
-
garment_recommendation, color_recommendation = get_outfit_recommendation(pred_class, dominant_color)
|
| 64 |
-
return f"Predicted category: {pred_class}\nRecommended complementary item: {garment_recommendation} in {color_recommendation}"
|
| 65 |
-
|
| 66 |
-
# Define the Gradio interface
|
| 67 |
-
def gradio_predict(image):
|
| 68 |
-
image = Image.open(image).convert('RGB')
|
| 69 |
-
return predict(image)
|
| 70 |
-
|
| 71 |
-
interface = gr.Interface(
|
| 72 |
-
fn=gradio_predict,
|
| 73 |
-
inputs=gr.inputs.Image(shape=(224, 224)),
|
| 74 |
-
outputs="text",
|
| 75 |
-
title="Outfit Recommender",
|
| 76 |
-
description="Upload an image of a garment to get a recommendation for a complementary outfit based on fashion theory."
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
interface.launch()
|
|
|
|
| 1 |
+
from fastai.vision.all import *
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from colorthief import ColorThief
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import matplotlib.colors as mcolors
|
| 6 |
+
|
| 7 |
+
# Load the model
|
| 8 |
+
learn = load_learner('fashion_mnist_resnet18.pkl')
|
| 9 |
+
|
| 10 |
+
# Define class names
|
| 11 |
+
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
|
| 12 |
+
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
|
| 13 |
+
|
| 14 |
+
# Define basic color wheel for complementary colors
|
| 15 |
+
color_wheel = {
|
| 16 |
+
'red': 'cyan',
|
| 17 |
+
'green': 'magenta',
|
| 18 |
+
'blue': 'yellow',
|
| 19 |
+
'yellow': 'blue',
|
| 20 |
+
'cyan': 'red',
|
| 21 |
+
'magenta': 'green'
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Helper function to get the name of the closest color
|
| 25 |
+
def closest_color(requested_color):
|
| 26 |
+
min_colors = {}
|
| 27 |
+
for key, name in mcolors.CSS4_COLORS.items():
|
| 28 |
+
r_c, g_c, b_c = mcolors.hex2color(name)
|
| 29 |
+
rd = (r_c - requested_color[0]) ** 2
|
| 30 |
+
gd = (g_c - requested_color[1]) ** 2
|
| 31 |
+
bd = (b_c - requested_color[2]) ** 2
|
| 32 |
+
min_colors[(rd + gd + bd)] = key
|
| 33 |
+
return min_colors[min(min_colors.keys())]
|
| 34 |
+
|
| 35 |
+
# Extract dominant color from the image
|
| 36 |
+
def get_dominant_color(image):
|
| 37 |
+
color_thief = ColorThief(image)
|
| 38 |
+
dominant_color = color_thief.get_color(quality=1)
|
| 39 |
+
dominant_color = tuple(c / 255 for c in dominant_color) # Normalize RGB values to [0, 1]
|
| 40 |
+
return closest_color(dominant_color)
|
| 41 |
+
|
| 42 |
+
# Enhanced recommendation logic based on garment type and color
|
| 43 |
+
def get_outfit_recommendation(pred_class, color):
|
| 44 |
+
garment_recommendation = ''
|
| 45 |
+
color_recommendation = color_wheel.get(color, 'color')
|
| 46 |
+
|
| 47 |
+
if pred_class in ['T-shirt/top', 'Pullover', 'Shirt']:
|
| 48 |
+
garment_recommendation = 'Trouser'
|
| 49 |
+
elif pred_class == 'Trouser':
|
| 50 |
+
garment_recommendation = 'T-shirt/top'
|
| 51 |
+
elif pred_class in ['Dress', 'Coat']:
|
| 52 |
+
garment_recommendation = 'Sandal'
|
| 53 |
+
elif pred_class in ['Sandal', 'Sneaker', 'Ankle boot']:
|
| 54 |
+
garment_recommendation = 'Trouser'
|
| 55 |
+
else:
|
| 56 |
+
garment_recommendation = 'T-shirt/top'
|
| 57 |
+
|
| 58 |
+
return garment_recommendation, color_recommendation
|
| 59 |
+
|
| 60 |
+
def predict(image):
|
| 61 |
+
pred_class, pred_idx, outputs = learn.predict(image)
|
| 62 |
+
dominant_color = get_dominant_color(image)
|
| 63 |
+
garment_recommendation, color_recommendation = get_outfit_recommendation(pred_class, dominant_color)
|
| 64 |
+
return f"Predicted category: {pred_class}\nRecommended complementary item: {garment_recommendation} in {color_recommendation}"
|
| 65 |
+
|
| 66 |
+
# Define the Gradio interface
|
| 67 |
+
def gradio_predict(image):
|
| 68 |
+
image = Image.open(image).convert('RGB')
|
| 69 |
+
return predict(image)
|
| 70 |
+
|
| 71 |
+
interface = gr.Interface(
|
| 72 |
+
fn=gradio_predict,
|
| 73 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
| 74 |
+
outputs="text",
|
| 75 |
+
title="Outfit Recommender",
|
| 76 |
+
description="Upload an image of a garment to get a recommendation for a complementary outfit based on fashion theory."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
interface.launch()
|