Spaces:
Sleeping
Sleeping
uodated formatting and make it standardize
Browse files
app.py
CHANGED
@@ -3,35 +3,89 @@ from PIL import Image
|
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.models import load_model
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Define a function to make predictions on a single image
|
7 |
def predict_single_image(image, model):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
# Load the model from .h5 file
|
27 |
@st.cache(allow_output_mutation=True)
|
28 |
def load_model_from_h5():
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
# Streamlit app
|
32 |
def main():
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if uploaded_file is not None:
|
37 |
# Load the image
|
@@ -39,12 +93,14 @@ def main():
|
|
39 |
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
40 |
|
41 |
# Button to make prediction
|
42 |
-
if st.button('Predict'):
|
43 |
# Load the model from .h5 file
|
44 |
model_h5 = load_model_from_h5()
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
|
49 |
if __name__ == '__main__':
|
50 |
-
main()
|
|
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.models import load_model
|
5 |
|
6 |
+
# Custom CSS for styling
|
7 |
+
custom_css = """
|
8 |
+
<style>
|
9 |
+
body {
|
10 |
+
background-color: #f0f2f6;
|
11 |
+
}
|
12 |
+
.stButton>button {
|
13 |
+
background-color: #4CAF50;
|
14 |
+
color: white;
|
15 |
+
padding: 10px 15px;
|
16 |
+
text-align: center;
|
17 |
+
text-decoration: none;
|
18 |
+
display: inline-block;
|
19 |
+
font-size: 16px;
|
20 |
+
margin: 4px 2px;
|
21 |
+
cursor: pointer;
|
22 |
+
border-radius: 8px;
|
23 |
+
border: none;
|
24 |
+
}
|
25 |
+
.stButton>button:hover {
|
26 |
+
background-color: #45a049;
|
27 |
+
}
|
28 |
+
.stTextInput>div>div>input {
|
29 |
+
border-radius: 8px;
|
30 |
+
}
|
31 |
+
.stTextInput>div>div>input:focus {
|
32 |
+
border-color: #4CAF50;
|
33 |
+
box-shadow: none;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
"""
|
37 |
+
|
38 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
39 |
+
|
40 |
+
# Set page configuration
|
41 |
+
st.set_page_config(
|
42 |
+
page_title="Face Mask Detection App",
|
43 |
+
page_icon=":mask:",
|
44 |
+
layout="wide"
|
45 |
+
)
|
46 |
+
|
47 |
# Define a function to make predictions on a single image
|
48 |
def predict_single_image(image, model):
|
49 |
+
try:
|
50 |
+
# Resize and preprocess the image
|
51 |
+
img = image.resize((128, 128))
|
52 |
+
img = np.array(img) / 255.0 # Normalization
|
53 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
54 |
|
55 |
+
# Make prediction using the provided model
|
56 |
+
prediction = model.predict(img)
|
57 |
|
58 |
+
# Thresholding prediction
|
59 |
+
threshold = 0.5
|
60 |
+
prediction_class = (prediction > threshold).astype(int)
|
61 |
|
62 |
+
# Interpret prediction
|
63 |
+
if prediction_class == 1:
|
64 |
+
return "With Mask"
|
65 |
+
else:
|
66 |
+
return "Without Mask"
|
67 |
+
except Exception as e:
|
68 |
+
st.error(f"Error occurred during prediction: {str(e)}")
|
69 |
|
70 |
# Load the model from .h5 file
|
71 |
@st.cache(allow_output_mutation=True)
|
72 |
def load_model_from_h5():
|
73 |
+
try:
|
74 |
+
return load_model('model.h5')
|
75 |
+
except Exception as e:
|
76 |
+
st.error(f"Error occurred while loading the model: {str(e)}")
|
77 |
|
78 |
# Streamlit app
|
79 |
def main():
|
80 |
+
# App title and description
|
81 |
+
st.title("Face Mask Detection App")
|
82 |
+
st.write("Upload an image and click 'Predict' to see the prediction.")
|
83 |
+
|
84 |
+
# Header Image
|
85 |
+
header_image = Image.open("header_image.jpg")
|
86 |
+
st.image(header_image, use_column_width=True)
|
87 |
+
|
88 |
+
uploaded_file = st.file_uploader("", type=["jpg", "png", "jpeg"])
|
89 |
|
90 |
if uploaded_file is not None:
|
91 |
# Load the image
|
|
|
93 |
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
94 |
|
95 |
# Button to make prediction
|
96 |
+
if st.button('Predict', key='predict_button'):
|
97 |
# Load the model from .h5 file
|
98 |
model_h5 = load_model_from_h5()
|
99 |
+
if model_h5:
|
100 |
+
# Make predictions using the provided model
|
101 |
+
prediction = predict_single_image(image, model_h5)
|
102 |
+
if prediction:
|
103 |
+
st.success(f"Prediction: {prediction}")
|
104 |
|
105 |
if __name__ == '__main__':
|
106 |
+
main()
|