-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (39 loc) · 1.68 KB
/
main.py
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
import streamlit as st
import tensorflow as tf
from tensorflow import keras
from PIL import Image
import numpy as np
MODEL_PATH = "Custom_Lite_100x100.h5"
st.set_page_config(page_title="X-Ray Pneumonia Classifier", layout="wide", initial_sidebar_state="expanded")
selected_page = st.sidebar.radio("Navigation", ["Upload Image", "About"])
@st.cache_resource()
def load_model():
model = keras.models.load_model(MODEL_PATH)
return model
model = load_model()
if selected_page == "About":
st.title("Welcome to the Image Classifier")
st.write("Use the sidebar to navigate.")
elif selected_page == "Upload Image":
st.title("Pneumonia X-Ray classifier")
st.subheader("Upload an Image")
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
# print(uploaded_image)
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", width=500)
if st.button("Classify"):
# Preprocess the image for the model
image = tf.keras.preprocessing.image.load_img(uploaded_image, target_size=(100, 100))
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])
input_arr = input_arr.astype('float32') / 255.
# Make predictions using the model
prediction = float(model.predict(input_arr,verbose = 0)[0])
# print("\n", prediction)
if prediction>0.5 :
defect = 'Pneumonia: Positive'
else:
defect = 'Pneumonia: Negative'
st.subheader(defect)
st.write(f"Confidence: {prediction:.2%}")