Spaces:
Running
Running
| import sys | |
| import os | |
| import tempfile | |
| import streamlit as st | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| st.set_page_config( | |
| page_title="More Close To Whom?", | |
| page_icon="π¨βπ©βπ§", | |
| layout="wide", | |
| ) | |
| st.title("π¨βπ©βπ§ More Close To Whom?") | |
| st.caption("Upload photos of father, mother, and child to discover who the child resembles more") | |
| # Backend selector | |
| backend_choice = st.radio( | |
| "ML Backend", | |
| ["π§ DeepFace", "β‘ InsightFace", "π€ HF Model"], | |
| horizontal=True, | |
| help="Select the AI model to use for face analysis", | |
| ) | |
| st.divider() | |
| # Upload row: Father | Child (center, wider) | Mother | |
| col_f, col_c, col_m = st.columns([1, 1.3, 1]) | |
| with col_f: | |
| st.markdown("#### π¨ Father") | |
| tab1, tab2 = st.tabs(["π Upload", "π· Camera"]) | |
| with tab1: | |
| upload = st.file_uploader( | |
| "Upload Father Image", | |
| type=["jpg", "jpeg", "png","webp"], | |
| key="father_upload", | |
| label_visibility="collapsed", | |
| ) | |
| with tab2: | |
| camera = st.camera_input( | |
| "Capture Father Image", | |
| key="father_camera", | |
| label_visibility="collapsed", | |
| ) | |
| father_file = camera if camera else upload | |
| if father_file: | |
| st.image(father_file, use_container_width=True) | |
| with col_c: | |
| st.markdown("#### π§ Child") | |
| tab1, tab2 = st.tabs(["π Upload", "π· Camera"]) | |
| with tab1: | |
| upload = st.file_uploader( | |
| "Upload child photo", | |
| type=["jpg", "jpeg", "png","webp"], | |
| key="child_upload", | |
| label_visibility="collapsed", | |
| ) | |
| with tab2: | |
| camera = st.camera_input( | |
| "Capture child photo", | |
| key="child_camera", | |
| label_visibility="collapsed", | |
| ) | |
| child_file = camera if camera else upload | |
| if child_file: | |
| st.image(child_file, use_container_width=True) | |
| with col_m: | |
| st.markdown("#### π© Mother") | |
| tab1, tab2 = st.tabs(["π Upload", "π· Camera"]) | |
| with tab1: | |
| upload = st.file_uploader( | |
| "Upload mother photo", | |
| type=["jpg", "jpeg", "png","webp"], | |
| key="mother_upload", | |
| label_visibility="collapsed", | |
| ) | |
| with tab2: | |
| camera = st.camera_input( | |
| "Capture mother photo", | |
| key="mother_camera", | |
| label_visibility="collapsed", | |
| ) | |
| mother_file = camera if camera else upload | |
| if mother_file: | |
| st.image(mother_file, use_container_width=True) | |
| st.divider() | |
| all_uploaded = all([father_file, child_file, mother_file]) | |
| if st.button("βΆ Analyze Resemblance", type="primary", disabled=not all_uploaded): | |
| with st.spinner("Analyzing faces β this may take a moment on first run..."): | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| def save(uploaded, name): | |
| path = os.path.join(tmpdir, name) | |
| with open(path, "wb") as f: | |
| f.write(uploaded.getvalue()) | |
| return path | |
| father_path = save(father_file, "father.jpg") | |
| mother_path = save(mother_file, "mother.jpg") | |
| child_path = save(child_file, "child.jpg") | |
| try: | |
| if "DeepFace" in backend_choice: | |
| from backends.deepface_backend import analyze | |
| elif "InsightFace" in backend_choice: | |
| from backends.insightface_backend import analyze | |
| else: | |
| from backends.hf_backend import analyze | |
| result = analyze(father_path, mother_path, child_path) | |
| st.success("Analysis complete!") | |
| r1, r2, r3 = st.columns(3) | |
| with r1: | |
| st.metric("π¨ Father resemblance", f"{result['father_score']}%") | |
| st.progress(result["father_score"] / 100) | |
| with r2: | |
| st.metric("π© Mother resemblance", f"{result['mother_score']}%") | |
| st.progress(result["mother_score"] / 100) | |
| with r3: | |
| st.metric("π§ Estimated child age", f"{result['age']} yrs") | |
| st.divider() | |
| diff = abs(result["father_score"] - result["mother_score"]) | |
| if diff < 2: | |
| st.info("βοΈ Child resembles both parents almost equally!") | |
| elif result["father_score"] >= result["mother_score"]: | |
| st.success(f"β Child looks more like **Father π¨** ({diff:.1f}% difference)") | |
| else: | |
| st.success(f"β Child looks more like **Mother π©** ({diff:.1f}% difference)") | |
| except ValueError as e: | |
| st.error(f"β οΈ {e}") | |
| except Exception as e: | |
| st.error(f"β Analysis failed: {e}") | |
| elif not all_uploaded: | |
| st.info("π Please upload photos of all three family members to begin") | |