Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +54 -0
- cnn_model.h5 +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the model
|
| 7 |
+
model = load_model('cnn_model.h5', compile=False)
|
| 8 |
+
|
| 9 |
+
# Function to process the uploaded image
|
| 10 |
+
def process_image(img):
|
| 11 |
+
img = img.resize((128, 128))
|
| 12 |
+
img = np.array(img)
|
| 13 |
+
img = img / 255.0
|
| 14 |
+
img = np.expand_dims(img, axis=0)
|
| 15 |
+
return img
|
| 16 |
+
|
| 17 |
+
# Title of the application
|
| 18 |
+
st.title('👶 Age Detection from Image 📸')
|
| 19 |
+
st.write("Upload a photo, and the model will predict the age.")
|
| 20 |
+
|
| 21 |
+
# Sidebar for additional interaction options
|
| 22 |
+
st.sidebar.header("Instructions")
|
| 23 |
+
st.sidebar.write("""
|
| 24 |
+
1. Upload an image of a face.
|
| 25 |
+
2. The model will predict the age based on the image.
|
| 26 |
+
3. The output will be displayed below the image.
|
| 27 |
+
""")
|
| 28 |
+
|
| 29 |
+
# File uploader for the user to upload an image
|
| 30 |
+
file = st.file_uploader('Select an image (jpg, jpeg, png)', type=['jpg', 'jpeg', 'png'])
|
| 31 |
+
|
| 32 |
+
if file is not None:
|
| 33 |
+
# Displaying the uploaded image
|
| 34 |
+
img = Image.open(file)
|
| 35 |
+
st.image(img, caption='Uploaded Image', use_column_width=True)
|
| 36 |
+
|
| 37 |
+
# Process the image and predict the result
|
| 38 |
+
image = process_image(img)
|
| 39 |
+
prediction = model.predict(image)
|
| 40 |
+
prediction = np.round(prediction).astype(int) # Rounding the prediction
|
| 41 |
+
|
| 42 |
+
# Show result in a more interactive format
|
| 43 |
+
st.subheader("Prediction Result:")
|
| 44 |
+
st.write(f"Predicted Age: **{prediction[0][0]}** years old")
|
| 45 |
+
|
| 46 |
+
# Optionally, you can add a confidence message
|
| 47 |
+
st.markdown(f"""
|
| 48 |
+
**Confidence:** The model has made this prediction based on its trained data, but the prediction may vary depending on the quality of the image and other factors.
|
| 49 |
+
""")
|
| 50 |
+
|
| 51 |
+
# A divider for clarity
|
| 52 |
+
st.markdown("---")
|
| 53 |
+
else:
|
| 54 |
+
st.write("Please upload an image to get started.")
|
cnn_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:245aecb3416a5808c9d86e676a79737c3d3060e4a8b0bd5e0e37d58b78294ac7
|
| 3 |
+
size 83915432
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
Pillow
|