Motion-based video monitoring for early detection of livestock diseases: The case of African swine fever

Early detection of infectious diseases can substantially reduce the health and economic impacts on livestock production. Here we describe a system for monitoring animal activity based on video and data processing techniques, in order to detect slowdown and weakening due to infection with African swine fever (ASF), one of the most significant threats to the pig industry. The system classifies and quantifies motion-based animal behaviour and daily activity in video sequences, allowing automated and non-intrusive surveillance in real-time. The aim of this system is to evaluate significant changes in animals’ motion after being experimentally infected with ASF virus. Indeed, pig mobility declined progressively and fell significantly below pre-infection levels starting at four days after infection at a confidence level of 95%. Furthermore, daily motion decreased in infected animals by approximately 10% before the detection of the disease by clinical signs. These results show the promise of video processing techniques for real-time early detection of livestock infectious diseases.


Introduction
Late detection of emergency diseases promotes disease spread and increases the risk of epidemic, and it may cause significant economic losses in affected countries [1], as demonstrated by the last epidemics of foot-and-mouth disease [2,3], classical swine fever [4,5] or the current epidemic of African swine fever (ASF) [6]. Classical strategies to detect diseases on the farm include active and passive surveillance as well as sentinel surveillance, which focuses on farms at higher risk of incursion [7].
The effectiveness of passive surveillance relies on the ability to detect disease based on the observation of clinical signs. This can pose a severe obstacle to early detection, particularly if death is the most visible clinical sign as is the case with foot-and-mouth disease, classical swine fever or ASF. On the other hand, farms may apply active surveillance approaches to gain information about disease prevalence on the farm under a sampling protocol based on appropriate a1111111111 a1111111111 a1111111111 a1111111111 a1111111111 laboratory tests, for example the qPCR quantitative polymerase chain reaction (qPCR) of blood samples, which can allow rapid and reliable detection of infection onset. While this can be effective, it is financially and logistically burdensome and not always feasible.
Many animal diseases manifest fever and weakness which may be difficult to detect by simple observation in early stages of infection. However, it can be detected through continuousquantitative monitoring of animal behaviour through new technologies [8]. In this work, we aim to demonstrate that animals infected with ASF virus show a progressive deceleration in performing daily activities caused by muscle weakness from early stages of infection, which should translate into reduced overall motion, which quantitative automatic video processing should be able to detect [9]. This might allow detection of ASF onset in real time. Within the framework of the EU-funded Rapidia Field project (www.rapidia.eu), we continuously monitored eight pigs housed in a single indoor pen under Biosafety Level 3 (BSL-3) experimental conditions for 11 days before, and then 12 days after, experimental infection with ASF virus. Animal motion was quantified and classified using video processing and supervised learning, respectively, in order to detect significant early changes in behaviour between pre-and postinfection periods.
Lastest research and developments based on computer vision techniques have improved the current technology mainly designed for motion detection, object recognition and object tracking. With this work, we have implemented algorithms for motion capture, in which movement recorded by a camera is translated into digital models. It also incorporates a supervised learning method to classify motion-based animal behaviour. Finally, the system aims to assess the randomness of runs in order to detect a reduction in daily motion, such that a certain number of consecutive low motion scores may be attributable to ASF.

Ethics statement
The experiment was carried out at the BSL-3 facilities of the VISAVET Centre of the Veterinary Faculty of the Universidad Complutense de Madrid (UCM). The in vivo experimental protocol was approved by the Committee of Animal Experimentation of the UCM (Regulation 2010/63/EU and Spanish Royal Decree 53/2013). The protocol included a detailed description of efforts to prevent unnecessary suffering of the animals, including humane endpoints and the use of anesthetics and euthanasic following Galindo-Cardiel et al. clinical evaluation guidelines [10]. Specifically, humane endpoints were reached when at least one parameter was scored as severe. Animals' health and welfare was checked twice daily (morning and evening) by a clinical veterinarian. Pigs were also monitored 24h/day by videocameras that were checked regularly during the day.

