File size: 1,645 Bytes
e1930e6
2ca8ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87964f4
2ca8ac1
 
 
 
 
 
 
 
 
08819d8
2ca8ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import numpy as np
from keras.models import load_model

def getAge(distr):
    distr = distr * 4
    if 0.65 <= distr <= 1.4:
        return "0-18"
    elif 1.65 <= distr <= 2.4:
        return "19-30"
    elif 2.65 <= distr <= 3.4:
        return "31-80"
    elif 3.65 <= distr <= 4.4:
        return "80 +"
    return "Unknown"

def getGender(prob):
    return "Male" if prob < 0.5 else "Female"

def getAgeGender(image_path):
    # Loading the uploaded Image:
    image = cv2.imread(image_path, 0)
    image = cv2.resize(image, dsize=(64, 64))
    image = image.reshape((image.shape[0], image.shape[1], 1))

    # Loading the trained model:
    model = load_model('data_old.h5')

    # Getting the predictions:
    image = image / 255
    val = model.predict(np.array([image]))
    age = getAge(val[0])
    gender = getGender(val[1])
    return age, gender

def main():
    st.title("Age and Gender Prediction")

    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

    if uploaded_file is not None:
        # Save the uploaded file to a temporary location
        temp_image_path = "temp_image.jpg"
        with open(temp_image_path, "wb") as f:
            f.write(uploaded_file.getvalue())

        # Get age and gender predictions
        age, gender = getAgeGender(temp_image_path)

        # Display the uploaded image
        st.image(temp_image_path, caption="Uploaded Image", use_column_width=True)

        # Display the predictions
        st.write(f"Predicted Age: {age}")
        st.write(f"Predicted Gender: {gender}")

if __name__ == "__main__":
    main()