File size: 2,310 Bytes
2ce82a3
 
 
 
 
3947e56
 
 
2ce82a3
 
 
 
 
 
 
 
 
258d822
2ce82a3
 
 
 
 
3947e56
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce82a3
a319b8a
2ce82a3
 
 
 
 
 
 
 
258d822
 
2ce82a3
 
 
 
 
 
 
 
 
b9a229f
2ce82a3
b9a229f
3947e56
 
a319b8a
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
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}")