The experiment
The experiment lasted from 11 April 2015 (day 1) to 3 May 2015 (day 23), during which eight Landrace Large White healthy pigs from an authorized breeding centre (four months old and weighing approximately 40 kg) arrived together one day before and were enclosed together in one indoor pen maintained at constant temperature and humidity, with ad libitum access to food and water. They were continuously monitored using one camera in a fixed position. At least twice per day, animals were evaluated by veterinarians and technicians who monitored animal internal temperature, recorded clinical signs [11], kept the pen clean and controlled feed supplies. Once per day, blood and oral samples were analysed for ASF virus using a qPCR test [12], which allows the confirmatory diagnosis of ASF virus within hours of a sample.
All animals were accommodated in the pen and allowed to acclimate for one day before the experiment. On day 11, all animals were simultaneously inoculated with an attenuated strain of ASF virus from a tick captured in Kenya in 2005 (ASFV Ken05/Tk1). The first positive qPCR detection of the virus occurred on day 15 in six of eight animals. Subsequently, some animals showed mild clinical signs but first evident clinical signs associated with ASF started from day 18 in five of eight animals. After this day, all animals spent increasing amounts of time lying down until ultimately dying from the disease. The last animal died on day 23.

Video filming
A total of 541 hours of video footage were taken using a single camera equipped with nightvision capability at a fixed position in an upper corner of the pen (Fig 2A). Video footage was collected continuously throughout the 23-day experiment. The room was artificially illuminated from 7 a.m. to 9 p.m., while the camera recorded under night conditions from 9 p.m. to 7 a.m. The 'U'-shape and the limited size of the pen as well as the tall metal enclosure did not allow individual tracking of the animals. Since the fixed camera filmed the same background throughout, and light intensity remained fairly constant within the day and night periods, it was easy to  distinguish between animals in motion and static background, despite the standard definition (SD) resolution of the video which might make subsequent motion detection less sensitive.
Video was recorded at 6 frames per second (704 x 576 pixels) in RGB24 format, providing 24 bits in red, green and blue channels (RGB) for broad range of colour. We used the red channel for all analyses since it provided the best contrast for animal recognition (see Fig 2B). In addition, we analysed only a region of interest in which all animals were always observable (see Fig 2C).

