umarigan commited on
Commit
ece3181
·
1 Parent(s): e32e099

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ from tensorflow import keras
6
+
7
+ # Load the saved model
8
+ loaded_model = keras.models.load_model('tuned_model_classic.h5')
9
+
10
+ # Define the class labels (you can customize these according to your problem)
11
+ class_labels = ['Stroke', 'Non-Stroke']
12
+
13
+ # Streamlit App
14
+ st.title('Image Classifier')
15
+ st.write('Upload an image to classify')
16
+
17
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
18
+
19
+ if uploaded_image is not None:
20
+ # Read the image and preprocess it
21
+ image = Image.open(uploaded_image)
22
+ image = image.convert('RGB')
23
+ image = image.resize((150, 150)) # Resize to match the model's input shape
24
+ image = np.array(image) # Convert PIL image to numpy array
25
+ image = image / 255.0 # Normalize pixel values (similar to how you did in the model training)
26
+
27
+ # Make prediction using the loaded model
28
+ prediction = loaded_model.predict(np.expand_dims(image, axis=0))[0]
29
+ predicted_class_index = np.argmax(prediction)
30
+ predicted_class = class_labels[predicted_class_index]
31
+ confidence = prediction[predicted_class_index]
32
+
33
+ # Display the uploaded image and the prediction
34
+ st.image(image, caption=f'Uploaded Image', use_column_width=True)
35
+
36
+ # Check if the predicted class is "Non-Stroke" and the confidence is high (you can adjust the threshold)
37
+ if predicted_class == 'Non-Stroke' and confidence > 0.8:
38
+ st.write(f'Predicted Class: Uncertain (Possibly both Stroke and Non-Stroke) (Confidence: {confidence:.2f})')
39
+ else:
40
+ st.write(f'Predicted Class: {predicted_class} (Confidence: {confidence:.2f})')