Sayandip's picture
Update app.py
a319b8a verified
Raw
History Blame Contribute Delete
2.31 kB
import streamlit as st
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from PIL import Image
from gtts import gTTS
import tempfile
import base64
# Load the pre-trained model
model = tf.keras.models.load_model("Model.keras")
# Define image size expected by the model
img_height, img_width = 128, 128
# Function to preprocess image before feeding it to the model
def preprocess_image(img):
img = img.convert('RGB') # Convert image to RGB (remove alpha channel if present)
img = img.resize((img_height, img_width)) # Resize image
img_array = image.img_to_array(img) / 255.0 # Normalize pixel values
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
return img_array
# Function to convert text to speech and auto-play
def speak_auto(text):
tts = gTTS(text=text, lang='en')
with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
tts.save(fp.name)
audio_bytes = fp.read()
b64 = base64.b64encode(audio_bytes).decode()
audio_html = f"""
<audio autoplay>
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
</audio>
"""
st.markdown(audio_html, unsafe_allow_html=True)
# Streamlit app title
st.title('Yawning Detection by Mouth State Prediction')
# Streamlit file uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Open the image
img = Image.open(uploaded_file)
# Display the uploaded image with adjusted width
st.image(img, caption="Uploaded Image", use_container_width=True)
# Preprocess the image
img_array = preprocess_image(img)
# Make prediction
prediction_prob = model.predict(img_array)
prediction = (prediction_prob > 0.5).astype(int) # Binary classification: 0 or 1
# Display the prediction result
result = "The person is yawning." if prediction[0] == 1 else "The person is not yawning."
st.write(f"Prediction: {result}")
st.write(f"Prediction probability: {prediction_prob[0][0]:.4f}")
# Convert the result into speech and play it
speak_auto(f"Based on the uploaded image of mouth, {result}. Prediction probability is {prediction_prob[0][0]:.4f}")