Snehal19 commited on
Commit
5c582cd
·
verified ·
1 Parent(s): c878ba5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # app.py
2
-
3
  import gradio as gr
4
  import stone
5
  import tempfile
@@ -7,17 +5,17 @@ import os
7
 
8
  def get_primary_color(image):
9
  """
10
- Detects the most dominant skin color in the first face detected
11
- and returns a single color swatch plus its hex code.
12
  """
13
- # Save upload to temp file
14
  tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
15
  image.save(tmp.name, format="PNG")
16
  tmp_path = tmp.name
17
  tmp.close()
18
 
19
  try:
20
- # Run stone to get dominant colors
21
  result = stone.process(tmp_path, image_type="auto", return_report_image=False)
22
  except Exception as e:
23
  os.remove(tmp_path)
@@ -30,16 +28,18 @@ def get_primary_color(image):
30
  if not faces:
31
  return "<p>No face detected.</p>"
32
 
33
- # Grab the first dominant color of the first face
34
- dominant_colors = faces[0].get("dominant_colors", [])
35
- if not dominant_colors:
36
- return "<p>No skin color found.</p>"
37
 
38
- hexcode = dominant_colors[0].get("color")
 
 
39
  if not hexcode:
40
  return "<p>No valid color found.</p>"
41
 
42
- # Render a single swatch and show its hex code
43
  swatch_html = (
44
  f"<div style='display:flex; align-items:center; gap:8px;'>"
45
  f"<div style='background:{hexcode}; width:50px; height:50px; "
@@ -50,8 +50,8 @@ def get_primary_color(image):
50
  return swatch_html
51
 
52
  with gr.Blocks() as demo:
53
- gr.Markdown("## 🎯 Single Face Skin Color")
54
- gr.Markdown("Upload a portrait and get the single most dominant skin-tone color.")
55
  with gr.Row():
56
  inp = gr.Image(type="pil", label="Your Photo")
57
  btn = gr.Button("Detect Color")
 
 
 
1
  import gradio as gr
2
  import stone
3
  import tempfile
 
5
 
6
  def get_primary_color(image):
7
  """
8
+ Detects the single most dominant skin color in the first face detected
9
+ by selecting the cluster with the highest percentage.
10
  """
11
+ # Save uploaded image to a temp file
12
  tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
13
  image.save(tmp.name, format="PNG")
14
  tmp_path = tmp.name
15
  tmp.close()
16
 
17
  try:
18
+ # Run stone to get dominant_colors
19
  result = stone.process(tmp_path, image_type="auto", return_report_image=False)
20
  except Exception as e:
21
  os.remove(tmp_path)
 
28
  if not faces:
29
  return "<p>No face detected.</p>"
30
 
31
+ # Get the list of (color, percent) for the first face
32
+ doms = faces[0].get("dominant_colors", [])
33
+ if not doms:
34
+ return "<p>No skin colors found.</p>"
35
 
36
+ # Pick the one with max percent
37
+ best = max(doms, key=lambda c: c.get("percent", 0))
38
+ hexcode = best.get("color")
39
  if not hexcode:
40
  return "<p>No valid color found.</p>"
41
 
42
+ # Render a single swatch + hex code
43
  swatch_html = (
44
  f"<div style='display:flex; align-items:center; gap:8px;'>"
45
  f"<div style='background:{hexcode}; width:50px; height:50px; "
 
50
  return swatch_html
51
 
52
  with gr.Blocks() as demo:
53
+ gr.Markdown("## 🎯 Primary Face Skin Color")
54
+ gr.Markdown("Upload a portrait and get the **most dominant** skintone color.")
55
  with gr.Row():
56
  inp = gr.Image(type="pil", label="Your Photo")
57
  btn = gr.Button("Detect Color")