ajeetkumar01 commited on
Commit
0106479
·
verified ·
1 Parent(s): 581163f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ 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
+ # Resize and preprocess the image
9
+ img = image.resize((128, 128))
10
+ img = np.array(img) / 255.0 # Normalization
11
+ img = np.expand_dims(img, axis=0) # Add batch dimension
12
+
13
+ # Make prediction using the provided model
14
+ prediction = model.predict(img)
15
+
16
+ # Thresholding prediction
17
+ threshold = 0.5
18
+ prediction_class = (prediction > threshold).astype(int)
19
+
20
+ # Interpret prediction
21
+ if prediction_class == 1:
22
+ return "With Mask"
23
+ else:
24
+ return "Without Mask"
25
+
26
+ # Load the model from .h5 file
27
+ @st.cache(allow_output_mutation=True)
28
+ def load_model_from_h5():
29
+ return load_model('model.h5')
30
+
31
+ # Streamlit app
32
+ def main():
33
+ st.title("Mask Detection App")
34
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
35
+
36
+ if uploaded_file is not None:
37
+ # Load the image
38
+ image = Image.open(uploaded_file)
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
+ # Make predictions using the provided model
46
+ prediction = predict_single_image(image, model_h5)
47
+ st.write("Prediction:", prediction)
48
+
49
+ if __name__ == '__main__':
50
+ main()