Development of an Anomaly Detection System for Cattle Using Infrared Image and Machine Learning

In Japan, the number of cattle is on a declining trend. There is a strong demand for a technology that can efficiently grasp the individual health status of cattle and detect changes in deep body temperature, which is the most basic and important tool for managing the health of cattle. In this study, we developed an artificial intelligence (AI) characterization module that estimates the deep body temperature in a noncontact manner using AI technology. A laboratory model was developed, and the coefficient of determination (R2) and the estimation accuracy were improved after machine learning, which shows the feasibility of the noncontact estimation of cattle body temperature. On the other hand, we collected the environment temperature, humidity, illuminance, and infrared (IR) images of the body surface of the cattle for the first time in a real-life environment of cattle to develop an AI characterization module that estimates the body temperature of cattle. By using this system to test three cattle, R2 = 0.287 was obtained after machine learning, and the estimation accuracy reached about ± 0.5 °C. This shows that the temperature inside cattle can be inferred to some extent, and the health status of cattle can be predicted from this temperature.


Introduction
In Japan, the number of breeding farms of milk and beef cattle and the number of cattle are on a declining trend, especially for beef cattle. (1) This is one of the causes of the rapid rise in calf market price due to the shortage of cattle. (1) However, it is expected that the number of breeding animals can be increased by reducing the cost per unit owing to economies of scale. There is also a danger that the breeding management cannot keep up, leading to an increase in the number of illnesses and accidents. The importance of daily health care is stressed, because many cases of cattle deaths and sickness each year not only cause significant problems in livestock management, but also place a great psychological burden on breeders. Therefore, there is a strong demand for a technology that can efficiently grasp the individual health status of a herd of cattle so that illnesses can be detected at an early stage before serious symptoms appear.
There are various methods for managing the health of cattle, such as urine tests, (2,3) rumen tests, (4)(5)(6) and body hair monitoring, (7) but the measurement of body temperature is the most basic and important tool. (8)(9)(10)(11)(12) However, the conventional measurement of rectal temperature is inefficient, and it is difficult to measure body temperature continuously. On the other hand, a contact-type sensor is difficult to fix on the body of cattle, making it difficult to determine their overall health status. (9) In addition, temperature measurement is stressful for cattle and has a negative impact on their lives. (7,13) A system using a thermography camera for measuring the body surface temperature of cattle in a noncontact manner has been reported, but there is still a need to improve its operability and mobility, and it is not yet possible to measure the deep body temperature over a long distance. (14)(15)(16)(17) In recent years, image processing technology has advanced and is being actively used because of improvements in machine learning accuracy and computer performance. In this study, we developed an artificial intelligence (AI) characterization module that estimates the deep body temperature in a noncontact manner using AI technology. The system collects health-related information, and we verify the feasibility of collecting health information using the system.

Experimental Equipment, Conditions, and Methods
In order to achieve the purpose of this experiment mentioned above, a learning model is created. The learning model is calculated using test data from the cattle house, and the possibility of estimating body temperature is examined. In order to create a learning model, it is necessary to set parameters that have a major effect on body temperature. Because of basic rules in machine learning, i.e., more data under different conditions mean higher model performance, we hope to collect data over a wide range of conditions and as much data as possible. However, the factors affecting cattle body temperature are unclear, and the feasibility of this method must be confirmed. Thus, first, a simulation experiment is performed in the laboratory to clarify the possible factors and the feasibility of this method. Next, we examine the possibility of collecting actual cattle information and estimating body temperature in a cattle house. Hereinafter, the apparatus, conditions, and method of each experiment will be described. Figure 1 shows the fundamental structure of the newly developed cattle house simulation device. The latter mainly consists of a heat source (Interface Corporation, 0.1 °C surface uniformity and 0.1 °C control accuracy), chillers (CCA-1112A and CTP-3000, Tokyo Rikakikai Co., Ltd.), a humidifier (AHCU-2, Kitz Micro Filter Corporation, 5-80% RH), temperature loggers (SL-type, KN Laboratories), and an infrared (IR) thermal imager (testo 872, Testo SE & Co. KGaA). The core part of the heat source is a Peltier element, which is basically a thermoelectric cooling device using the Peltier effect to create a heat flux at the junction of two different types of material. It can transfer heat from one side of the device to the other, with the consumption of electrical energy, depending on the direction of the current. With this principle, the Peltier element in the heat source could create a uniform, stable, and highaccuracy temperature at the surface, which could be considered as the body surface of cattle if it is covered with skin and hair. The parameters that may have an effect on body temperature are air temperature, humidity, and IR temperature. The Peltier element is placed in a metal box with pipes embedded in the walls. These pipes are full of circulating water, so that the internal environment could be controlled by heating or cooling the circulating water, whose temperature is considered as air temperature. Button-type temperature loggers are used to measure the surface (true value) and air temperatures. Humidity is adjusted using a humidifier and a hygrometer. The IR thermal imager is used to observe the surface of the Peltier element from the top of the box. In order to process the data from the IR camera, a program in Python was written to summarize the maximum temperature of each IR image as a number to record the temperature data representing the image. In this manner, enough data could be obtained for modeling to simulate the body surface. Using these data, a body temperature estimation model was created. As shown in Table 1, the environmental temperature (T air ), humidity (Hum), and Peltier element's IR temperature (T IR ) were set as the inputs of X, and the Peltier element's set temperature, which was considered as the cattle's deep body temperature (true value), was set as the input of Y. The true value was calculated as

