import gradio as gr import stone import tempfile import os def get_primary_color(image): """ Detects the single most dominant skin color in the first face detected by selecting the cluster with the highest percentage. """ # Save uploaded image to a temp file tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False) image.save(tmp.name, format="PNG") tmp_path = tmp.name tmp.close() try: # Run stone to get dominant_colors result = stone.process(tmp_path, image_type="auto", return_report_image=False) except Exception as e: os.remove(tmp_path) return f"
Error: {e}
" finally: if os.path.exists(tmp_path): os.remove(tmp_path) faces = result.get("faces", []) if not faces: return "No face detected.
" # Get the list of (color, percent) for the first face doms = faces[0].get("dominant_colors", []) if not doms: return "No skin colors found.
" # Pick the one with max percent best = max(doms, key=lambda c: c.get("percent", 0)) hexcode = best.get("color") if not hexcode: return "No valid color found.
" # Render a single swatch + hex code swatch_html = ( f"