Machine Learning roach for Brain Tumor Detection

Machine Learning roach for Brain Tumor Detection

Machine Learning approach for Brain Tumor Detection

Brain tumour detection using OpenCV and Machine learning in Python  Programming. This model can be deployed on the cloud and detect tumour precisely.

For this project, we'll need some libraries listed below:

  1. Matplotlib
  2. Pandas
  3. Sklearn
  4. Cv2
  5. Numpy

This project is divided into two parts:

1) Tumour segmentation:

we will create a class which is TumorExtractor(), which will help to extract tumour part from the image

     class TumorExtractor():


we will initialize the class with a file name and read the image in greyscale format,

     def __init__(self,img_file,width,height):
         self.img = cv2.resize(cv2.imread(img_file,0),(width,height))

below function used to display image,

     def verify_image(self, tum):
        cv2.imshow('Windows1', tum)
        except KeyboardInterrupt as e:

extracting tumour from the image, we need to extract brain part from
the image. This function will extract the brain from the image and
remove the unwanted part of the image.

        def extract_brain(self,ero_itr=5,dil_itr=5):


            # remove all the noise from the image
            #eg:- salt and pepper
             kernel1 = np.ones((5,5),np.float32)/25
             dst1 = cv2.filter2D(self.img,-1,kernel1)
             #threshold the image to genrate filter
             ret2,th2 = cv2.threshold(dst1,45,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
              #erosion of image to remove irregular shape and noise
              kernel3 = np.ones((5,5), np.uint8)
              img_erosion = cv2.erode(th2, kernel3, iterations=ero_itr)
             #dilation of image to maximize the region of interest
             img_dilation = cv2.dilate(img_erosion, kernel3, iterations=dil_itr)

        # we will draw the contour around the largest irregular shape to extract brain part from image

        contours,hierarchy = cv2.findContours(img_dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
            if len(contours) != 0:
               c = max(contours, key = cv2.contourArea)
               x,y,w,h = cv2.boundingRect(c)
               new_img = self.img[y:y+h,x:x+w]

         return new_img

        # we will return extracted brian part from the image


Now we will extract tumour from brain image:

   def extract_tumor(self):
        #extract brain from the image by calling the function extract_brain()
        new_img = self.extract_brain()


        #apply the medium filter to smooth image and remove the noise
        median_filter = cv2.medianBlur(new_img, 5)
        #apply erosion and dilation
        erosion = cv2.morphologyEx(median_filter, cv2.MORPH_ERODE, kernel)
        dilation = cv2.morphologyEx(erosion, cv2.MORPH_DILATE, kernel)
        #applying thresholding on the image to extract the tumor part from the image
        ret3,th3 = cv2.threshold(dilation,160,255,cv2.THRESH_BINARY)



        #apply contour filter and extract the largest contour part
        contours,hierarchy = cv2.findContours(th3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        if len(contours) != 0:
            c = max(contours, key = cv2.contourArea)
            x,y,w,h = cv2.boundingRect(c)
            tumor = new_img[y:y+h,x:x+w]



            #finally applying above technique, we were able to segment and detect the tumor part inthe image

            new_img = cv2.rectangle(new_img, (x, y), (x + w, y + h), (255,255,255), 1)
            cv2.putText(new_img, 'Tumor Detected', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255,255,255), 2)



2) Classification of Image:

In this part, we will create a machine learning model to classify the image into two categories:

  1. Yes
  2. No

Now, we will create a class for a machine learning model which is given below:

we need to initialize the model with important variable,
     def __init__(self,folder_list):
       folder_list{{string}}: it will store the filename
       labels{{list}}: it will store the list of labels
       image_data{{list}}: it will store the array of the image
       model{{Object}}: it is an object of Machine learning model which Support vector machine
       stdscl{{Object}}: it is an object of a standard scaling function
        self.folder_list = folder_list
        self.image_data = []
        self.model  = SVC()
        self.stdscl = StandardScaler()
        self.x_train  = None
        self.x_test = None
        self.y_train = None
        self.y_test  = None

next step, we will create the URL for reading data and convert to image matrice for training,
      def create_url(self):
       # this will read the directory of all image in the folder
        prediction_list  = os.listdir(self.folder_list)
        url_list = []
        for pred in prediction_list:
        dct = {}

       #here, we will genrate the url for image and segregate the url depending upo the classses
        for url,pred in zip(url_list,prediction_list):
            images  = os.listdir(url)
            img_lst = []
            for img in images:
#         print(dct)
        return dct

Now, we will read the image and return in the form of image array matrix,
     def convert_to_standerd_image(self,img):
        # we will read the image
        # we reduce the image size
        # we will convert it in 1D array and return
            img = cv2.imread(img,0)
            img = cv2.resize(img,(128,128))
            img = img.reshape(1,-1)
            return img
        except Exception as e:
            print("format incorrect ")
            return [[]]

we will preprocess data and scaler it so that we can reduce the computation of the SVC algorithm.
    def preprocess_image(self):
        # call the functions to create URL
        # prepare the training data from the given dataset
        # define the label for each class
        img_urls  = self.create_url()
        for tags in img_urls:
            for img in img_lst:
                if tags == "yes":

Now, we will train and scale the data,

    def train_model(self):
       # we will divide the data into two part which is a train set and test set
       # scale the data using a standard scaler
       # train the SVC model using preprocessed data
        self.x_train,self.x_test,self.y_train,self.y_test = train_test_split(self.image_data,self.labels,test_size= 0.2,random_state=0)
        self.x_train = self.stdscl.fit_transform(self.x_train)
        self.x_test = self.stdscl.transform(self.x_test),self.y_train)

Finally, we will check the accuracy of our model and test it against user image,
    def predict_score(self):
        #This function is used to test accuracy of thee model
        predictions = self.model.predict(self.x_test)
        print("Model Accuracy: ",accuracy_score(predictions,self.y_test))


    def predict_class(self,img_file):
       # This function is used for prediction of each image
        img = self.convert_to_standerd_image(img_file)
        img = self.stdscl.transform(img)
        pred = self.model.predict(img)
        if(pred == 1):
            te = TumorExtractor(img_file,600,400)
            print("Tumor present")
            print("Tumor not present")

Driver Code:
     if __name__ == "__main__":
        clf = ImageClassifier("brain_tumor_dataset")

Dataset Link:-
Project Link:-

                                 ******************written by Abhishek Gupta*******************