vaniv commited on
Commit
9ef7617
·
verified ·
1 Parent(s): c5b0376

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -71
app.py CHANGED
@@ -1,71 +1,71 @@
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
- import io
7
- import colorsys
8
-
9
- # Load the model
10
- learn = load_learner('outfit_recommender_resnet18.pkl')
11
-
12
- # Define class names
13
- class_names = ['jeans', 'top']
14
-
15
- # Helper function to get the name of the closest color
16
- def closest_color(requested_color):
17
- min_colors = {}
18
- for key, name in mcolors.CSS4_COLORS.items():
19
- r_c, g_c, b_c = mcolors.hex2color(name)
20
- rd = (r_c - requested_color[0]) ** 2
21
- gd = (g_c - requested_color[1]) ** 2
22
- bd = (b_c - requested_color[2]) ** 2
23
- min_colors[(rd + gd + bd)] = key
24
- return min_colors[min(min_colors.keys())]
25
-
26
- def get_dominant_color(image):
27
- img_byte_arr = io.BytesIO()
28
- image.save(img_byte_arr, format='PNG')
29
- img_byte_arr = img_byte_arr.getvalue()
30
-
31
- color_thief = ColorThief(io.BytesIO(img_byte_arr))
32
- dominant_color = color_thief.get_color(quality=1)
33
- dominant_color = tuple(c / 255 for c in dominant_color) # Normalize RGB values to [0, 1]
34
- return dominant_color
35
-
36
- def get_complementary_color(rgb_color):
37
- h, s, v = colorsys.rgb_to_hsv(*rgb_color)
38
- complementary_h = (h + 0.5) % 1.0
39
- r, g, b = colorsys.hsv_to_rgb(complementary_h, s, v)
40
- return closest_color((r, g, b))
41
-
42
- def get_outfit_recommendation(pred_class):
43
- if pred_class == 'top':
44
- return 'Jeans'
45
- elif pred_class == 'jeans':
46
- return 'Top'
47
- else:
48
- return 'Item'
49
-
50
- def predict(image):
51
- pred_class, pred_idx, outputs = learn.predict(image)
52
- pred_class = class_names[pred_idx] # Convert index to class name
53
- dominant_color = get_dominant_color(image)
54
- complementary_color = get_complementary_color(dominant_color)
55
- garment_recommendation = get_outfit_recommendation(pred_class)
56
- return f"Complementary item: {garment_recommendation} in {complementary_color}"
57
-
58
- def gradio_predict(image):
59
- if isinstance(image, np.ndarray):
60
- image = Image.fromarray(image.astype('uint8'), 'RGB')
61
- return predict(image)
62
-
63
- interface = gr.Interface(
64
- fn=gradio_predict,
65
- inputs=gr.Image(),
66
- outputs="text",
67
- title="Outfit Recommender",
68
- description="Upload an image of a jeans or a top to get a recommendation for a complementary outfit based on fashion theory."
69
- )
70
-
71
- 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
+ import io
7
+ import colorsys
8
+
9
+ # Load the model
10
+ learn = load_learner('outfit_recommender_resnet18.pkl')
11
+
12
+ # Define class names
13
+ class_names = ['jeans', 'top']
14
+
15
+ # Helper function to get the name of the closest color
16
+ def closest_color(requested_color):
17
+ min_colors = {}
18
+ for key, name in mcolors.CSS4_COLORS.items():
19
+ r_c, g_c, b_c = mcolors.hex2color(name)
20
+ rd = (r_c - requested_color[0]) ** 2
21
+ gd = (g_c - requested_color[1]) ** 2
22
+ bd = (b_c - requested_color[2]) ** 2
23
+ min_colors[(rd + gd + bd)] = key
24
+ return min_colors[min(min_colors.keys())]
25
+
26
+ def get_dominant_color(image):
27
+ img_byte_arr = io.BytesIO()
28
+ image.save(img_byte_arr, format='PNG')
29
+ img_byte_arr = img_byte_arr.getvalue()
30
+
31
+ color_thief = ColorThief(io.BytesIO(img_byte_arr))
32
+ dominant_color = color_thief.get_color(quality=1)
33
+ dominant_color = tuple(c / 255 for c in dominant_color) # Normalize RGB values to [0, 1]
34
+ return dominant_color
35
+
36
+ def get_complementary_color(rgb_color):
37
+ h, s, v = colorsys.rgb_to_hsv(*rgb_color)
38
+ complementary_h = (h + 0.5) % 1.0
39
+ r, g, b = colorsys.hsv_to_rgb(complementary_h, s, v)
40
+ return closest_color((r, g, b))
41
+
42
+ def get_outfit_recommendation(pred_class):
43
+ if pred_class == 'top':
44
+ return 'Jeans'
45
+ elif pred_class == 'jeans':
46
+ return 'Top'
47
+ else:
48
+ return 'Item'
49
+
50
+ def predict(image):
51
+ pred_class, _, _ = learn.predict(image)
52
+ pred_class = class_names[pred_idx] # Convert index to class name
53
+ dominant_color = get_dominant_color(image)
54
+ complementary_color = get_complementary_color(dominant_color)
55
+ garment_recommendation = get_outfit_recommendation(pred_class)
56
+ return f"Complementary item: {garment_recommendation} in {complementary_color}"
57
+
58
+ def gradio_predict(image):
59
+ if isinstance(image, np.ndarray):
60
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
61
+ return predict(image)
62
+
63
+ interface = gr.Interface(
64
+ fn=gradio_predict,
65
+ inputs=gr.Image(),
66
+ outputs="text",
67
+ title="Outfit Recommender",
68
+ description="Upload an image of a jeans or a top to get a recommendation for a complementary outfit based on fashion theory."
69
+ )
70
+
71
+ interface.launch()