How to detect respiratory disease in early pigs using Edge Impulse

by Clinton Oduor

Precision livestock farming-based TinyML device for early detection of pigs respiratory diseases using Edge Impulse.


By 2050, the global human population is projected to be over 10 billion, consuming 50–60% more than it does now. Without modifying our current consumption practices, there will be a significant increase in food insecurity globally. Animal products are critical to the nutrition, food security, livelihoods and resilience of hundreds of millions of people throughout the world. Livestock accounts for 40% of worldwide income from agriculture. Demand for animal products is set to continue increasing in the next three decades, as is their market price.

For years, experiments, trials and research have been conducted to turn Precision Livestock Farming (PLF) into a success. Precision Livestock Farming is potentially one of the most powerful avenues for development interventions in the livestock sector in particular as well as the national development at large. The rise of new technologies such as TinyML and IoT have the potential to revolutionize the livestock farming industry. They can be used to boost farm profitability, efficiency and sustainability by improving the breeding, nutritional, early disease diagnosis, and other management aspects of various livestock species. The challenge and the success of farming lie in how precisely we can intensify our farming practices in the long run.

Over the years, pig farming has gradually risen to become one of the top agribusiness ventures. Globally, pork accounts for more than 38% of the world’s meat production, making it a very popular meat.



Health and nutrition are the two major factors governing the physical and economic performance of pigs from weaning to slaughter. Respiratory diseases in pigs are arguably the most important health concern for swine producers today. According to a survey done by the National Animal Health Monitoring System(NAHMS), respiratory diseases were identified as the main cause of swine mortality – 44.2% of nursery pigs and 61.1% of grower/finisher pigs.

Early detection and diagnosis of respiratory infections in commercial pig farms is critical for timely intervention, which improves treatment success, reduces disease impacts, and promotes sustainable pig production.

Pigs make a wide range of sounds with a variety of meanings. Sick pigs’ vocalizations could signal respiratory illnesses. Coughing is one of the symptoms that veterinarians look for when diagnosing and treating common livestock respiratory illnesses.


In this project, I will build a low-cost TinyML device that can detect sounds of a coughing pig within a herd of grunting, squealing and screaming pigs in a pigsty.

Project Architecture


Model Training on Edge Impulse

Step 1: Data Acquisition

The first step was to collect data that represent the two classes to be classified:

  • Coughing Pig Sound
  • Noise (Pigsty Background, grunting pigs, squealing pigs, and screaming pigs)

Getting an off-the-shelf dataset for the project was hard so I decided to prepare my own from scattered sources online. For the coughing pig sound, I managed to get 7 minutes of data. I then used Audacity software to highlight and export .WAV samples of 1 second length of the region of interest as shown in the image below.


While exporting, I set the sampling rate at 16 KHz and 16 bit-PCM. The dataset was too small with limited samples of coughing sound samples so I decided to perform some data curation using this GitHub repo by Shawn Hymel. During the curation, I mixed the coughing samples with pig-sty background noise to get an output of 1800 samples.

For the Noise class, I collected the grunting, squealing and screaming sounds from YouTube converting them to mp3 then to.WAV file format. I then sampled them at 16KHz using audacity and put them in one folder. After that, I added the below python script in the same folder and ran it on Visual Studio Code to split the files into chunks of 1 second each.

from pydub import AudioSegment
from pydub.utils import make_chunks
import os
def process_sudio(file_name):
myaudio = AudioSegment.from_file(file_name, "wav")
chunk_length_ms = 1000 # pydub calculates in millisec
chunks = make_chunks(myaudio,chunk_length_ms) #Make chunks of one sec
for i, chunk in enumerate(chunks):
chunk_name = './Noise/' + file_name + "_{0}.wav".format(i)
print ("exporting", chunk_name)
chunk.export(chunk_name, format="wav")
all_file_names = os.listdir()
os.makedirs('Noise') # creating a folder named chunked
for each_file in all_file_names:
if ('.wav' in each_file):

After that, I uploaded them to Edge Impulse Studio for training using the Web Interface Uploader as shown below.

Step 2: Impulse Design

After data acquisition, I designed my impulse as shown below. For the DSP block, I chose MFCC while for the machine learning block I decided to go with a Neural Network block as shown below.

After that, I saved my parameters as default and generated features. Below is a sample of how an extracted spectrogram of a coughing pig looks like.

Step 3: Training

Next, I proceeded to the Neural Network Classifier. I started with 50 epochs and a learning rate of 0.005 with a 0.80 as minimum confidence rating as shown in the figure below.

Neural Network Architecture:

After training for 50 epochs, I got results as shown in the confusion matrix below.

Model Deployment

After model training and testing, we proceed to model deployment. I chose Arduino Library and turned on EON compiler as shown in the image below, then clicked “build”. The EON Compiler optimizes the trained neural network to run on up to 55% RAM and 35% ROM while still maintaining desired accuracy level. This is particularly important for this project since we will be running inference on a resource-constrained Arduino microcontroller. The build process downloaded an Arduino library as shown below.


The hardware consists of two Arduino boards (Arduino Nano 33 BLE & Arduino MKR Fox 1200) communicating to each other via I2C protocol. Arduino Nano 33 BLE has an onboard microphone and it is used for performing inference at the edge while the MKR Fox 1200 is used for sending alerts upon detection of a coughing pig sound via the Sigfox network.

For the Arduino Nano 33 BLE sense, I uploaded a modified code as a library as found on the GitHub page for this project here.

I also uploaded this Arduino code to the Arduino MKR Fox 1200 to send alerts via the Sigfox Network. The circuit was then enclosed in a casing and an antenna fitted as shown below.

After assembling everything and making sure that everything works well, I deployed the device for testing at a pig farm as shown below.

Future Work

  • Collect more data from different pig species at different stages of growth to retrain the model to be more robust.
  • Try looking for specific coughing patterns for specific pigs’ respiratory diseases.

Download the Code

Download the Schema

Source : Arduino Projecthub
Previous Story

An Ugly Truth

Next Story

Get A Galaxy Of Care With Samsung Care+