vaniv commited on
Commit
6c8becc
·
verified ·
1 Parent(s): 097d0b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -17,9 +17,13 @@ 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
 
@@ -33,31 +37,37 @@ def get_dominant_color(image):
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
  complementary_color = closest_color((r, g, b))
41
- complementary_palette = []
42
- # Get shades or tones of the complementary color
43
- complementary_colors = get_monochromatic_palette(complementary_color)
44
- for color in complementary_colors:
45
- if closest_color(color) != complementary_color:
46
- complementary_palette.append(closest_color(color))
47
 
48
- return complementary_color, complementary_palette
49
-
50
- def get_monochromatic_palette(color_name, num_shades=3):
51
- rgb_color = mcolors.CSS4_COLORS[color_name]
52
- r, g, b = mcolors.hex2color(rgb_color)
53
- hsv_color = colorsys.rgb_to_hsv(r, g, b)
54
- hsv_colors = [hsv_color]
55
- for i in range(1, num_shades):
56
- hsv_colors.append((hsv_color[0], hsv_color[1], hsv_color[2] * (1 - 0.2 * i)))
57
- hex_palette = [mcolors.rgb2hex(colorsys.hsv_to_rgb(*hsv)) for hsv in hsv_colors]
58
- named_palette = [closest_color(mcolors.hex2color(hex_color)) for hex_color in hex_palette]
59
 
60
- return named_palette
 
 
 
 
 
61
 
62
  def get_outfit_recommendation(pred_class):
63
  if pred_class == 'top':
@@ -74,7 +84,7 @@ def predict(image):
74
  garment_recommendation = get_outfit_recommendation(pred_class)
75
 
76
  # Construct output string
77
- output = f"For your {pred_class}, consider pairing it with a {garment_recommendation.lower()} in {complementary_color}, {', '.join(complementary_palette)}"
78
 
79
  return output # Return the formatted output
80
 
@@ -91,4 +101,4 @@ interface = gr.Interface(
91
  description="Upload an image of jeans or a top to get a recommendation for a complementary outfit based on fashion theory."
92
  )
93
 
94
- interface.launch()
 
17
  min_colors = {}
18
  for key, name in mcolors.CSS4_COLORS.items():
19
  r_c, g_c, b_c = mcolors.hex2color(name)
20
+ if isinstance(requested_color, str):
21
+ r_r, g_r, b_r = mcolors.hex2color(requested_color)
22
+ else:
23
+ r_r, g_r, b_r = requested_color
24
+ rd = (r_c - r_r) ** 2
25
+ gd = (g_c - g_r) ** 2
26
+ bd = (b_c - b_r) ** 2
27
  min_colors[(rd + gd + bd)] = key
28
  return min_colors[min(min_colors.keys())]
29
 
 
37
  dominant_color = tuple(c / 255 for c in dominant_color) # Normalize RGB values to [0, 1]
38
  return dominant_color
39
 
40
+ def get_monochromatic_palette(color_name, num_colors=5):
41
+ rgb_color = mcolors.to_rgb(mcolors.CSS4_COLORS[color_name])
42
+ h, s, v = colorsys.rgb_to_hsv(*rgb_color)
43
+
44
+ palette = []
45
+ for i in range(num_colors):
46
+ # Vary both saturation and value
47
+ new_s = max(0, min(1, s + (i - num_colors // 2) * 0.1))
48
+ new_v = max(0, min(1, v + (i - num_colors // 2) * 0.1))
49
+
50
+ new_rgb = colorsys.hsv_to_rgb(h, new_s, new_v)
51
+ palette.append(closest_color(new_rgb))
52
+
53
+ # Remove duplicates while preserving order
54
+ return list(dict.fromkeys(palette))
55
+
56
  def get_complementary_color(rgb_color):
57
  h, s, v = colorsys.rgb_to_hsv(*rgb_color)
58
  complementary_h = (h + 0.5) % 1.0
59
  r, g, b = colorsys.hsv_to_rgb(complementary_h, s, v)
60
  complementary_color = closest_color((r, g, b))
 
 
 
 
 
 
61
 
62
+ # Get monochromatic palette of the complementary color
63
+ complementary_palette = get_monochromatic_palette(complementary_color)
 
 
 
 
 
 
 
 
 
64
 
65
+ # Ensure the main complementary color is first in the list
66
+ if complementary_color in complementary_palette:
67
+ complementary_palette.remove(complementary_color)
68
+ complementary_palette.insert(0, complementary_color)
69
+
70
+ return complementary_color, complementary_palette
71
 
72
  def get_outfit_recommendation(pred_class):
73
  if pred_class == 'top':
 
84
  garment_recommendation = get_outfit_recommendation(pred_class)
85
 
86
  # Construct output string
87
+ output = f"For your {pred_class}, consider pairing it with a {garment_recommendation.lower()} in {', '.join(complementary_palette)}"
88
 
89
  return output # Return the formatted output
90
 
 
101
  description="Upload an image of jeans or a top to get a recommendation for a complementary outfit based on fashion theory."
102
  )
103
 
104
+ interface.launch(share=True)