Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,120 @@
|
|
1 |
import gradio as gr
|
2 |
import stone
|
3 |
-
import tempfile
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
"""
|
8 |
-
Detects dominant skin colors in the uploaded image
|
9 |
-
and returns an HTML row of color swatches.
|
10 |
-
"""
|
11 |
-
# Save the upload to a temp file
|
12 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
13 |
image.save(tmp, format="PNG")
|
14 |
tmp_path = tmp.name
|
15 |
|
16 |
try:
|
17 |
-
# Run
|
18 |
-
result = stone.process(
|
|
|
|
|
|
|
|
|
19 |
finally:
|
20 |
os.remove(tmp_path)
|
21 |
|
22 |
faces = result.get("faces", [])
|
23 |
if not faces:
|
24 |
-
return "<p>No face detected.</p>"
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
# Extract
|
27 |
colors = [c["color"] for c in faces[0]["dominant_colors"]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
swatches_html = "".join(
|
31 |
f'<div style="background:{hexcode}; width:50px; height:50px; '
|
32 |
'display:inline-block; margin:2px; border-radius:4px;"></div>'
|
33 |
for hexcode in colors
|
34 |
)
|
35 |
-
|
36 |
|
37 |
-
#
|
|
|
|
|
|
|
38 |
with gr.Blocks() as demo:
|
39 |
-
gr.Markdown("
|
40 |
-
gr.Markdown("Upload a clear portrait to
|
|
|
41 |
with gr.Row():
|
42 |
inp = gr.Image(type="pil", label="Your Photo")
|
43 |
-
btn = gr.Button("
|
44 |
-
out = gr.HTML(label="Color Palette")
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
demo.launch()
|
|
|
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 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
8 |
image.save(tmp, format="PNG")
|
9 |
tmp_path = tmp.name
|
10 |
|
11 |
try:
|
12 |
+
# 2) Run stone with return_report_image=True to get annotated face image
|
13 |
+
result = stone.process(
|
14 |
+
tmp_path,
|
15 |
+
image_type="auto",
|
16 |
+
return_report_image=True
|
17 |
+
)
|
18 |
finally:
|
19 |
os.remove(tmp_path)
|
20 |
|
21 |
faces = result.get("faces", [])
|
22 |
if not faces:
|
23 |
+
return None, "<p>No face detected.</p>"
|
24 |
+
|
25 |
+
# 3) Grab the first annotated image (PIL.Image)
|
26 |
+
report_images = result.get("report_images", [])
|
27 |
+
annotated_img = report_images[0]
|
28 |
|
29 |
+
# 4) Extract dominant skin-tone hex codes
|
30 |
colors = [c["color"] for c in faces[0]["dominant_colors"]]
|
31 |
+
swatches_html = "".join(
|
32 |
+
f'<div style="background:{hexcode}; width:50px; height:50px; '
|
33 |
+
'display:inline-block; margin:2px; border-radius:4px;"></div>'
|
34 |
+
for hexcode in colors
|
35 |
+
)
|
36 |
+
swatches_html = f"<div style='display:flex; flex-wrap:wrap;'>{swatches_html}</div>"
|
37 |
+
|
38 |
+
# Return the annotated face image + HTML swatches
|
39 |
+
return annotated_img, swatches_html
|
40 |
+
|
41 |
+
# Build the Gradio interface
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
gr.Markdown("## 🤳 Face‑Based Skin Tone Palette")
|
44 |
+
gr.Markdown("Upload a clear portrait to detect your face and extract your skin‑tone palette.")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
inp = gr.Image(type="pil", label="Your Photo")
|
48 |
+
btn = gr.Button("Analyze")
|
49 |
+
|
50 |
+
img_out = gr.Image(type="pil", label="Detected Face Region")
|
51 |
+
swatches_out = gr.HTML(label="Skin‑Tone Palette")
|
52 |
+
|
53 |
+
btn.click(
|
54 |
+
fn=detect_and_palette,
|
55 |
+
inputs=inp,
|
56 |
+
outputs=[img_out, swatches_out]
|
57 |
+
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
demo.launch()
|
61 |
+
import gradio as gr
|
62 |
+
import stone
|
63 |
+
import tempfile, os
|
64 |
+
|
65 |
+
def detect_and_palette(image):
|
66 |
+
# 1) Save upload to a temp file
|
67 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
68 |
+
image.save(tmp, format="PNG")
|
69 |
+
tmp_path = tmp.name
|
70 |
+
|
71 |
+
try:
|
72 |
+
# 2) Run stone with return_report_image=True to get annotated face image
|
73 |
+
result = stone.process(
|
74 |
+
tmp_path,
|
75 |
+
image_type="auto",
|
76 |
+
return_report_image=True
|
77 |
+
)
|
78 |
+
finally:
|
79 |
+
os.remove(tmp_path)
|
80 |
|
81 |
+
faces = result.get("faces", [])
|
82 |
+
if not faces:
|
83 |
+
return None, "<p>No face detected.</p>"
|
84 |
+
|
85 |
+
# 3) Grab the first annotated image (PIL.Image)
|
86 |
+
report_images = result.get("report_images", [])
|
87 |
+
annotated_img = report_images[0]
|
88 |
+
|
89 |
+
# 4) Extract dominant skin-tone hex codes
|
90 |
+
colors = [c["color"] for c in faces[0]["dominant_colors"]]
|
91 |
swatches_html = "".join(
|
92 |
f'<div style="background:{hexcode}; width:50px; height:50px; '
|
93 |
'display:inline-block; margin:2px; border-radius:4px;"></div>'
|
94 |
for hexcode in colors
|
95 |
)
|
96 |
+
swatches_html = f"<div style='display:flex; flex-wrap:wrap;'>{swatches_html}</div>"
|
97 |
|
98 |
+
# Return the annotated face image + HTML swatches
|
99 |
+
return annotated_img, swatches_html
|
100 |
+
|
101 |
+
# Build the Gradio interface
|
102 |
with gr.Blocks() as demo:
|
103 |
+
gr.Markdown("## 🤳 Face‑Based Skin Tone Palette")
|
104 |
+
gr.Markdown("Upload a clear portrait to detect your face and extract your skin‑tone palette.")
|
105 |
+
|
106 |
with gr.Row():
|
107 |
inp = gr.Image(type="pil", label="Your Photo")
|
108 |
+
btn = gr.Button("Analyze")
|
|
|
109 |
|
110 |
+
img_out = gr.Image(type="pil", label="Detected Face Region")
|
111 |
+
swatches_out = gr.HTML(label="Skin‑Tone Palette")
|
112 |
+
|
113 |
+
btn.click(
|
114 |
+
fn=detect_and_palette,
|
115 |
+
inputs=inp,
|
116 |
+
outputs=[img_out, swatches_out]
|
117 |
+
)
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
demo.launch()
|