Simulated experiments in the laboratory
True value = f(T air , Hum, T IR ). (1) In this experiment, a total of 117 sets of data were recorded. They were used for model building and training, and their coefficient of determination (R 2 ) was evaluated. Here, a linear regression model and a polynomial (quadratic) regression model were selected as the models for machine learning to evaluate the proposed model because of their high R 2 (R 2 was determined to be 0.895 for the linear regression model and 0.922 for the polynomial regression model). In the experiment, we also investigated the accuracy of the estimation. We compared the estimated temperature with the actual temperature to find the difference, and the range of the difference was regarded as the accuracy.
In order to examine the practicality and performance of the models built in the laboratory in an actual cattle house, test data in a cattle house were obtained using an IR image monitoring system. In addition to IR images, environmental data such as air temperature and humidity were also collected. A tail temperature sensor was used to collect the tail temperature as reference data for comparison with the rectal temperature, which was considered as a typical deep body temperature for cattle. By using the model calculated in the laboratory, the estimated deep body temperature was compared with the actual temperature measured from the tail, and the results (R 2 ) were evaluated. Basic information about the experiment in the cattle house is shown in Table 2. The data obtained from the experiment in Table 2 were used to test the feasibility and accuracy of the model established in the laboratory.

Experiments in the cattle house
The data from the cattle house must be collected and analyzed in order to examine the correlation between the data from the actual cattle house and the laboratory, and these data are also used to discuss the possibility of realizing this system in a cattle house. As much surrounding information as possible was collected using sensors, and a model related to body temperature was created using the AI characterization module. Environmental temperature, humidity, illuminance, and so forth were recorded using a sensor, and the temperature distribution was recorded using an IR camera. The deep body temperature was recorded by fixing a temperature sensor to the cattle tail. In the model building stage, a model was created by machine learning using the information that had an effect on the body temperature from the outside as the input values and the measured deep body temperature as the target value. In the model testing stage, the created model was used to estimate the deep body temperature. The results were evaluated by comparison with the actual temperature measured from the tail. For both the model building and model testing stages, the images obtained using the IR camera must still be digitally processed. Because the temperature of the cattle in the cattle house is significantly higher than the environmental temperature, it is easy to distinguish the outlines of cattle from the thermal images. In order to obtain the temperature data of the cattle bodies more accurately, the outlines of the cattle are first extracted from the captured image by AI technology at this stage. Then, the first 50 data points with the highest pixel temperatures in the picture are sampled, and the average value is used to represent the temperature measured in this image using the IR camera. As shown in Table 2, we used the 100-pixel temperatures in Table 1 Data structure of the model in the laboratory. As inputs of X As input of Y IR temperature True value Air temperature Humidity the image (100 data points with the highest pixel temperatures). However, this method requires a large amount of calculation and the calculation efficiency is low. Therefore, we used 50 data points, and the results obtained were almost the same as those obtained using 100 points, which proves that using 50 data points will not affect the accuracy of the model evaluation. As shown in Table 3, we used the simultaneously recorded environmental temperature, humidity, illuminance, and other data as the X data at the inputs and the tail temperature of the cattle as the Y data to perform machine learning to obtain the model, and then we used the test data to evaluate the system using R 2 . As can be seen by comparing Tables 1 and 3, we added the measured illuminance in the measurement stage and used it as a parameter to build the model and evaluate it at the same time.
In this experiment, a prototype system for estimating the body temperature of cattle was prepared and evaluated using three Japanese Black cattle. However, owing to problems such as signal interference between devices, in order to improve the measurement accuracy of the experiment, we carried out two sets of experiments separately, each time using two of the three cattle as the experimental objects in the experiment. The first group (Group 1) used the No.

