Covid-19 Detection from X-Ray

3 minute read

☝️ to the video demo on my Youtube channel Source code

Automated detection of COVID-19 cases using deep neural networks with X-ray images

The objective of this project is to develop a web application that provides an accurate diagnosis of COVID-19 from chest X-ray images. While test kits are used for diagnosis of Coronavirus disease , the process takes time and the test kits are limited in their availability. However, the COVID-19 is also diagnosable using radiological images taken through lung X-Ray. This process inspired me to develop a web application that provides COVID-19 test and delivers instant results. First thing first, I collected publicly available images, then I trained a convolutional neural network to classify images into two categories positive and negative for SARS cov2. The model consists of uncovering hidden patterns by detecting structural abnormalities and disease categorization. I developed the application using python’s Flask framework.The user can upload lung frontal X-Ray image, submit it and then start testing. Few seconds later, the user will be redirected to a new page where the X-Ray image and the result of the test will be displayed. I used Javascript/Jquery and bootstrap for the Frontend.

Introduction

The novel coronavirus 2019 (COVID-2019), which first appeared in Wuhan city of China in December 2019, spread rapidly around the world and became a pandemic.It has caused a devastating effect on both daily lives, public health, and the global economy. It is critical to detect the positive cases as early as possible so as to prevent the further spread of this epidemic and to quickly treat affected patients.The need for auxiliary diagnostic tools has increased as there are no accurate automated toolkits available. Recent findings obtained using radiology imaging techniques suggest that such images contain salient information about the COVID-19 virus. Application of advanced artificial intelligence (AI) techniques coupled with radiological imaging can be helpful for the accurate detection of this disease, and can also be assistive to overcome the problem of a lack of specialized physicians in remote villages.

Inspiration

Why do we need a deep learning model for early Covid-19 Detection? Because of the followings :

  • Blood Tests are costly
  • Blood Tests take time to conduct ~5 hours per patient
  • Extent of Spread can be detected
  • Defaillant or inaccurate toolkits

Data Collection

At the time I decided to start this project, I didn’t find ready open-source data of x-ray images of both positive and negative patients. For normal x-ray images, I find Kaggle Chest X-ray dataset. For affected chest, i keep searching till i found out a github repository that shares an open dataset of chest X-ray and CT images of patients which are positive or suspected of COVID-19 or other viral and bacterial pneumonias (MERS, SARS, and ARDS.). All the data is collected from public sources as well as through indirect collection from hospitals and physicians.
Creation of Covid-19 samples from the github images repository and Metadata.csv. Metadata.csv contains extra data about the patient (age, sex….) and the label of the findings (‘COVID-19’, ‘ARDS’, ‘SARS’, ‘Pneumocystis’, ‘Streptococcus’,’No Finding’, ‘Chlamydophila’, ‘E.Coli’….).For our purpose, we read only images of COVID-19 positive, so we should filter rows from Metadat.csv to keep only rows where[“finding”]== “COVID-19” with a front view.

# To Create data for positive samples
FILE_PATH = "images/metadata.csv"
IMAGES_PATH = "images"
# Shortlis of covid-19 xray images with front view (PA)
cnt = 0

for (i, row) in df.iterrows() :
    if row["finding"]== "COVID-19" and row['view']=="PA":
        filename = row["filename"]
        image_path = os.path.join(IMAGES_PATH, filename)
        image_copy_path = os.path.join(TARGET_DIR, filename)
        shutil.copy2(image_path, image_copy_path)
        print("Moving image", cnt)
        cnt += 1

The integral data creation code Our Final dataset is composed of :

  • Covid positive Samples : 142 samples
  • Normal Chest X-Rays : 142 samples

    Convolutional neural network architecture

    After preprocessing the images, it is time to build a Convolutional Neural Network using Sequential API of Keras.This model aims to classify whether an image covid-19 positive or negative sample.

Fig. 1: CNN model architecture

As shown above in figure 1, the first layer group contains Convolution, Relu and MaxPooling layers. The second layer group contains Convolution, Relu and MaxPooling layers. I then add a flatten and Relu activation layer to stack the output convolutions as well as cater overfitting. Last but not least I add a Sigmoid classifier.


To be continued ….

Technology and tools used for this project covers

  • Python
  • Numpy and Pandas for data prerocessing
  • Matplotlib for data visualization
  • Keras sequential model for CNN building
  • Jupyter notebook, visual studio code as IDE
  • Python flask for http server
  • HTML/CSS/Javascript for UI