├── NeuralNetwork.py ├── PredictCancer.py ├── dataset_create.py ├── README.md └── LuncCancerTrain.py /NeuralNetwork.py: -------------------------------------------------------------------------------- 1 | from sklearn.neural_network import MLPClassifier 2 | from datafile import dataset,dataset_output 3 | import pickle 4 | 5 | clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(40, 10), random_state=1,learning_rate_init=0.001,max_iter=10000) 6 | clf.fit(dataset, dataset_output) 7 | 8 | filename = 'finalized_model.sav' 9 | pickle.dump(clf, open(filename, 'wb')) -------------------------------------------------------------------------------- /PredictCancer.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import lungc 3 | import pickle 4 | 5 | num=input("Enter test image number: ") 6 | img=cv2.imread("test/test ("+num+").jpg",0) 7 | left_feature,right_feature,img_left,img_right=lungc.process_lung_test(img) 8 | 9 | 10 | filename = 'finalized_model.sav' 11 | loaded_model = pickle.load(open(filename, 'rb')) 12 | result = loaded_model.predict([left_feature]) 13 | print(result[0]) 14 | if(result[0]): 15 | print("Tumour present") 16 | else: 17 | print("Normal Lung") 18 | -------------------------------------------------------------------------------- /dataset_create.py: -------------------------------------------------------------------------------- 1 | import lungc 2 | import os.path 3 | import cv2 4 | import random 5 | import numpy as np 6 | 7 | nocanc=[] 8 | nocanc_output =[] 9 | for i in range(1,139):#without folder #run for full data 10 | if not os.path.isfile('without/nocanc ('+str(i)+').jpg'): 11 | continue 12 | print(i) 13 | img=cv2.imread('without/nocanc ('+str(i)+').jpg',0) 14 | left_feature,right_feature,img_left,img_right=lungc.process_lung(img) 15 | 16 | nocanc.append(left_feature) 17 | nocanc_output.append(0) 18 | 19 | cancl=[] 20 | cancl_output =[] 21 | for i in range(1,121):#left folder #run for full data 22 | if not os.path.isfile('Left/canc ('+str(i)+').jpg'): 23 | continue 24 | print(i) 25 | img=cv2.imread('Left/canc ('+str(i)+').jpg',0) 26 | left_feature,right_feature,img_left,img_right=lungc.process_lung(img) 27 | 28 | cancl.append(left_feature) 29 | cancl_output.append(1) 30 | 31 | random.seed(2) 32 | dataset=cancl+nocanc 33 | dataset_output = cancl_output + nocanc_output 34 | random.shuffle(dataset) 35 | random.shuffle(dataset_output) 36 | 37 | with open('datafile.py', 'w') as f: 38 | f.write('dataset = %s' % dataset) 39 | f.write('\ndataset = %s' % dataset_output) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lung-Cancer-Detection-Using-Python 2 | ## Dataset 3 | The Cancer Imaging Archive (TCIA) 4 | http://www.cancerimagingarchive.net/ 5 | 6 | ## Code files 7 | Code have been written in Modular fashion 8 | 9 | PredictCancer.py : Final program for testing a image 10 | 11 | NeuralNetwork.py : MLP using SKlearn to learn the features and saving the Weights using pickle 12 | 13 | LungCancerTrain.py : All Image processing techniques and code for training the model are written here 14 | 15 | dataset_create.py : For making the folders of both positive and negative cases and naming the images in required format 16 | 17 | Test Case images of both categories and added in the repository along with its terminal output for reference 18 | 19 | ## Dependencies 20 | Python3 , OpenCV - cv2 , pickle , datafile libraries 21 | 22 | ## Output 23 | 24 | Positive case 25 | 26 | ![cancer image](https://user-images.githubusercontent.com/33830482/42348966-c8301910-80c8-11e8-9427-34fba3c0b84c.png) 27 | ![cancer terminal](https://user-images.githubusercontent.com/33830482/42348967-c92cb17a-80c8-11e8-82df-c31cba6ac42a.png) 28 | 29 | Negative case 30 | 31 | ![no cancer image](https://user-images.githubusercontent.com/33830482/42348968-c95ed394-80c8-11e8-8c2e-5f25a61f3ccd.png) 32 | ![no cancer terminal](https://user-images.githubusercontent.com/33830482/42348970-c9da984e-80c8-11e8-87e0-4afe7dde8bfb.png) 33 | 34 | This work was done in partnership with my friend Tarun Bhargav Sriram as a project for Digital Image Processing elective. 35 | Any queries about the project, contact at vinaybn8997@gmail.com 36 | -------------------------------------------------------------------------------- /LuncCancerTrain.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from scipy import ndimage 3 | import numpy as np 4 | import os.path 5 | from math import sqrt 6 | 7 | def preprocess(img): 8 | img_filted=cv2.medianBlur(img,5)#5 is mask size 9 | thresh,img_thresh=cv2.threshold(img_filted,127,255,cv2.THRESH_BINARY) 10 | kernel=np.ones((7,7),np.uint8) 11 | img_opened=cv2.morphologyEx(img_thresh,cv2.MORPH_OPEN,kernel) 12 | img_closed=cv2.morphologyEx(img_opened,cv2.MORPH_CLOSE,kernel) 13 | return img_closed,img_filted,img_thresh,img_opened 14 | 15 | def lung_side_detect(c_index,contours,img): 16 | img_roied_left_lung=img 17 | img_roied_right_lung=img 18 | #left lung check 19 | x,y,w,h=cv2.boundingRect(contours[c_index[0]]) 20 | M=cv2.moments(contours[c_index[0]]) 21 | cx=int(M['m10']/M['m00']) 22 | cy=int(M['m01']/M['m00']) 23 | #print(cx,cy,int(img.shape[1]/2)) 24 | if(cx