|
a |
|
b/Lung Cancer Prediction System.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 1, |
|
|
6 |
"id": "788d3638", |
|
|
7 |
"metadata": {}, |
|
|
8 |
"outputs": [], |
|
|
9 |
"source": [ |
|
|
10 |
"import warnings\n", |
|
|
11 |
"warnings.filterwarnings('ignore')\n", |
|
|
12 |
"\n", |
|
|
13 |
"import math\n", |
|
|
14 |
"\n", |
|
|
15 |
"import glob\n", |
|
|
16 |
"from tkinter import *\n", |
|
|
17 |
"from tkinter import filedialog\n", |
|
|
18 |
"from PIL import ImageTk, Image\n", |
|
|
19 |
"import os\n", |
|
|
20 |
"import imghdr\n", |
|
|
21 |
"import cv2 as cv\n", |
|
|
22 |
"\n", |
|
|
23 |
"import pandas as pd\n", |
|
|
24 |
"import numpy as np\n", |
|
|
25 |
"from numpy import sqrt\n", |
|
|
26 |
"\n", |
|
|
27 |
"from skimage.transform import pyramid_reduce, resize\n", |
|
|
28 |
"\n", |
|
|
29 |
"import skimage.filters as filters\n", |
|
|
30 |
"\n", |
|
|
31 |
"from sklearn.model_selection import train_test_split\n", |
|
|
32 |
"from sklearn.metrics import accuracy_score\n", |
|
|
33 |
"\n", |
|
|
34 |
"from sklearn.utils import shuffle\n", |
|
|
35 |
" \n", |
|
|
36 |
"#models\n", |
|
|
37 |
"from sklearn.ensemble import RandomForestClassifier\n", |
|
|
38 |
"from sklearn.linear_model import LogisticRegression\n", |
|
|
39 |
"from sklearn.svm import SVC\n", |
|
|
40 |
"\n", |
|
|
41 |
"import tensorflow as tf\n", |
|
|
42 |
"from tensorflow.keras import datasets, layers, models\n", |
|
|
43 |
"\n", |
|
|
44 |
"from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, Activation\n", |
|
|
45 |
"\n", |
|
|
46 |
"#preprocessing part\n", |
|
|
47 |
"from skimage.segmentation import clear_border\n", |
|
|
48 |
"from skimage.measure import label,regionprops, perimeter\n", |
|
|
49 |
"from skimage.morphology import ball, disk, binary_erosion, remove_small_objects, reconstruction, binary_closing, binary_opening\n", |
|
|
50 |
"from skimage.filters import roberts, sobel\n", |
|
|
51 |
"from scipy import ndimage as ndi" |
|
|
52 |
] |
|
|
53 |
}, |
|
|
54 |
{ |
|
|
55 |
"cell_type": "code", |
|
|
56 |
"execution_count": 2, |
|
|
57 |
"id": "83fce547", |
|
|
58 |
"metadata": {}, |
|
|
59 |
"outputs": [ |
|
|
60 |
{ |
|
|
61 |
"name": "stdout", |
|
|
62 |
"output_type": "stream", |
|
|
63 |
"text": [ |
|
|
64 |
"Total: 1385\n" |
|
|
65 |
] |
|
|
66 |
} |
|
|
67 |
], |
|
|
68 |
"source": [ |
|
|
69 |
"#get all classes names\n", |
|
|
70 |
"classes_old = [\"Bengin\",\"Malignant\",\"Normal\"]\n", |
|
|
71 |
"classes = [\"Benign\",\"Malignant\",\"Normal\"]\n", |
|
|
72 |
"\n", |
|
|
73 |
"#get all images location(total 1097 img)\n", |
|
|
74 |
"img_list = sorted(glob.glob('dataset/after_preprocessing/train_test/*/*/*.*'))\n", |
|
|
75 |
"print('Total: ', len(img_list))\n", |
|
|
76 |
"\n", |
|
|
77 |
"#Define variable to hold X & y\n", |
|
|
78 |
"#create numpy array placeholder for pixels with 1 channel(grayscale)\n", |
|
|
79 |
"IMG_SIZE = 528\n", |
|
|
80 |
"CHANNEL = 1\n", |
|
|
81 |
"#arg: (length of numpy set, height, width, color channel)\n", |
|
|
82 |
"X_segmented = np.empty((len(img_list), IMG_SIZE, IMG_SIZE), dtype=np.uint8)\n", |
|
|
83 |
"\n", |
|
|
84 |
"y = []\n", |
|
|
85 |
"\n", |
|
|
86 |
"# convert images to numpy arrays\n", |
|
|
87 |
"for i, img_path in enumerate(img_list):\n", |
|
|
88 |
" # load image\n", |
|
|
89 |
" img = cv.imread(img_path, cv.IMREAD_GRAYSCALE)\n", |
|
|
90 |
" img = cv.resize(img, (IMG_SIZE, IMG_SIZE))\n", |
|
|
91 |
" X_segmented[i] = img\n", |
|
|
92 |
" y.append(classes[0]) if img_path.find(classes_old[0]) != -1 else (y.append(classes[1]) if img_path.find(classes_old[1]) != -1 else y.append(classes_old[2]))\n", |
|
|
93 |
" \n", |
|
|
94 |
"y = pd.Series(y)" |
|
|
95 |
] |
|
|
96 |
}, |
|
|
97 |
{ |
|
|
98 |
"cell_type": "code", |
|
|
99 |
"execution_count": 3, |
|
|
100 |
"id": "7957ef48", |
|
|
101 |
"metadata": {}, |
|
|
102 |
"outputs": [], |
|
|
103 |
"source": [ |
|
|
104 |
"IMG_SIZE = 128\n", |
|
|
105 |
"CHANNEL = 1\n", |
|
|
106 |
"\n", |
|
|
107 |
"X_dl_segmented_resize = np.empty((len(img_list), IMG_SIZE, IMG_SIZE), dtype=np.uint8)\n", |
|
|
108 |
"# X_dl_nodules_resize = np.empty((len(img_list), IMG_SIZE, IMG_SIZE), dtype=np.uint8)\n", |
|
|
109 |
"# X_dl_nodules_further_resize = np.empty((len(img_list), IMG_SIZE, IMG_SIZE), dtype=np.uint8)\n", |
|
|
110 |
"\n", |
|
|
111 |
"for i, img in enumerate(X_segmented):\n", |
|
|
112 |
" X_dl_segmented_resize[i] = cv.resize(img, (IMG_SIZE, IMG_SIZE))\n", |
|
|
113 |
" \n", |
|
|
114 |
"# for i, img in enumerate(X_nodules):\n", |
|
|
115 |
"# X_nodules_resize[i] = cv.resize(img, (IMG_SIZE, IMG_SIZE))\n", |
|
|
116 |
" \n", |
|
|
117 |
"# for i, img in enumerate(X_nodules_further):\n", |
|
|
118 |
"# X_nodules_further_resize[i] = cv.resize(img, (IMG_SIZE, IMG_SIZE))\n", |
|
|
119 |
"\n", |
|
|
120 |
"# convert to 3d array\n", |
|
|
121 |
"X_dl_segmented_resize = X_dl_segmented_resize.reshape(-1, IMG_SIZE, IMG_SIZE, CHANNEL)\n", |
|
|
122 |
"# X_dl_nodules_resize = X_nodules_resize.reshape(-1, IMG_SIZE, IMG_SIZE, CHANNEL)\n", |
|
|
123 |
"# X_dl_nodules_further_resize = X_nodules_further_resize.reshape(-1, IMG_SIZE, IMG_SIZE, CHANNEL)\n", |
|
|
124 |
"\n", |
|
|
125 |
"# convert to 2d array\n", |
|
|
126 |
"X_ml_segmented_resize = X_dl_segmented_resize.reshape(X_dl_segmented_resize.shape[0], -1)\n", |
|
|
127 |
"\n", |
|
|
128 |
"# #randomizedSearchCV Dataset\n", |
|
|
129 |
"# X_rscv_segmented = X_segmented.reshape(X_segmented.shape[0], -1)" |
|
|
130 |
] |
|
|
131 |
}, |
|
|
132 |
{ |
|
|
133 |
"cell_type": "code", |
|
|
134 |
"execution_count": 4, |
|
|
135 |
"id": "8a4fe6cc", |
|
|
136 |
"metadata": {}, |
|
|
137 |
"outputs": [], |
|
|
138 |
"source": [ |
|
|
139 |
"#split into train and test and factorize the label\n", |
|
|
140 |
"X_dl_train_segmented = X_dl_segmented_resize[300:1385]\n", |
|
|
141 |
"X_dl_test_segmented = X_dl_segmented_resize[0:300]\n", |
|
|
142 |
"\n", |
|
|
143 |
"X_ml_train_segmented = X_ml_segmented_resize[300:1385]\n", |
|
|
144 |
"X_ml_test_segmented = X_ml_segmented_resize[0:300]\n", |
|
|
145 |
"\n", |
|
|
146 |
"# X_train_nodules = X_nodules_resize[300:1385]\n", |
|
|
147 |
"# X_test_nodules = X_nodules_resize[0:300]\n", |
|
|
148 |
"# X_train_nodules_further = X_nodules_further_resize[300:1385]\n", |
|
|
149 |
"# X_test_nodules_further = X_nodules_further_resize[0:300]\n", |
|
|
150 |
"\n", |
|
|
151 |
"y_train = y[300:1385]\n", |
|
|
152 |
"y_test = y[0:300]\n", |
|
|
153 |
"r_rscv = y.copy()\n", |
|
|
154 |
"\n", |
|
|
155 |
"y_train = pd.Series(y_train)\n", |
|
|
156 |
"y_test = pd.Series(y_test)\n", |
|
|
157 |
"y_rscv = pd.Series(r_rscv)\n", |
|
|
158 |
"\n", |
|
|
159 |
"y_train = y_train.factorize()\n", |
|
|
160 |
"y_test = y_test.factorize()\n", |
|
|
161 |
"y_rscv = r_rscv.factorize()\n", |
|
|
162 |
"\n", |
|
|
163 |
"xtr_dl, xts_dl, ytr_dl, yts_dl = train_test_split(X_dl_segmented_resize, y_rscv[0], test_size=math.floor(len(X_dl_segmented_resize)*0.2167), \n", |
|
|
164 |
" random_state = np.random.randint(1,1000, 1)[0])\n", |
|
|
165 |
"xtr_ml, xts_ml, ytr_ml, yts_ml = train_test_split(X_ml_segmented_resize, y_rscv[0], test_size=math.floor(len(X_ml_segmented_resize)*0.2167), \n", |
|
|
166 |
" random_state = np.random.randint(1,1000, 1)[0])" |
|
|
167 |
] |
|
|
168 |
}, |
|
|
169 |
{ |
|
|
170 |
"cell_type": "code", |
|
|
171 |
"execution_count": 5, |
|
|
172 |
"id": "fd9e020b", |
|
|
173 |
"metadata": {}, |
|
|
174 |
"outputs": [], |
|
|
175 |
"source": [ |
|
|
176 |
"cnn_svm = models.Sequential([\n", |
|
|
177 |
" layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, CHANNEL)),\n", |
|
|
178 |
" layers.MaxPooling2D((2, 2)),\n", |
|
|
179 |
" \n", |
|
|
180 |
" layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),\n", |
|
|
181 |
" layers.MaxPooling2D((2, 2)),\n", |
|
|
182 |
" \n", |
|
|
183 |
" layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),\n", |
|
|
184 |
" layers.MaxPooling2D((2, 2)),\n", |
|
|
185 |
" \n", |
|
|
186 |
" layers.Conv2D(filters=256, kernel_size=(3, 3), activation='relu'),\n", |
|
|
187 |
" layers.MaxPooling2D((2, 2)),\n", |
|
|
188 |
" \n", |
|
|
189 |
" #dense network\n", |
|
|
190 |
" #CNN middle layer no need to specify the shape because the network can figure it out automatically\n", |
|
|
191 |
" layers.Flatten(),\n", |
|
|
192 |
" ])\n", |
|
|
193 |
"\n", |
|
|
194 |
"X_full_cnn_svm = cnn_svm.predict(X_dl_segmented_resize)" |
|
|
195 |
] |
|
|
196 |
}, |
|
|
197 |
{ |
|
|
198 |
"cell_type": "code", |
|
|
199 |
"execution_count": 6, |
|
|
200 |
"id": "1113289b", |
|
|
201 |
"metadata": {}, |
|
|
202 |
"outputs": [ |
|
|
203 |
{ |
|
|
204 |
"data": { |
|
|
205 |
"text/plain": [ |
|
|
206 |
"(1385, 9216)" |
|
|
207 |
] |
|
|
208 |
}, |
|
|
209 |
"execution_count": 6, |
|
|
210 |
"metadata": {}, |
|
|
211 |
"output_type": "execute_result" |
|
|
212 |
} |
|
|
213 |
], |
|
|
214 |
"source": [ |
|
|
215 |
"X_full_cnn_svm.shape" |
|
|
216 |
] |
|
|
217 |
}, |
|
|
218 |
{ |
|
|
219 |
"cell_type": "code", |
|
|
220 |
"execution_count": 7, |
|
|
221 |
"id": "6ed00b00", |
|
|
222 |
"metadata": {}, |
|
|
223 |
"outputs": [], |
|
|
224 |
"source": [ |
|
|
225 |
"xtr_hybird, xts_hybird, ytr_hybird, yts_hybird = train_test_split(X_full_cnn_svm, y_rscv[0], test_size=math.floor(len(X_dl_segmented_resize)*0.2167), \n", |
|
|
226 |
" random_state = np.random.randint(1,1000, 1)[0])" |
|
|
227 |
] |
|
|
228 |
}, |
|
|
229 |
{ |
|
|
230 |
"cell_type": "code", |
|
|
231 |
"execution_count": 8, |
|
|
232 |
"id": "f0b854f2", |
|
|
233 |
"metadata": {}, |
|
|
234 |
"outputs": [ |
|
|
235 |
{ |
|
|
236 |
"name": "stdout", |
|
|
237 |
"output_type": "stream", |
|
|
238 |
"text": [ |
|
|
239 |
"Random Forest Accuracy: 0.9233333333333333\n" |
|
|
240 |
] |
|
|
241 |
} |
|
|
242 |
], |
|
|
243 |
"source": [ |
|
|
244 |
"loaded_model = RandomForestClassifier(n_estimators = 200, min_samples_split = 2, min_samples_leaf = 2,\n", |
|
|
245 |
" max_features = 'sqrt', max_depth = 80, bootstrap = False)\n", |
|
|
246 |
"\n", |
|
|
247 |
"loaded_model.fit(xtr_hybird, ytr_hybird)\n", |
|
|
248 |
"\n", |
|
|
249 |
"y_pred_cnn_rf_bestParam = loaded_model.predict(xts_hybird)\n", |
|
|
250 |
"cnn_rf_bestParam_segmented_score = accuracy_score(yts_hybird, y_pred_cnn_rf_bestParam)\n", |
|
|
251 |
"print('Random Forest Accuracy: ', cnn_rf_bestParam_segmented_score)" |
|
|
252 |
] |
|
|
253 |
}, |
|
|
254 |
{ |
|
|
255 |
"cell_type": "code", |
|
|
256 |
"execution_count": 9, |
|
|
257 |
"id": "e2f618e8", |
|
|
258 |
"metadata": {}, |
|
|
259 |
"outputs": [], |
|
|
260 |
"source": [ |
|
|
261 |
"def get_segmented_lungs(im, num, save=False, plot=False, show_on_window=False, crop_percentage=0.05):\n", |
|
|
262 |
" #This funtion segments the lungs from the given 2D slice.\n", |
|
|
263 |
" \n", |
|
|
264 |
" crop = im.copy()\n", |
|
|
265 |
" if show_on_window:\n", |
|
|
266 |
" height,width=im.shape[:2]\n", |
|
|
267 |
" start_row,start_col=int(height*crop_percentage),int(width*crop_percentage)\n", |
|
|
268 |
" end_row,end_col=int(height*(1-crop_percentage)),int(width*(1-crop_percentage))\n", |
|
|
269 |
" crop=crop[start_row:end_row,start_col:end_col]\n", |
|
|
270 |
" else:\n", |
|
|
271 |
" if num == 161 or (num >= 173 and num <= 174) or (num == 758):\n", |
|
|
272 |
" height,width=im.shape[:2]\n", |
|
|
273 |
" start_row,start_col=int(height*0.20),int(width*0.20)\n", |
|
|
274 |
" end_row,end_col=int(height*0.80),int(width*0.80)\n", |
|
|
275 |
" crop=crop[start_row:end_row,start_col:end_col]\n", |
|
|
276 |
" elif num >= 756 and num <= 767:\n", |
|
|
277 |
" #Step 1: Crop the image \n", |
|
|
278 |
" height,width=im.shape[:2]\n", |
|
|
279 |
" start_row,start_col=int(height*0),int(width*0)\n", |
|
|
280 |
" end_row,end_col=int(height*1),int(width*1)\n", |
|
|
281 |
" crop=crop[start_row:end_row,start_col:end_col]\n", |
|
|
282 |
" elif num == 1320 or num == 1219 or (num >= 712 and num <= 767) or (num >= 779 and num <= 799) or (num >= 688 and num <= 699) or (num >= 648 and num <= 664) or (num >= 225 and num <= 234):\n", |
|
|
283 |
" #Step 1: Crop the image \n", |
|
|
284 |
" height,width=im.shape[:2]\n", |
|
|
285 |
" start_row,start_col=int(height*0.03),int(width*0.03)\n", |
|
|
286 |
" end_row,end_col=int(height*0.97),int(width*0.97)\n", |
|
|
287 |
" crop=crop[start_row:end_row,start_col:end_col]\n", |
|
|
288 |
" else:\n", |
|
|
289 |
" #Step 1: Crop the image \n", |
|
|
290 |
" height,width=im.shape[:2]\n", |
|
|
291 |
" start_row,start_col=int(height*0.12),int(width*0.12)\n", |
|
|
292 |
" end_row,end_col=int(height*0.88),int(width*0.88)\n", |
|
|
293 |
" crop=crop[start_row:end_row,start_col:end_col]\n", |
|
|
294 |
" \n", |
|
|
295 |
" #Step 2: Convert into a binary image. \n", |
|
|
296 |
" ret,binary = cv.threshold(crop,140,255,cv.THRESH_BINARY_INV)\n", |
|
|
297 |
" \n", |
|
|
298 |
" #Step 3: Remove the blobs connected to the border of the image.\n", |
|
|
299 |
" cleared = clear_border(binary) \n", |
|
|
300 |
" \n", |
|
|
301 |
" #Step 4: Closure operation with a disk of radius 10. This operation is \n", |
|
|
302 |
" #to keep nodules attached to the lung wall.\n", |
|
|
303 |
" selem = disk(2)\n", |
|
|
304 |
" closing = binary_closing(cleared, selem)\n", |
|
|
305 |
" \n", |
|
|
306 |
" #Step 5: Label the image.\n", |
|
|
307 |
" label_image = label(closing)\n", |
|
|
308 |
" \n", |
|
|
309 |
" #Step 6: Keep the labels with 2 largest areas.\n", |
|
|
310 |
" areas = [r.area for r in regionprops(label_image)]\n", |
|
|
311 |
" areas.sort()\n", |
|
|
312 |
" if len(areas) > 2:\n", |
|
|
313 |
" for region in regionprops(label_image):\n", |
|
|
314 |
" if region.area < areas[-2]:\n", |
|
|
315 |
" for coordinates in region.coords: \n", |
|
|
316 |
" label_image[coordinates[0], coordinates[1]] = 0\n", |
|
|
317 |
" segmented_area = label_image > 0\n", |
|
|
318 |
" \n", |
|
|
319 |
" #Step 7: Erosion operation with a disk of radius 2. This operation is \n", |
|
|
320 |
" #seperate the lung nodules attached to the blood vessels.\n", |
|
|
321 |
" selem = disk(2)\n", |
|
|
322 |
" erosion = binary_erosion(segmented_area, selem) \n", |
|
|
323 |
" \n", |
|
|
324 |
" # Step 4: Closure operation with a disk of radius 10. This operation is \n", |
|
|
325 |
" # to keep nodules attached to the lung wall.\n", |
|
|
326 |
" selem = disk(10)\n", |
|
|
327 |
" closing2 = binary_closing(erosion, selem) \n", |
|
|
328 |
" \n", |
|
|
329 |
" #Step 8: Fill in the small holes inside the binary mask of lungs.\n", |
|
|
330 |
" edges = roberts(closing2)\n", |
|
|
331 |
" fill_holes = ndi.binary_fill_holes(edges)\n", |
|
|
332 |
" \n", |
|
|
333 |
" superimpose = crop.copy()\n", |
|
|
334 |
" #Step 9: Superimpose叠加 the binary mask on the input image.\n", |
|
|
335 |
" get_high_vals = fill_holes == 0\n", |
|
|
336 |
" superimpose[get_high_vals] = 0\n", |
|
|
337 |
"\n", |
|
|
338 |
" superimpose = cv.resize(superimpose, (528, 528)) \n", |
|
|
339 |
" \n", |
|
|
340 |
" if show_on_window:\n", |
|
|
341 |
" directory1 = 'result/'\n", |
|
|
342 |
" directory2 = '.jpg'\n", |
|
|
343 |
" images = [im, crop, binary, cleared, closing, segmented_area, erosion, closing2, fill_holes, superimpose]\n", |
|
|
344 |
" titles = ['0_original_image', '1_cropped_image', '2_binary_image', '3_remove_blobs', '4_closure', '5_roi', '6_erosion', '7_closure', '8_fill_hole', '9_result']\n", |
|
|
345 |
" for i, title in enumerate(titles):\n", |
|
|
346 |
" filename = directory1 + title + directory2\n", |
|
|
347 |
" try:\n", |
|
|
348 |
" cv.imwrite(filename, images[i])\n", |
|
|
349 |
" except:\n", |
|
|
350 |
" indices = images[i].astype(np.uint8) #convert to an unsigned byte\n", |
|
|
351 |
" indices*=255\n", |
|
|
352 |
" cv.imwrite(filename, indices)\n", |
|
|
353 |
" else:\n", |
|
|
354 |
" #flip vertically\n", |
|
|
355 |
" directory1 = 'preprocessing/pre1/'\n", |
|
|
356 |
" directory2 = '.jpg'\n", |
|
|
357 |
" images = [crop, binary, cleared, label_image, superimpose]\n", |
|
|
358 |
" titles = ['cropped_image', 'binary_image', 'remove_blobs', 'label', 'result']\n", |
|
|
359 |
"\n", |
|
|
360 |
" if save:\n", |
|
|
361 |
" for y in range(5):\n", |
|
|
362 |
" filename = directory1 + str(y+1) + titles[y] + '/' + titles[y] + str(num+1) + directory2\n", |
|
|
363 |
" cv.imwrite(filename, images[y])\n", |
|
|
364 |
"\n", |
|
|
365 |
" images = [im, crop, binary, cleared, closing, label_image, segmented_area, erosion, closing2, fill_holes, superimpose]\n", |
|
|
366 |
" \n", |
|
|
367 |
" if plot:\n", |
|
|
368 |
" titles = ['Original Image', \n", |
|
|
369 |
" 'Step 1: Cropped Image', \n", |
|
|
370 |
" 'Step 2: Binary image', \n", |
|
|
371 |
" 'Step 3: Remove blobs', \n", |
|
|
372 |
" 'Step 4: Closure', \n", |
|
|
373 |
" 'Step 5: Label', \n", |
|
|
374 |
" 'Step 6: Region On Interest',\n", |
|
|
375 |
" 'Step 7: Erosion',\n", |
|
|
376 |
" 'Step 8: Closure', \n", |
|
|
377 |
" 'Step 9: Fill Holes',\n", |
|
|
378 |
" 'Step 10: Result']\n", |
|
|
379 |
" plot_img(images, titles, camp=plt.cm.bone, rows = 3, cols = 4, fontsize= 50)\n", |
|
|
380 |
" \n", |
|
|
381 |
"# if show_on_window:\n", |
|
|
382 |
"# directory1 = 'result/'\n", |
|
|
383 |
"# directory2 = '.jpg'\n", |
|
|
384 |
"# titles = ['0_original_image', '1_cropped_image', '2_binary_image', '3_remove_blobs', '4_closure', '5_roi', '6_erosion', '7_fill_hole', '8_result']\n", |
|
|
385 |
"# for i, title in enumerate(titles):\n", |
|
|
386 |
"# filename = directory1 + title + directory2\n", |
|
|
387 |
"# try:\n", |
|
|
388 |
"# cv.imwrite(filename, images[i])\n", |
|
|
389 |
"# except:\n", |
|
|
390 |
"# indices = images[i].astype(np.uint8) #convert to an unsigned byte\n", |
|
|
391 |
"# indices*=255\n", |
|
|
392 |
"# cv.imwrite(filename, indices)\n", |
|
|
393 |
" \n", |
|
|
394 |
" return superimpose" |
|
|
395 |
] |
|
|
396 |
}, |
|
|
397 |
{ |
|
|
398 |
"cell_type": "code", |
|
|
399 |
"execution_count": 10, |
|
|
400 |
"id": "d52cdbbf", |
|
|
401 |
"metadata": {}, |
|
|
402 |
"outputs": [], |
|
|
403 |
"source": [ |
|
|
404 |
"#define a function to convert the y_pred, y_test to human readable (from 0,1,2... to inclusion, pitted....)\n", |
|
|
405 |
"def convertLabels(y_test, classes):\n", |
|
|
406 |
" return classes[y_test]" |
|
|
407 |
] |
|
|
408 |
}, |
|
|
409 |
{ |
|
|
410 |
"cell_type": "code", |
|
|
411 |
"execution_count": 11, |
|
|
412 |
"id": "07894d70", |
|
|
413 |
"metadata": {}, |
|
|
414 |
"outputs": [], |
|
|
415 |
"source": [ |
|
|
416 |
"root = Tk()\n", |
|
|
417 |
"root.title('Lung Cancer Prediction System')\n", |
|
|
418 |
"width= root.winfo_screenwidth() \n", |
|
|
419 |
"height= root.winfo_screenheight() \n", |
|
|
420 |
"root.geometry(\"%dx%d\" %(width, height))\n", |
|
|
421 |
"\n", |
|
|
422 |
"def openfn():\n", |
|
|
423 |
" global image_path\n", |
|
|
424 |
" filename = filedialog.askopenfilename(initialdir=\"test\", title =\"Select a CT-Scan Image\", filetypes=((\"jpg files\",\"*.jpg*\"),(\"png files\",\"*.png\"),(\"jpeg files\",\"*.jpeg\")))\n", |
|
|
425 |
" image_path = filename\n", |
|
|
426 |
"# return filename\n", |
|
|
427 |
"\n", |
|
|
428 |
"def open_img():\n", |
|
|
429 |
"# img_path = openfn()\n", |
|
|
430 |
" global image_path\n", |
|
|
431 |
" openfn()\n", |
|
|
432 |
" if imghdr.what(image_path) == 'png' or imghdr.what(image_path) == 'jpeg' or imghdr.what(image_path) == 'jpg':\n", |
|
|
433 |
" img = cv.imread(image_path, cv.IMREAD_GRAYSCALE)\n", |
|
|
434 |
" img = cv.resize(img, (528, 528))\n", |
|
|
435 |
" \n", |
|
|
436 |
" numOfImg = 1\n", |
|
|
437 |
" test = img.copy()\n", |
|
|
438 |
" segment_result = get_segmented_lungs(test, numOfImg, show_on_window=True, crop_percentage=float(int(clicked.get())/100))\n", |
|
|
439 |
" \n", |
|
|
440 |
" plot_on_app()\n", |
|
|
441 |
" get_prediction_result()\n", |
|
|
442 |
" \n", |
|
|
443 |
"def plot_on_app():\n", |
|
|
444 |
" result_list = sorted(glob.glob('result/*.*'))\n", |
|
|
445 |
" coor_x = 82\n", |
|
|
446 |
" coor_y = 62\n", |
|
|
447 |
" for i, result_path in enumerate(result_list):\n", |
|
|
448 |
" if i == 5:\n", |
|
|
449 |
" coor_y = coor_y + 320\n", |
|
|
450 |
" coor_x = 82\n", |
|
|
451 |
" img = Image.open(result_path)\n", |
|
|
452 |
" img = img.resize((242, 242))\n", |
|
|
453 |
" image = ImageTk.PhotoImage(img)\n", |
|
|
454 |
"\n", |
|
|
455 |
" label_image = Label(image=image)\n", |
|
|
456 |
" label_image.image = image\n", |
|
|
457 |
" label_image.place(x=coor_x,y=coor_y)\n", |
|
|
458 |
"\n", |
|
|
459 |
" coor_x = coor_x + 280\n", |
|
|
460 |
"\n", |
|
|
461 |
"def get_prediction_result():\n", |
|
|
462 |
" img = cv.imread('result/9_result.jpg', cv.IMREAD_GRAYSCALE)\n", |
|
|
463 |
" img = cv.resize(img, (128, 128))\n", |
|
|
464 |
" predict_img = np.expand_dims(img, 0)\n", |
|
|
465 |
" predict_img = predict_img.reshape(-1, 128, 128, 1)\n", |
|
|
466 |
" extraction_predict_img = cnn_svm.predict(predict_img)\n", |
|
|
467 |
" result_predict_img = loaded_model.predict(extraction_predict_img)\n", |
|
|
468 |
" result_predict_img_converted = convertLabels(result_predict_img[0], classes)\n", |
|
|
469 |
" result_label.config(text='Prediction Result : ' + str(result_predict_img_converted))\n", |
|
|
470 |
" \n", |
|
|
471 |
"# Change the label text\n", |
|
|
472 |
"def recrop():\n", |
|
|
473 |
" global image_path\n", |
|
|
474 |
" if image_path != '':\n", |
|
|
475 |
" img = cv.imread(image_path, cv.IMREAD_GRAYSCALE)\n", |
|
|
476 |
" img = cv.resize(img, (528, 528))\n", |
|
|
477 |
" numOfImg = 1\n", |
|
|
478 |
" test = img.copy()\n", |
|
|
479 |
" get_segmented_lungs(test, numOfImg, show_on_window=True, crop_percentage=float(int(clicked.get())/100))\n", |
|
|
480 |
" plot_on_app()\n", |
|
|
481 |
" get_prediction_result()\n", |
|
|
482 |
" \n", |
|
|
483 |
"coor_x = 80\n", |
|
|
484 |
"coor_y = 60\n", |
|
|
485 |
"for i in range(10):\n", |
|
|
486 |
" if i == 5:\n", |
|
|
487 |
" coor_y = coor_y + 320\n", |
|
|
488 |
" coor_x = 80\n", |
|
|
489 |
" Frame(root, highlightbackground=\"black\", highlightthickness=2,width=250, height=250).place(x=coor_x, y=coor_y)\n", |
|
|
490 |
" coor_x = coor_x + 280\n", |
|
|
491 |
" \n", |
|
|
492 |
"txt1 = 'Select a CT-Scan to predict ->'\n", |
|
|
493 |
"Label(root, text=txt1, font=('Times', '18', 'italic')).place(x = 40, y = 10)\n", |
|
|
494 |
"\n", |
|
|
495 |
"Button(root, text='Select a CT-Scan Image', font=('Times', '13', 'italic'), command=open_img, bg='#C7C6C1', bd=3).place(x = 350, y = 10) \n", |
|
|
496 |
"titles = ['Original Image', 'Step 1: Cropped Image', 'Step 2: Binary image', 'Step 3: Remove blobs', 'Step 4: Closure'\n", |
|
|
497 |
" , 'Step 5: Region On Interest', 'Step 6: Erosion', 'Step 7: Closure', 'Step 8: Fill Holes', 'Step 9: Segmented Result']\n", |
|
|
498 |
"\n", |
|
|
499 |
"txt1 = 'Prediction Result : ' \n", |
|
|
500 |
"result_label = Label(root, text=txt1, font=('Times', '18', 'italic'))\n", |
|
|
501 |
"result_label.place(x = 40, y = 710)\n", |
|
|
502 |
"\n", |
|
|
503 |
"txt1 = 'Average Test Accuracy : 0.9386, Standard Deviation : 0.0246' \n", |
|
|
504 |
"Label(root, text=txt1, font=('Times', '18', 'italic')).place(x = 770, y = 690)\n", |
|
|
505 |
"\n", |
|
|
506 |
"txt1 = 'Average Precision Score : 0.95, Average Recall Score : 0.94, Average F1 Score : 0.94'\n", |
|
|
507 |
"Label(root, text=txt1, font=('Times', '18', 'italic')).place(x = 650, y = 730)\n", |
|
|
508 |
"\n", |
|
|
509 |
"txt1 = 'Crop : %'\n", |
|
|
510 |
"Label(root, text=txt1, font=('Times', '18', 'italic')).place(x = 700, y = 10)\n", |
|
|
511 |
"\n", |
|
|
512 |
"#txt1 = ''\n", |
|
|
513 |
"#Label(root, text=text1, font=('Times', '18', 'italic')).place(x = 200, y = 700)\n", |
|
|
514 |
"\n", |
|
|
515 |
"# Dropdown menu options\n", |
|
|
516 |
"options = [\n", |
|
|
517 |
" \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\",\n", |
|
|
518 |
" \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\",\n", |
|
|
519 |
" \"21\", \"22\", \"23\", \"24\", \"25\", \"26\", \"27\", \"28\", \"29\", \"30\"\n", |
|
|
520 |
"]\n", |
|
|
521 |
" \n", |
|
|
522 |
"# datatype of menu text\n", |
|
|
523 |
"clicked = StringVar()\n", |
|
|
524 |
" \n", |
|
|
525 |
"# initial menu text\n", |
|
|
526 |
"clicked.set( \"5\" )\n", |
|
|
527 |
" \n", |
|
|
528 |
"# Create Dropdown menu\n", |
|
|
529 |
"drop = OptionMenu( root , clicked , *options).place(x = 772, y = 10)\n", |
|
|
530 |
"\n", |
|
|
531 |
"Button(root, text='Reload', command=recrop, font=('Times', '13', 'italic'), bg='#C7C6C1', bd=3).place(x = 890, y = 10)\n", |
|
|
532 |
"\n", |
|
|
533 |
"coor_x = [130, 370, 660, 940, 1250, 70, 405, 690, 960, 1200]\n", |
|
|
534 |
"coor_y = 310\n", |
|
|
535 |
"for i in range(10):\n", |
|
|
536 |
" if i == 5:\n", |
|
|
537 |
" coor_y = coor_y + 320\n", |
|
|
538 |
" Label(root, text=titles[i], font=('Times', '18', 'italic')).place(x = coor_x[i], y = coor_y)\n", |
|
|
539 |
"\n", |
|
|
540 |
"#variable declare\n", |
|
|
541 |
"image_path = ''\n", |
|
|
542 |
" \n", |
|
|
543 |
"root.mainloop()" |
|
|
544 |
] |
|
|
545 |
}, |
|
|
546 |
{ |
|
|
547 |
"cell_type": "code", |
|
|
548 |
"execution_count": null, |
|
|
549 |
"id": "319121c2", |
|
|
550 |
"metadata": {}, |
|
|
551 |
"outputs": [], |
|
|
552 |
"source": [] |
|
|
553 |
} |
|
|
554 |
], |
|
|
555 |
"metadata": { |
|
|
556 |
"kernelspec": { |
|
|
557 |
"display_name": "Python 3", |
|
|
558 |
"language": "python", |
|
|
559 |
"name": "python3" |
|
|
560 |
}, |
|
|
561 |
"language_info": { |
|
|
562 |
"codemirror_mode": { |
|
|
563 |
"name": "ipython", |
|
|
564 |
"version": 3 |
|
|
565 |
}, |
|
|
566 |
"file_extension": ".py", |
|
|
567 |
"mimetype": "text/x-python", |
|
|
568 |
"name": "python", |
|
|
569 |
"nbconvert_exporter": "python", |
|
|
570 |
"pygments_lexer": "ipython3", |
|
|
571 |
"version": "3.8.8" |
|
|
572 |
} |
|
|
573 |
}, |
|
|
574 |
"nbformat": 4, |
|
|
575 |
"nbformat_minor": 5 |
|
|
576 |
} |