Yadvendra commited on
Commit
6c2a7b3
·
verified ·
1 Parent(s): 8c91436

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing import image
4
+ import numpy as np
5
+ from PIL import Image
6
+
7
+ # Function to load and preprocess the uploaded image
8
+ def load_and_preprocess_image(uploaded_image, target_size=(224, 224)):
9
+ img = Image.open(uploaded_image) # Open the uploaded image
10
+ img = img.resize(target_size) # Resize to match model's input shape
11
+ img_array = np.array(img) # Convert to numpy array
12
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
13
+ img_array = img_array / 255.0 # Normalize pixel values
14
+ return img_array
15
+
16
+ # Load your pre-trained model (assuming it's in HDF5 format)
17
+ @st.cache(allow_output_mutation=True) # Cache the model to avoid reloading it each time
18
+ def load_cnn_model():
19
+ model = load_model('brain_tumor_model.h5') # Replace with your model path
20
+ return model
21
+
22
+ # Streamlit App UI
23
+ st.title("Brain Tumor using CNN")
24
+ st.write("Upload a brain scan (JPG format), and the model will predict its class.")
25
+
26
+ # Upload image button
27
+ uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
28
+
29
+ # If an image is uploaded, process it
30
+ if uploaded_file is not None:
31
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
32
+ st.write("Classifying...")
33
+
34
+ # Preprocess the uploaded image
35
+ processed_image = load_and_preprocess_image(uploaded_file, target_size=(224, 224))
36
+
37
+ # Load the model
38
+ model = load_cnn_model()
39
+
40
+ # Make predictions
41
+ predictions = model.predict(processed_image)
42
+ predicted_class = np.argmax(predictions, axis=1)
43
+
44
+ # Map the class index to the actual class names
45
+ class_names = ['glioma', 'pituitary', 'meningioma', 'healthy']
46
+ result = class_names[predicted_class[0]]
47
+
48
+ # Display the prediction
49
+ st.write(f"Predicted Class: **{result}**")
50
+ else:
51
+ st.write("Please upload an image to classify.")
52
+