Examination of the validity of simulating cattle temperature information in the laboratory
A total of 141 sets of data were input into the prediction models mentioned above to verify the validity of the model. Figure 2 shows an example of the estimated body temperature of  Table 3 Data structure of the model in the cattle house. As inputs of X As input of Y IR temperature Tail temperature Air temperature Humidity Illuminance an experimental cow. It can be seen that both the linear and quadratic regression models fluctuate more than the temperature measured using the body surface temperature sensor, but show a temperature closer to the true value than to the original IR data. R 2 between the predicted value and the true value (tail temperature) was calculated as shown in Table 6. The results show that all these models improved R 2 . The polynomial (quadratic) regression model performed best with R 2 = 0.135. However, it is considered that the results must still be improved to estimate the deep body temperature accurately. In comparison with the initial results after the machine learning, R 2 and the estimation accuracy have improved. Therefore, it is believed that this method could improve the ability to estimate the body temperature to a certain extent, such that the body temperature of cattle can be measured in a noncontact manner. Although R 2 at the initial stage is low, according to the simulation experiments in the laboratory, the  results are shown below. First, regarding the selection of measurement data, it is known that the environmental temperature, humidity, and IR images collected are all important factors for estimating the deep body temperature and must be measured in the actual cattle house. Furthermore, the animals sometimes move, causing the shooting angle of IR thermography to change. It is necessary to let the IR camera take as many images of the cattle to make an accurate observation. We also know that it is difficult to fully simulate an actual cattle house in the laboratory. The heat source experiment is a simulator for real cattle, which can partly simulate the real situation in a cattle house. The number of parameters that can be controlled in a laboratory is limited. There are many factors that may affect the body temperature of cattle, such as moving distance, atmospheric pressure, wind speed, and illuminance. In addition, the temperature of cattle will change when they drink water and eat. Clearly, it is difficult to simulate these behaviors using the device.
In conclusion, although it is difficult to completely simulate cattle and their living environment in the laboratory, and more factors that may affect the estimation results must be considered, from the results of the laboratory simulation, the machine learning method increased R 2 , thus improving the estimation of the deep body temperature. At the same time, it also played a positive role in the actual field measurement. We generally understand the factors that have an impact on body temperature estimation and know the precautions that must be taken in actual measurements. Tables 7 and 8 show the results of Group 1 and Group 2, respectively. The test data were input to evaluate both models. The input of Y (tail temperature) was initially hidden, then the predicted value was compared with the input of Y. The R 2 of the predicted value to the true value (tail temperature) was calculated. It is shown that R 2 clearly increases in the two groups. The best R 2 was as high as 0.403, which is about five times higher than the original result. The polynomial regression models have better performance than the linear regression models, consistent with the preliminary result mentioned in Sect. 3.1. Compared with the model based on the data obtained from the laboratory, the model based on the measurement results obtained from the actual cattle house is closer to the actual situation, so the estimated result is greatly improved. Obviously, the higher the accuracy of the estimation of the deep body temperature, the more accurately the health of animals can be determined. A much improved accuracy may be achieved by increasing the use of sensors, calculation time, and so forth and in future research, we will also work to improve the estimation accuracy, thereby ensuring more accurate measurements. The estimation accuracy of the IR body temperature prediction model was also improved significantly for both groups. The estimation accuracy was also greatly reduced to ± 0.5 °C. These results prove that the proposed system can be used for multiple cattle. Moreover, the results prove that the improvements in the cattle monitoring experiment and IR imaging were efficient. Narrowing the interval between the shooting of IR images increased the amount of data and made the data more continuous. Since the real body temperature does not fluctuate sharply, a continuous IR temperature is originally more similar to the reference temperature. However, R 2 and the estimation accuracy are still low. It is believed that more efforts and attempts are needed in image processing. In this experiment, when we collected the IR temperature, the whole body of the cattle was used. This may lead to an insufficient measurement accuracy. In future studies, we will consider the measurement of specific body parts, which may improve R 2 and the estimation accuracy. It is also found that those models built using the data from two cattle had unstable performance in the estimation of only one of them. For example, if the estimation model made using the data from Group 1 (Nos. 64 and 70) is used to estimate the temperature of the No. 70 cow, the best R 2 is only 0.221. The result proves that it is difficult to build a common estimation model for multiple cattle. The most possible reason is the individual difference of cattle. Until now, the method used to ensure a high estimation accuracy is to establish estimation models and then use them to estimate the respective body temperature of the cattle. However, in the following research, as the measurement time and the amount of measurement data (e.g., T air , Hum, and T IR ) increase, a common estimation model is possible to improve the estimation accuracy. Nevertheless, the method of using environmental sensors and a thermal imager to estimate the deep body temperature of cattle in a noncontact manner is still a significant improvement.

Experiments in cattle house
In conclusion, the suitability of AI body temperature monitoring for multiple cattle and the estimation system has been proved, but the common model remains ineffective owing to individual differences among cattle. Although R 2 and the estimation accuracy of the system have been increased, it is still necessary to improve them further by determining other factors that may affect body temperature in future research.

Conclusions
In this research, we collected the environmental temperature, humidity, illuminance, and IR images of the body surface of cattle in a cattle house, and developed an AI characterization module that estimates the deep body temperature of cattle in a noncontact manner using AI technology for the first time. The possibility of realizing such a module to monitor the health of cattle was verified using this system. The conclusions are summarized as follows.
(1) A laboratory model was developed and compared with the initial results, and R 2 has been improved from 0.03 to 0.135 after machine learning using the polynomial (quadratic) regression model, which shows the feasibility of the noncontact estimation of cattle body temperatures. (2) By using this system to test three cattle, R 2 = 0.403 was obtained and the estimation accuracy reached about ± 0.5 °C after machine learning. These results show that the deep body temperature of cattle can be inferred to some extent and that the health of cattle can be predicted by collecting data on the actual environment of cattle.