Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,63 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import stone
|
3 |
-
import tempfile
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
14 |
-
result = stone.process(
|
15 |
-
tmp_path,
|
16 |
-
image_type="auto",
|
17 |
-
return_report_image=True
|
18 |
-
)
|
19 |
except Exception as e:
|
20 |
-
|
21 |
-
|
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 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
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 |
-
|
55 |
-
|
56 |
-
return annotated_img, swatches_html
|
57 |
|
58 |
-
# 7) Build Gradio interface
|
59 |
with gr.Blocks() as demo:
|
60 |
-
gr.Markdown("##
|
61 |
-
gr.Markdown("Upload a
|
62 |
-
|
63 |
with gr.Row():
|
64 |
inp = gr.Image(type="pil", label="Your Photo")
|
65 |
-
btn = gr.Button("
|
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()
|