ParentCloseTesting / src /streamlit_app.py
Prince-1's picture
Upload 2 files
4649692 verified
Raw
History Blame Contribute Delete
5.13 kB
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")