Motion capture
Animal movements were recorded and digitally processed through the Optical flow algorithm based on the Horn-Schunck methodology [13] implemented in Matlab. This algorithm estimates the speed and direction of moving objects between consecutive images based on the movement of brightness patterns [14]. This estimation process is described below.
Supposing that we know the value of a function E(x, y, t) determining the brightness of any point (x, y) in the (fixed) 2-dimensional domain D recorded in the video, at any time t 2 [0, T max ] during recording and considering a point following a trajectory (x(t), y(t)) in D, if this point maintains its brightness along the entire time interval, the value of E(x(t), y(t), t) remains constant for any t 2 [0, T max ]. Then, assuming that functions E, x and y are smooth, In this equation, dx dt ðtÞ; dy dt ðtÞ À Á represents the velocity of the point that at time t is in (x(t), y(t)). Since we cover the entire domain D with all possible trajectories, we deduce from Eq 2 that @E @x ðx; y; tÞuðx; y; tÞ þ @E @y ðx; y; tÞvðx; y; tÞ þ @E @t ðx; y; tÞ ¼ 0; 8ðx; y; tÞ 2 D Â ½0; T max ; ð3Þ where (u(x, y, t), v(x, y, t)) is the velocity of the point located at (x, y) 2 D at time t. This can be expressed more compactly as To compute the velocity function (u, v), the Horn-Schunck method [13] minimises the error z(u, v) given by where η is a weighting factor that scales global smoothness. Using the theory of calculus of variation, the velocity (u, v) minimising Eq 5 satisfies the Euler-Lagrange equations of z, given by where Δu and Δv are the Laplacians of u and v, defined as In reality, the entire video scene consists of N max 2 N frames (or images) instead of continuous time t 2 [0, T max ], E(x, y, t) takes values in {0, 1, Á Á Á, 255} and D is divided into a matrix of 704 × 576 pixels. Therefore, we considered the discrete function E i,j,k 2 {0, 1, Á Á Á, 255} as the measured average brightness of the pixel at the intersection of the ith row and jth column in the kth frame. Hence, the minimum (u i,j,k , v i,j,k ) of Eq 5 at an arbitrary pixel (i, j) in the discrete domain D of 704 × 576 pixels per frame k 2 {0, 1, Á Á Á, N max can be estimated iteratively as follows [13]: is the maximum number of iterations, η = 10 considering a large relative motion between frames [15], and ð" u n i;j;k ; " v n i;j;k Þ is the neighbourhood average of ðu n i;j;k ; v n i;j;k Þ computed using the convolution kernel ½1 ffiffi ffi . We computed (E x , E y ) using the convolution kernel [−1 −2 −1;0 0 0;1 2 1] and its transposed form for each pixel in the first image and E t between consecutive images using the [−1 1] kernel [15,17].
Finally, we computed the global motion in frame k as Eq 9 allows us to obtain a unique motion value for each frame k and to reduce motion analysis to time series analysis.

Motion smoothing
We analysed the global motion, m k , in order to detect a significant reduction in animal motion following ASF infection. Nevertheless, two main facts had to be considered before the analysis. First, the video resolution caused a perturbation in the values of m k . Indeed, even when no motion was recorded, we observed that the background slightly changed between consecutive frames causing a problematic baseline noise in the time series. Second, as mentioned previously, the human factor altered the real values of global motion when the workers were in the region of interest of the screen. Thus, some intervals of time scored excessively high values of m k only when the workers were in the pen.
To smooth the perturbation in m k , we considered a simple moving average filter by replacing each data point in the time series with the average of the previous K 2 N data points (i.e. the previous K frames). In this work, we considered K = 90; that is, 15 seconds approximately (see Fig 3). Henceforth, all motion works were carried out throughout the new time serieŝ

Motion classification
Activity budgets relate to the use of an animal's time including moving, standing or lying, feeding, drinking, social and aggressive behaviours [21]. In this work, three types of motion were proposed to classify animal activity during the experiment: (1) baseline, when all animals were lying or sleeping, such that motion was minimal or absent; (2) animal motion, when animals were walking, feeding or playing; and (3) human-related motion, when veterinary practices were carried out. Animal movements sometimes increased in the presence of humans, and also movements of the humans themselves contributed to overall motion when they entered the region of interest being recorded. Therefore we quantified the relative amounts of all three types of motion each day and sought to determine changes in animal motion unrelated to human interaction. To classify motion as baseline, animal motion or human-related motion, we used short intervals of S = 15 seconds (corresponding to 6S = 90 frames) and we joined consecutive values ofm k into these intervals as follows M r ¼ ½m 1þ6SðrÀ 1Þ ;m 6Sr ; 8r 2 f1; 2; . . .g: We described each interval M r using five statistical parameters [18]: minimum (M min r ), first quartile (M q1 r ), median (M med r ), third quartile (M q3 r ) and maximum (M max r ). Firstly, we analysed animal behaviour before experimental infection in order to quantify healthy levels of motion. To do this, we grouped the short series M r corresponding to the preinfection phase by using the k-means algorithm (kmeans command in Matlab performed with squared Euclidean distance) with the five statistical values (M min r , M q1 r , M med r , M q3 r , M max r ), which assigned the input observations into k clusters through step-wise cluster centroid estimation based on optimisation of within-and between-cluster dispersion [19].
To set up the optimal number of clusters k used in the k-means algorithm, we used the gap criterion algorithm (evalclusters command in Matlab), which scored the so-called gap statistic associated to a proposed number of clusters based on the minimisation of within-cluster dispersion [20]. Consequently, the maximum value of the gap statistic is associated with the optimal number of clusters (assuming more than 3 and less than 50).
These k clusters should represent a different type of motion recorded during 15-seconds intervals, when the animals were not infected. For instance, intervals in which the animals showed a similar activity within 15 seconds should be grouped in the same cluster (i.e. where there is no high variation between M min r , M q1 r , M med r , M q3 r and M max r ). But depending on the number of animals in motion and the type of activity carried out, the mean value of motion may differ between intervals (i.e. M med r ) and, consequently, such intervals may be grouped in different clusters. On the contrary, intervals in which the animals showed significant changes in the activity such as when they were walking and the human interaction accelerated the overall motion during an instant, should be grouped in clusters with a high variation between M min r , M q1 r , M med r , M q3 r and M max r . After k-means analysis, all k clusters (and the M r records therein) were assigned to the three types of motion relevant for this work: baseline (group G 1 ), animal motion (group G 2 ) and (3) human-related motion (group G 3 ).
Secondly, we classified the remaining M r records to groups G 1 , G 2 and G 3 corresponding to post-infection periods (Fig 1). To do this, we built a support vector machine (SVM) pairwise classifier in Matlab.
To build an accurate SVM classifier, half of the M r records in the pre-infection phase were randomly assigned to the so-called training dataset and the other half of records to the socalled testing dataset. To train the SVM model, we used the svmtrain command with the training dataset; then, we evaluated the accuracy of the model using the svmclassify command with the testing dataset. Once the accuracy of the model was verified, it was then used to classify the remaining M r records of the experiment.

Changes in animal motion as a result of infection
We expected that the daily activity budget (i.e. time spent lying/sleeping and feeding/walking) would change after infection with ASF virus. To measure this reliably, we used the Wald-Wolfowitz runs test (runstest command in Matlab) at significance levels of 90%, 95% and 99%, corresponding to respective p-values of less than 0.1, 0.05 or 0.01, which evaluated whether consecutive daily motion values were randomly distributed around the average value of the considered period. First, runs tests were performed during the pre-infection phase (days 1-11) to verify random distribution of motion values around the average value. Then, runs tests were repeated by expanding the window by one additional day until day 18, when clinical signs were detected. This allowed us to determine whether and when consecutive motion values significantly diverged from the average value, indicating ASF infection.

Motion classification
The gap statistic showed a maximum of 3.6955 for 32 clusters, with the next highest values being 3.6676 for 28 clusters and 3.6653 for 34 clusters. Therefore, we considered k = 32 clusters in the k-means algorithm. Fig 4 shows the centroids of these clusters and the percentage of M r records classified in each one. After comparing many video sequences associated with M r records across all clusters, we assigned cluster 1-7 to baseline (G 1 ), clusters 8-19 to animal motion (G 2 ), and clusters 20-32 to human-related motion (G 3 ). As expected, the lowest motion values corresponded to the baseline, which occurred during 70.06% of the pre-infection period, i.e. around 70% of their time inactive [21]. Intermediate motion values corresponded to animal motion, which occurred during 27.67% of the pre-infection period. Finally, human-related motion was greatest occurring during 2.27% of the time.
The SVM classifier showed accuracy of 95.66-98.87% at classifying records to groups G 1 , G 2 and G 3 (Table 1). Conversely, only 1.64% of records in G 1 were incorrectly assigned to G 2 , 1.13% of records in G 3 were incorrectly assigned to G 2 and records in G 2 were incorrectly assigned to G 1 (1.14%) or G 3 (3.20%). Table 2 shows the proportions of time spent each day in baseline, animal motion and humanrelated motion computed with the entire data set. After the virus inoculation, lying/sleeping time increased substantially and feeding/walking the time where during which animals were in motion decreased substantially across the following post-infection phases. In fact, in the first four days the animal motion decreased 8.51 points and, before the detection through clinical signs, 9.97 points respect to the pre-infection phase.

Changes in animal motion as a result of infection
Comparison of the percentages of records M r classified by the SVM approach into groups G 1 , G 2 and G 3 for each day of the experiment (Fig 5) shows that human-related motion occurred during only a fraction of the time, but it made a disproportionate contribution to the amount of motion generated, covering the clusters with the highest motion values,m k . Therefore, such motion was excluded from the analysis. We executed the SVM-classifier for the remaining days of the experiment. Fig 5 shows the percentage of records, M r , classified in  groups G 1 , G 2 and G 3 per day of the experiment; that is, the time classified as baseline motion, animal in motion and human interaction per day. Throughout the experiment, the human interaction represented a small percentage of the records. Pre-infection phase (days 1-11). Aside from day 1, when animals were slightly hyperactive because of their new environment, they spent fairly constant proportions of each day lying/sleeping and feeding/walking. These proportions varied randomly around the respective mean values of m 1 and m 2 .
Infection phase (days 12-15). Following experimental infection with ASF virus, the proportion of time spent each day in baseline tended to increase, while the proportion of time spent in animal motion tended to decrease. Thus, the percentage of time each day in baseline was higher than on any other previous day of the experiment, while the percentage of time in animal motion was lower than on any other previous day, pointing to a probable effect of ASF.  Clinical detection phase (days 19-23). The trends observed during the infection phase and PCR detection phase worsened during the clinical detection phase, with the percentage of baseline increasing and percentage of animal motion decreasing. This worsening should be interpreted with caution because the animals began to die rapidly from day 18 onwards. In fact, two animals died at day 19 and six at day 23. Therefore the data collected during this phase is unlikely to be useful for assessing the ability of our approach to detect ASF early.
Runs test. Runs tests were carried out for progressively longer time windows during the 23-day recording period in order to examine on what day the changes in baseline and animal motion deviated significantly from pre-infection values (Table 3). These runs tests focused on days 11-18, the period critical for early ASF detection (Fig 5). Runs tests for the period from days 1 to 11 suggested that, as expected, percentages of baseline and animal motion varied randomly around their mean values. Runs tests for longer periods indicated that although the percentage of baseline increased after inoculation and the percentage of animal motion, these changes did not deviate significantly from pre-infection values until day 15, when the differences became significant at the 90% level. The differences were significant at the 95% level on day 16 and at the 99% level on day 18.

Discussion
Video processing of livestock offers the possibility of non-intrusive animal monitoring in real time. Here we aimed to examine whether such monitoring could reliably detect decreases in pig motion in the early stages of ASF infection. Our results in this pilot study showed that, indeed, we detected a significant decrease in motion at just four days after experimental infection with the ASF virus. This was the same day that the virus was detected in blood using qPCR, and a full three days before clinical signs of ASF were observed.
The proposed method comprises motion estimation, clustering/classification of motion patterns and recognition of these patterns via supervised learning. "Optical flow" is a widely used algorithm to measure the apparent velocities of objects in video streams that allowed us to quantify motion between consecutive frames throughout the entire experiment. We divided the total recording period into intervals of a few seconds and classified motion in each interval into (1) baseline (little or no motion), (2) animal motion and (3) human-related motion. An SVM pairwise classifier was used to train the model to recognise the three types of motion based on a randomly selected 50% of the pre-infection dataset, and this ability was then assessed using the other half of the same dataset. After establishing an accuracy >95%, we Table 3. The |Z| statistic and 2-tailed significance (Sig.) for runs tests of percentages of daily time spent in baseline or animal motion (Fig 5). Runs tests were performed on progressively longer intervals of the experimental period, from days 1-11 to days 1-18.

Runs
Day interval applied the classifier to the data collection during the post-infection phases of the experiment. This allowed us to detect an increase in the proportion of baseline activity and concomitant decreases in animal motion. To assess the possibility that these results might be due only to chance, we performed runs tests on data over progressively longer time windows. This analysis indicated that the changes in motion attributable to ASF became significant at four days after infection (95% level).
In our study, a slight perturbation in the global motion was recorded, even when no animals were in the pen (before the start of the experiment) or when the animals did not move, probably reflecting the SD resolution of the camera. Our efforts to treat these perturbed time series as retrogressive models (ARMA/ARIMA) were unsatisfactory, so we treated them as normally distributed and as contributing a constant amount of daily motion to the observed total. It is possible that alternative approaches to noise reduction may allow more refined classification and recognition of motion types, such as differentiation between feeding and walking. In an ideal case, such approaches should be adaptable to a range of farm and animal types through machine learning algorithms, as used here. On the other hand, the runs tests used here were enough to detect changes in 11 days but, considering field conditions, i.e. longer periods in farms, it could be more effective the monitoring through quality control techniques. Likely important to the success of our approach is filtering out animal motion largely or entirely induced by veterinarians and technicians visiting the pen, as well as movement of these workers mistakenly recorded as animal motion during the experiment. While in practice only a fraction of daily motion in our study was human-related, this motion occasionally produced false spikes and so needed to be removed. Future work should examine the possibility of extending Optical flow through the implementation of animal tracking and recognition algorithms [22,23] in order to eradicate definitely human-related motion and to enhance the individual animal monitoring. Therefore, our results with an SD resolution camera suggest that future work should focus on high definition (HD) cameras, which can automatically adjust contrast and brightness and so which may further reduce background noise and blurring.
The technology developed here has been tested in an experimental environment with 8 animals during a short period. Studies related with ASF infected animals do not usually consume long periods nor use high herd sizes. But in field conditions, different issues may vary the motion scores such as the increment of the number of animals monitored, the apparition of any other animal interaction, climatic factors that may alter the light intensity, etc. All these factors should be considered in future works to verify that this system is useful to detect disease outbreaks in field conditions.
Our results illustrate the promise of video processing for early detection of ASF. Many other livestock diseases might present slowdown in motion caused by fever in animals, such as foot-and-mouth disease, classical swine fever, Rift Valley fever, African horse sickness or bluetongue. The possibility to extend the technology presented here to detect rapidly other diseases might improve the current surveillance protocols substantially. In fact, we showed that four days after inoculation, the system launched an alert of ASF-suspicion that, under real farm conditions, probably could be interpreted for the veterinary services to take blood test and confirm an outbreak three days before the first clinical signs.
Supporting information S1 File. Tables and scripts. Files containing raw data, main processed data and main scripts in Matlab. (ZIP)