File size: 1,875 Bytes
1073af4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53602c4
1073af4
34d9e47
53602c4
1073af4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d8ba66
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
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np

# Load the model
model = load_model('model.h5', compile=False)

# Image preprocessing function
def process_image(img):
    # Resize, normalize, and add batch dimension
    img = img.resize((128, 128))
    img = np.array(img)
    img = img / 255.0  # Normalize pixel values
    img = np.expand_dims(img, axis=0)  # Add batch dimension (1, 128, 128, 3)
    return img

# App title
st.title('Age Prediction from Image')
st.write("Upload a photo to predict the subject's age.")

# Sidebar instructions
st.sidebar.header("Instructions")
st.sidebar.write("""
1. Upload a clear face photo
2. Model will analyze facial features
3. Predicted age appears below image
4. This dataset contains images of people generated by Stable Diffusion.
For each image, you are to determine how old the person is.
Ages will range between 20 and 90 years old.
""")

# File uploader widget
file = st.file_uploader('Choose an image (JPG, JPEG, PNG)', type=['jpg', 'jpeg', 'png'])

if file is not None:
    # Convert to RGB if image has alpha channel (critical fix)
    img = Image.open(file).convert('RGB')
    
    # Display uploaded image
    st.image(img, caption='Uploaded Image', use_column_width=True)
    
    # Process and predict
    processed_image = process_image(img)
    prediction = model.predict(processed_image)
    prediction = np.round(prediction).astype(int)  # Round to nearest integer

    # Show prediction
    st.subheader("Prediction Result:")
    st.markdown(f"**Estimated Age:** {prediction[0][0]} years")
    
    # Disclaimer
    st.info("""
    Note: Predictions are based on patterns learned during training. 
    Results may vary with image quality and lighting conditions.
    """)
    
    st.divider()
else:
    st.write("Please upload an image to begin.")