Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,10 @@ import tempfile, os
|
|
| 4 |
|
| 5 |
def detect_and_palette(image):
|
| 6 |
# 1) Save upload to a temp file
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
try:
|
| 12 |
# 2) Run stone with return_report_image=True to get annotated face image
|
|
@@ -15,90 +16,46 @@ def detect_and_palette(image):
|
|
| 15 |
image_type="auto",
|
| 16 |
return_report_image=True
|
| 17 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
finally:
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
faces = result.get("faces", [])
|
| 22 |
-
|
| 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 |
-
|
| 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 |
-
#
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 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;'>{
|
| 97 |
|
| 98 |
-
# Return the annotated face image + HTML swatches
|
| 99 |
return annotated_img, swatches_html
|
| 100 |
|
| 101 |
-
# Build
|
| 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.")
|
|
|
|
| 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
|
|
|
|
| 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 skin‑tone palette.")
|