prshanthreddy commited on
Commit
2017254
·
verified ·
1 Parent(s): d97e568

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +101 -0
  2. lungs_weights.h5 +3 -0
  3. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ from PIL import Image
4
+ import numpy as np
5
+ from tensorflow.keras.applications.efficientnet import EfficientNetB7 as PretrainedModel, preprocess_input
6
+ from tensorflow.keras.layers import GlobalAveragePooling2D, Flatten, Dense
7
+ from tensorflow.keras.models import Model
8
+ from skimage.filters import threshold_otsu
9
+ import cv2
10
+ import time
11
+
12
+ X = Y = 224
13
+ import os
14
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
15
+
16
+ def preprocess_image(uploaded_image, predicted_label):
17
+ # Read image bytes from uploaded file
18
+ file_bytes = np.asarray(bytearray(uploaded_image.read()), dtype=np.uint8)
19
+ img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
20
+
21
+ if img is None:
22
+ raise ValueError("Failed to load the uploaded image.")
23
+
24
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
25
+ img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
26
+ thresh = threshold_otsu(img_gray)
27
+ img_otsu = img_gray < thresh
28
+ total_area = img_otsu.size
29
+ black_area = np.count_nonzero(img_otsu == 0)
30
+ white_area = np.count_nonzero(img_otsu == 1)
31
+
32
+ if predicted_label != 'lung_n':
33
+ if white_area >= 300000:
34
+ level = 3
35
+ elif 200000 <= white_area < 3000000:
36
+ level = 2
37
+ elif 100000 <= white_area < 200000:
38
+ level = 1
39
+ elif white_area < 100000:
40
+ level = 0
41
+
42
+ if level == 3:
43
+ st.error("Cancer type: " + predicted_label)
44
+ st.error("Level of Cancer: 3")
45
+ elif level == 2:
46
+ st.warning("Cancer type: " + predicted_label)
47
+ st.warning("Level of Cancer: 2")
48
+ elif level == 1:
49
+ st.info("Cancer type: " + predicted_label)
50
+ st.info("Level of Cancer: 1")
51
+ elif level == 0:
52
+ st.success("Cancer type: " + predicted_label)
53
+ st.success("Level of Cancer: 0")
54
+ else:
55
+ st.success("Predicted as Lung with Benign Tumor")
56
+
57
+
58
+ # Create a Streamlit application
59
+ def main():
60
+ st.title('Lung Cancer Detection & Severity Level using Deep Learning')
61
+ input_shape = (X, Y, 3)
62
+ K = 3
63
+ start_time = time.time()
64
+ ptm = PretrainedModel(
65
+ input_shape=(X, Y, 3),
66
+ weights='imagenet',
67
+ include_top=False
68
+ )
69
+ ptm.trainable = False
70
+ x = GlobalAveragePooling2D()(ptm.output)
71
+ x = Flatten()(x)
72
+ x = Dense(128, activation='relu')(x)
73
+ x = Dense(64, activation='relu')(x)
74
+ y = Dense(K, activation='softmax')(x)
75
+ model = Model(inputs=ptm.input, outputs=y)
76
+ model.load_weights('lungs_weights.h5')
77
+ # model.save('lungs_model.h5')
78
+
79
+ label_map = {0: 'lung_Adenocarcinoma', 1: 'lung_benign', 2: 'lung_squamous cell carcinoma'}
80
+
81
+ uploaded_image = st.file_uploader('Upload an image')
82
+
83
+ if uploaded_image is not None:
84
+ image = Image.open(uploaded_image)
85
+ image = image.resize((224, 224))
86
+ image_array = np.array(image)
87
+ image_array = preprocess_input(image_array)
88
+ image_array = np.expand_dims(image_array, axis=0)
89
+ predictions = model.predict(image_array)
90
+ predicted_class = np.argmax(predictions)
91
+ predicted_label = label_map[predicted_class]
92
+ preprocess_image(uploaded_image, predicted_label)
93
+ end_time = time.time()
94
+ elapsed_time = end_time - start_time
95
+ st.write('Time taken to predict is approximately:', round(elapsed_time, 2), 'seconds')
96
+ st.image(image, caption='Uploaded Image', width=300)
97
+ else:
98
+ st.write('Please upload an image')
99
+
100
+ if __name__ == '__main__':
101
+ main()
lungs_weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:db1a9bd087cd0aac54215314a5b8d3516eefbd63b0be2b62cf377b20d7771eb4
3
+ size 259503352
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ Pillow
3
+ keras
4
+ tensorflow
5
+ streamlit
6
+ opencv-python
7
+ scikit-image
8
+