File size: 5,132 Bytes
4649692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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")