LovnishVerma commited on
Commit
3c4d877
1 Parent(s): 4550e07

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import gradio as gr
3
+ import cv2
4
+ import numpy as np
5
+ from tensorflow.keras.models import load_model
6
+ from tensorflow.keras.applications.vgg16 import preprocess_input
7
+ from tensorflow.keras.preprocessing import image
8
+ from werkzeug.utils import secure_filename
9
+ import os
10
+
11
+ # Loading Models
12
+ braintumor_model = load_model('models/brain_tumor_binary.h5')
13
+
14
+ # Configuring Streamlit
15
+ UPLOAD_FOLDER = 'static/uploads'
16
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
17
+ st.set_page_config(page_title="Brain Tumor Prediction App", page_icon=":brain:")
18
+
19
+ def allowed_file(filename):
20
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
21
+
22
+ def preprocess_imgs(set_name, img_size):
23
+ set_new = []
24
+ for img in set_name:
25
+ img = cv2.resize(img, dsize=img_size, interpolation=cv2.INTER_CUBIC)
26
+ set_new.append(preprocess_input(img))
27
+ return np.array(set_new)
28
+
29
+ def crop_imgs(set_name, add_pixels_value=0):
30
+ set_new = []
31
+ for img in set_name:
32
+ gray = cv2.GaussianBlur(img, (5, 5), 0)
33
+ thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
34
+ thresh = cv2.erode(thresh, None, iterations=2)
35
+ thresh = cv2.dilate(thresh, None, iterations=2)
36
+ cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
37
+ cnts = cnts[0] if len(cnts) == 2 else cnts[1]
38
+ c = max(cnts, key=cv2.contourArea)
39
+ extLeft = tuple(c[c[:, :, 0].argmin()][0])
40
+ extRight = tuple(c[c[:, :, 0].argmax()][0])
41
+ extTop = tuple(c[c[:, :, 1].argmin()][0])
42
+ extBot = tuple(c[c[:, :, 1].argmax()][0])
43
+ ADD_PIXELS = add_pixels_value
44
+ new_img = img[extTop[1] - ADD_PIXELS:extBot[1] + ADD_PIXELS,
45
+ extLeft[0] - ADD_PIXELS:extRight[0] + ADD_PIXELS].copy()
46
+ set_new.append(new_img)
47
+ return np.array(set_new)
48
+
49
+ # Function to preprocess the image
50
+ def preprocess_image(file_path):
51
+ img = image.load_img(file_path, target_size=(200, 200))
52
+ img_array = image.img_to_array(img)
53
+ img_array = np.expand_dims(img_array, axis=0)
54
+ img_array /= 255.0 # Normalize the image
55
+ return img_array
56
+
57
+ def predict_braintumor(img_path):
58
+ img_gray = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
59
+
60
+ # Crop and preprocess the grayscale image
61
+ img_processed = crop_imgs([img_gray])
62
+ img_processed = preprocess_imgs(img_processed, (224, 224))
63
+
64
+ # Make prediction
65
+ pred = braintumor_model.predict(img_processed)
66
+
67
+ # Handle binary decision
68
+ confidence = pred[0][0]
69
+
70
+ if confidence >= 0.5:
71
+ return "Brain Tumor Not Found!"
72
+ else:
73
+ return "Brain Tumor Found!"
74
+
75
+ def main():
76
+ st.title("Brain Tumor Prediction App")
77
+
78
+ uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
79
+
80
+ if uploaded_file is not None:
81
+ st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
82
+ st.write("")
83
+ st.write("Classifying...")
84
+
85
+ # Read the contents of the uploaded file
86
+ file_contents = uploaded_file.read()
87
+
88
+ # Save the uploaded file
89
+ filename = secure_filename(uploaded_file.name)
90
+ file_path = os.path.join(UPLOAD_FOLDER, filename)
91
+
92
+ with open(file_path, "wb") as f:
93
+ f.write(file_contents)
94
+
95
+ # Make prediction
96
+ result = predict_braintumor(file_path)
97
+
98
+ # Display prediction
99
+ st.subheader("Prediction:")
100
+ st.success(result)
101
+
102
+ if __name__ == "__main__":
103
+ main()
104
+ gr.Interface(fn=predict_braintumor, inputs="image", outputs="text").launch()