Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import stone
|
| 3 |
-
import tempfile
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 13 |
result = stone.process(tmp_path, image_type="auto", return_report_image=False)
|
| 14 |
finally:
|
| 15 |
os.remove(tmp_path)
|
|
@@ -18,23 +23,27 @@ def get_colors(image):
|
|
| 18 |
if not faces:
|
| 19 |
return "<p>No face detected.</p>"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
colors = [c["color"] for c in faces[0]["dominant_colors"]]
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
'display:inline-block; margin:2px; border-radius:4px;"></div>'
|
| 27 |
-
for
|
| 28 |
)
|
| 29 |
-
return
|
| 30 |
|
|
|
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
-
gr.Markdown("#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
btn.click(fn=
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import stone
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def get_palette(image):
|
| 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 the stone classifier
|
| 18 |
result = stone.process(tmp_path, image_type="auto", return_report_image=False)
|
| 19 |
finally:
|
| 20 |
os.remove(tmp_path)
|
|
|
|
| 23 |
if not faces:
|
| 24 |
return "<p>No face detected.</p>"
|
| 25 |
|
| 26 |
+
# Extract the hex codes of the dominant colors
|
| 27 |
colors = [c["color"] for c in faces[0]["dominant_colors"]]
|
| 28 |
+
|
| 29 |
+
# Build HTML swatches
|
| 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 |
+
return f"<div style='display:flex; flex-wrap:wrap;'>{swatches_html}</div>"
|
| 36 |
|
| 37 |
+
# Define the Gradio interface
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("# 🎨 Skin Color Palette API")
|
| 40 |
+
gr.Markdown("Upload a clear portrait to extract a palette of your skin's dominant tones.")
|
| 41 |
+
with gr.Row():
|
| 42 |
+
inp = gr.Image(type="pil", label="Your Photo")
|
| 43 |
+
btn = gr.Button("Generate Palette")
|
| 44 |
+
out = gr.HTML(label="Color Palette")
|
| 45 |
|
| 46 |
+
btn.click(fn=get_palette, inputs=inp, outputs=out)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|