Snehal19 commited on
Commit
c878ba5
·
verified ·
1 Parent(s): 65e82e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -54
app.py CHANGED
@@ -1,77 +1,63 @@
 
 
1
  import gradio as gr
2
  import stone
3
- import tempfile, os
4
-
5
- def detect_and_palette(image):
6
- # 1) Save upload to a temp file
 
 
 
 
 
7
  tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
8
  image.save(tmp.name, format="PNG")
9
  tmp_path = tmp.name
10
  tmp.close()
11
 
12
  try:
13
- # 2) Run stone with return_report_image=True to get annotated face image
14
- result = stone.process(
15
- tmp_path,
16
- image_type="auto",
17
- return_report_image=True
18
- )
19
  except Exception as e:
20
- # Clean up and return error message
21
- if os.path.exists(tmp_path):
22
- os.remove(tmp_path)
23
- return None, f"<p style='color:red;'>Error processing image: {e}</p>"
24
  finally:
25
- # Always remove the temp file
26
  if os.path.exists(tmp_path):
27
  os.remove(tmp_path)
28
 
29
- # 3) Gather faces and report_images
30
  faces = result.get("faces", [])
31
- report_images = result.get("report_images", {})
32
-
33
- if not faces or not report_images:
34
- return None, "<p>No face detected.</p>"
35
-
36
- # 4) Pick the first annotated image
37
- if isinstance(report_images, dict):
38
- annotated_img = next(iter(report_images.values()))
39
- elif isinstance(report_images, list):
40
- annotated_img = report_images[0]
41
- else:
42
- annotated_img = report_images
43
-
44
- # 5) Extract dominant color hex codes
45
- dominant = faces[0].get("dominant_colors", [])
46
- colors = [c.get("color") for c in dominant if c.get("color")]
47
-
48
- # 6) Build HTML swatches
49
- swatches = "".join(
50
- f'<div style="background:{hexcode}; width:50px; height:50px; '
51
- 'display:inline-block; margin:2px; border-radius:4px;"></div>'
52
- for hexcode in colors
53
  )
54
- swatches_html = f"<div style='display:flex; flex-wrap:wrap; gap:4px;'>{swatches}</div>"
55
-
56
- return annotated_img, swatches_html
57
 
58
- # 7) Build Gradio interface
59
  with gr.Blocks() as demo:
60
- gr.Markdown("## 🤳 Face‑Based Skin Tone Palette")
61
- gr.Markdown("Upload a clear portrait to detect your face and extract your skintone palette.")
62
-
63
  with gr.Row():
64
  inp = gr.Image(type="pil", label="Your Photo")
65
- btn = gr.Button("Analyze")
66
-
67
- img_out = gr.Image(type="pil", label="Detected Face Region")
68
- swatches_out = gr.HTML(label="Skin‑Tone Palette")
69
 
70
- btn.click(
71
- fn=detect_and_palette,
72
- inputs=inp,
73
- outputs=[img_out, swatches_out]
74
- )
75
 
76
  if __name__ == "__main__":
77
  demo.launch()
 
1
+ # app.py
2
+
3
  import gradio as gr
4
  import stone
5
+ import tempfile
6
+ 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)
24
+ return f"<p style='color:red;'>Error: {e}</p>"
 
 
25
  finally:
 
26
  if os.path.exists(tmp_path):
27
  os.remove(tmp_path)
28
 
 
29
  faces = result.get("faces", [])
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; "
46
+ "border-radius:4px; border:1px solid #ccc;'></div>"
47
+ f"<span style='font-family:monospace; font-size:1.2em;'>{hexcode}</span>"
48
+ "</div>"
 
 
 
49
  )
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")
58
+ out = gr.HTML(label="Primary Skin Color")
 
 
59
 
60
+ btn.click(fn=get_primary_color, inputs=inp, outputs=out)
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
  demo.launch()