Snehal19 commited on
Commit
456f198
·
verified ·
1 Parent(s): 50ddab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -1,15 +1,20 @@
1
  import gradio as gr
2
  import stone
3
- import tempfile, os
 
4
 
5
- def get_colors(image):
6
- # 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
- # process and grab faces
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
- # extract hex codes of dominant colors
22
  colors = [c["color"] for c in faces[0]["dominant_colors"]]
23
- # render as inline swatches
24
- swatches = "".join(
25
- f'<div style="background:{c}; width:30px; height:20px; '
 
26
  'display:inline-block; margin:2px; border-radius:4px;"></div>'
27
- for c in colors
28
  )
29
- return swatches
30
 
 
31
  with gr.Blocks() as demo:
32
- gr.Markdown("# Dominant SkinColor Swatches")
33
- img = gr.Image(type="pil", label="Upload Photo")
34
- btn = gr.Button("Get Colors")
35
- out = gr.HTML(label="Colors")
 
 
36
 
37
- btn.click(fn=get_colors, inputs=img, outputs=out)
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()