X-Ray-Based Quick COVID-19 Detection With Raspberry Pi

General X-Ray-based Covid-19 detection systems are fast and give quick results along with the status of how much the COVID-19 virus has infected the lungs. Previous Covid-19 detection systems took time to give reports while the infected person needed immediate attention. Also, all such detection systems used parts that required to be disposed of after every use, creating a high demand for raw materials. 

But our X-Ray based Covid-19 detection system needs to be installed only once with an X-Ray machine. The detection system gives timely status of infection inside the lungs. There are two ways to achieve this: Using the Python library or by creating and training an ML model. here we talk about first creating and traning ML method and we will also publish soon with other methods where we use pre-trained model for same thing.

Bill Of Material 

Let’s start our project by collecting the following components.

Note :- You can eliminate the cost of HDMI display by using your laptop or any monitor/TV instead as a display screen. You also need an X-Ray image of the lungs.


Install the latest Raspbian OS in the Raspberry Pi board. Next, you need to prepare X-Ray image datasets of lungs that are infected and not infected by Covid-19. You can obtain this by visiting kaggle, an online resource for X-Ray infected dataset prepared by a community of experts and doctors. You can also obtain data from github. Download the datasets containing X-Ray images of normal lungs and Covid-19 infected lungs. 

Now choose a platform for making your ML model and training it to detect Covid infected lungs in X-Ray images. Here we have various options such as TensorFlow, various PyTorch online options like SensiML ,Apache Spark, EDGE Impulse etc. Here I am using EDGE Impulse.

If you are also using Edge Impulse, then go to create project and select it. You are then asked to choose how you wish to use the project. Since you want to use the project for creating an image processing ML model, select the image. 

Then you will be asked how you want to classify the image (multi object classification or single object classification). Here you can select any. I am using single object classification (refer Fig 2). On clicking the newly created project, you will be asked to specify how you want to use it. Select connect a development board, which will upload the data to the project (refer Fig 3). 

Fig 1. Project creation
Fig 1. Project creation
Fig 2. Selecting the type to classify
Fig 3. Connect to dev board

Installation on Raspberry Pi 

Open the terminal and install the dependency for EDGE_Impulse on Raspberry Pi using the command given below

 curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt install -y gcc g++ make build-essential nodejs sox gstreamer1.0-tools gstreamer1.0-plugins-good gstreamer1.0-plugins-base gstreamer1.0-plugins-base-apps
npm config set user root && sudo npm install edge-impulse-linux -g --unsafe-perm

After installation, run it using edge-impulse-linux 

You will then be asked for your email id and password for EDGE Impulse. Simply enter the details and login (refer Fig 4).

Fig 4. Login using email id and password

Now it will ask you to select the project with which the device needs to be connected. Enter the project name (in my case, it is COVID-19 Detector) (refer Fig 5.).

Fig 5 .Project selection

Dataset Preparation 

Now you are given the URL for uploading the datasets. There are two options: either use Raspberry Pi’s camera to upload datasets of X-ray images (by placing the X-Ray images in the camera frame) or use the PC/Raspberry Pi to upload the X-Ray image files (refer Fig 6). I recommend you use the latter. 

So to manually upload the datasets, that is, images of X-Ray from the PC, select them as testing/training images. After selecting, upload them for training and set the level for classifying the images. Here I am setting the label as COVID-19 infected and Normal Lungs.(refer Fig  8). Now upload X-Ray images of the normal lungs and Covid-19 infected lungs as per their relevance.

Fig 6. Getting URL for creating ML model.
Fig 7. Dataset upload using Raspberry Pi camera for ML model.
Fig 7. Dataset upload using Raspberry Pi camera for ML model.


Fig 8. Uploading data using image file.

In case you miss uploading the images to their respective categories, you can later add and/or label them as ‘Infected Lung’ or ‘Normal Lung’ by using the Edit option. 

Please be careful while labeling the datasets. If you label X-Ray images showing normal lungs as Covid Infected then your model will get trained incorrectly, thus decreasing the accuracy. When you initially download the datasets (from the website), make sure to have two separate folders: one for the training datasets and the other for testing datasets. In both of them, create two more separate folders named as Infected lung X-ray and Normal lung X-ray. Upload images from both these folders separately and categorise them correctly after uploading (refer Fig 9.). 

Fig 9. Setting the level for X-ray Image of datasets for training model

Repeat the same steps for uploading the X-ray images of infected lungs and healthy lungs for testing datasets.The option for uploading X-ray images for testing datasets can be found next to the training option (refer Fig 10).

Fig 10.

Training ML Model

Now after uploading the X-Ray images of infected and non-infected datasets, you are now ready to prepare and train theour ML model and train them.  Go to  Impulse design then click on create Impulse then select the create Impulse (Refer Fg 11) . Next we need to add the processing block and learning block here (Refer Fig 12 , Fig 13).

Fig 11. Creating Impulse
Fig 12. Selecting processing block
Fig 13. Learning block

After adding processing and learning blocks we need to set the parameter. Here we get the features extracted from the X-Ray Image and then we save that features(Refer fig 14 , Fig 15). New we get to new option for training the ML model here we need to select the ML model training to be created using Keras expert mode or normal mode here we are using simple mode in simple ode we got options to set the number of learning cycles if we increase the number of learning cycles the accuracy increases because it go in loop for various cu=ysels of learning but it takes time to make ML Model learn we will get option to set for threshold and learning rate and these values effect the accuracy and time of ML model generated for COVID virus detection. 


Fig 15. Feathers graph for ML model.
Fig 16. Generating parameter
Fig 17. Setting and creating Ml model


Now our trained model is ready for testing. Here we can test the model by trying an already uploaded Image of X-Ray in testing and it will try to detect the Infection of COVID virus in X-ray of lung and tell you . 

Fig 18.Detecting infection from X-Ray Image in testing.
Fig 19.Classifying all Image for testing model accuracy.

Deploying ML Model 

Now our Ml model ready to deploy here we have many option and hardware on which we can deploy our ML model to detect the COVID-19 Virus infection in X-Ray image of Lung . Here we are using the Linex.so we select the Linex. Now after that we open the terminal and then run 


Now after this the Raspberry Pi starts downloading the ML Model and then it runs the model and gives you the URL to open where you can see the live camera video from Raspberry p. Now put your Lung X-ray in front of the camera with full light and then it starts detecting the infected or infected lung and tell you the COVID Test results in a few seconds. 

We have also more option to deploy our ML model of Covid detection one is SDK we can detect the COVID and deploy the Ml Model in our fav programming language like python using the SDK or you can directly upload the image of X-ray in testing data set and then run the mL model in live classification to detect the COVID-19 infected virus in matter of  seconds .

Fig 20. Selecting the board to deploy
Fig 21. Deploying ML model in Raspberry PI
Fig 21. Deploying ML model in Raspberry PI
Fig 23. Detecting the Virus Infected Lung from X-ray Image
Fig 24.Results output
Fig 26. Detecting Infection in X-Ray
Corona virus infection detection using x-ray
Fig 26. Normal Lung detected as normal
 Fig 27. Detecting virus infection inside lung using X-ay
Fig 27. Detecting virus infection inside lung using X-ay

Source link

We will be happy to hear your thoughts

Leave a reply

My Blog
Reset Password
Compare items
  • Total (0)