An Approach towards a Practicable Assessment of Neonatal Piglet Body Core Temperature Using Automatic Object Detection Based on Thermal Images

: Body core temperature (BCT) is an important characteristic for the vitality of pigs. Suboptimal BCT might indicate or lead to increased stress or diseases. Thermal imaging technologies offer the opportunity to determine BCT in a non-invasive, stress-free way, potentially reducing the manual effort. The current approaches often use multiple close-up images of different parts of the body to estimate the rectal temperature, which is laborious under practical farming conditions. Additionally, images need to be manually annotated for the regions of interest inside the manufacturer’s software. Our approach only needs a single (top view) thermal image of a piglet to automatically estimate the BCT. We ﬁrst trained a convolutional neural network for the detection of the relevant areas, followed by a background segmentation using the Otsu algorithm to generate precise mean, median, and max temperatures of each detected area. The best ﬁt of our method had an R 2 = 0.774. The standardized setup consists of a “FLIROnePro” attached to an Android tablet. To sum up, this approach could be an appropriate tool for animal monitoring under commercial and research farming conditions.

The major advantage of thermal images is that they are non-contact and quick to acquire; thus, they can protect animals from unnecessary stress, or in the worst case, disease cross-transmission, by measuring the temperature with a rectal thermometer [10].However, thermal cameras with good measurement accuracy are mostly very expensive, and thus their advantages over traditional digital rectal thermometers within a practical farm's daily life are doubtful.In addition, approaches for the estimation of rectal temperature with thermal images are often based on multiple, manually recorded close-up images from different body parts, so called thermal windows [10,11], which can reduce the possible stress and time-saving benefits because multiple recordings close to the animal have to be taken by the personnel.Furthermore, unlike IR pyrometers (point measurement devices), a thermal image does not immediately provide the desired surface temperature without a manual effort.Several factors, such as the emissivity value of the object's surface and the ambient climate information, need to be manually adjusted, and the relevant parts of the body have to be marked inside the manufacturer's software to achieve accurate values of different body parts, e.g., maximum, minimum, and average, which are needed for the regression of the rectal temperature.
The BCT can be used as a hint for several diseases or hormonal changes, such as mastitis, metritis, and agalactia (MMA) [12], the onset of farrowing in sows [13], or, for example, classical fever in fattening pigs [1].Further, thermal images were used to detect inflammation [14] and lesions [15], which are linked to isolated surface temperature deviations.
For neonatal piglets, especially for small ones [16], the BCT is a very important characteristic.Due to their lack of insulation [17], the temperature gradient between the ambient temperature and the BCT during the first hours of life leads to a loss of the BCT.This can result in reduced mobility and a decreased ability to compete for colostrum and, therefore, a reduced colostrum intake, which then increases the likelihood of starvation, diseases, and crushing [18].For example, [19] and [20] found a decrease of 4-5 • C in the piglet's BCT when they failed to suckle during the first 30 min of life.After 24 h of life, vital piglets should have reached their normal thermal homeostasis of about 38-39 • C again [21,22].
Therefore, the surveillance of a neonatal piglet's surface temperature could help to detect individuals with lower survivability and enable early and targeted manual assistance from animal care personnel.Since this and the manual, continuous surveillance of fattening pigs for fever or lesions can be very time-consuming, a practicable time-saving solution is needed.
This study has two goals.The first is to automatically combine the relevant information of the thermal image and its acquisition, followed by the detection of the piglet's body parts, to generate features for the second goal, which is the regression of the rectal temperature by only one top-view image.
To make the approach economically implementable on a practical farm, a comparatively cheap thermal camera (FlirOnePro for Android) was chosen.All computations of the present approach are performed by the free statistical software "R" [23] (Version 4.2.2) and implemented in the package "ThermalpigR" [24].

Materials and Methods
Based on our dataset of thermal images, the workflow of the study was organized as follows: Firstly, the ambient climate information are measured by an external sensor, the emissivity value adjustment, and the distance to the object and the readout of the radiometric data of the images are accumulated and computed to pixel-wise temperature values.Secondly, the machine learning methods of object detection (Yolov3-SPP) [25] and background subtraction (Otsu algorithm) [26] are used to automatically extract the regression features (e.g., mean and max of different body parts).Thirdly, a linear regression approach, based on the extracted features and transformations of them, is comprehensively tested to improve the estimation of the rectal temperature.

Data Acquisition
The thermal images of neonatal piglets were recorded at the experimental farm (Relliehausen) of the University of Göttingen with interruptions (COVID-19) in three runs from March 2020 to August 2020.The sows (11 in total) were housed in a common farrowing crate, had genetics of German Landrace × Large White (Viktoria-Bundeshybridzuchtpro gramm), and were all between the 2nd and 5th farrowing cycle.After 30 min of life, the piglets were removed from the pen and examined.The examinations took place according to a standardized setup (Figure 1).The piglets were examined as they were and not dry rubbed.Directly after recording the animal's individual top-view thermal image with a "FLIROnePro" (Teledyne FLIR LLC) [27] connected to an Android tablet for data transfer, the rectal temperature was measured with a rectal thermometer "SC 1080" (SCALA Electronic) [28].According to the manufacturer, the accuracy of the camera is ± 5% in a temperature range of 15-35 • C for the camera and 5-120 • C for the image scene [27].
Before an image was taken, the camera was calibrated by manually starting the automatic calibration according to the description of the manufacturer [27].When the neonatal piglets were measured, the camera was connected to a power supply and the battery was loaded until 100% or until a new piglet was born.In addition, the piglet's bodies were measured with a measuring tape for body length and chest girth (Figure 1b) and were weighted with a digital table scale (readability of 5 g).After the examination, they were given an ear tag for identification.During data acquisition, ambient temperature and humidity were measured inside each pen using a "TGP-4500" climate data logger (Gemini Data Loggers) (accuracy for ambient temperature is 0.45 • C and for humidity ± 3% at 25 • C [29]) every 15 min.In total, 160 images of 64 piglets could be used for further analysis.
Agriculture 2023, 13, x FOR PEER REVIEW 3 of 17 tablet for data transfer, the rectal temperature was measured with a rectal thermometer "SC 1080" (SCALA Electronic) [28].According to the manufacturer, the accuracy of the camera is ± 5% in a temperature range of 15-35°C for the camera and 5-120°C for the image scene [27].Before an image was taken, the camera was calibrated by manually starting the automatic calibration according to the description of the manufacturer [27].When the neonatal piglets were measured, the camera was connected to a power supply and the battery was loaded until 100% or until a new piglet was born.In addition, the piglet's bodies were measured with a measuring tape for body length and chest girth (Figure 1b) and were weighted with a digital table scale (readability of 5g).After the examination, they were given an ear tag for identification.During data acquisition, ambient temperature and humidity were measured inside each pen using a "TGP-4500" climate data logger (Gemini Data Loggers) (accuracy for ambient temperature is 0.45° C and for humidity ± 3% at 25° C [29]) every 15 min.In total, 160 images of 64 piglets could be used for further analysis.

Temperature Determination
The temperature  of each pixel was calculated from the radiation through the formulae of [30].

Temperature Determination
The temperature T of each pixel was calculated from the radiation through the formulae of [30].
where, B, R 1 , R 2 , O, and F are calibration constants [30].The object s radiation W = W obj is a function of the transmission value ( τ), the emissivity value (ε), the radiation of the atmosphere (W atm ), and the reflected radiation (W re f l , The emissivity value (ε) of the piglet's skin was set to 0.98 [31].The transmission value is calculated by the following formula: where X, α 1 , α 2 , β 1 , and β 2 are constants from the camera calibration, which are saved to the metadata of the image.The variable d is the distance to the image, which was standardized to 50 cm in this study.Finally, the vapor content H is calculated from the environmental temperature and the relative humidity: where H rel (0,1) is the relative humidity, and T atm is the environmental temperature.The environmental temperatures and the relative humidity for the time, when a thermal image was recorded, were interpolated linearly from the 15 min measurements of the climate data logger.

Object Detection and Feature Extraction
For training the object detection of the four classes (piglet, head, back, and back end), all 160 images were annotated in YOLO format by one expert using the Roboflow annotation tool [32].A description and examples of annotation of the body parts are given in Table 1.If a body part is not completely inside an image, the body part is still annotated for its visible part.On seven images, the head was not completely inside the image and on one image the back end.
After splitting the data randomly into training, validation, and test sets (70/20/10ratio), data augmentation methods (random horizontal, vertical flip, or rotate 90 • , 180 • , and 270 • ) were used (up to three times per image) to enlarge the training set to 316 images (validation and test set still containing 32 and 16 images, respectively).The convolutional deep neural network YOLOv3 [33] with spatial pyramid pooling block (SPP) [25,34] was trained via transfer learning from "Microsoft COCO" dataset's [35] pretrained weights at a learning rate of 0.001 for 300 epochs with default hyperparameters (only number of classes was adapted and the batch size was set to 16) and evaluated afterwards, both via Google Colab [36].An SPP block prevents images that have a different input size than required for the fully connected layers from being cropped or resized before the convolutional step.This ensures that all input images, regardless of their size, are neither distorted nor cut in such a way that all features are no longer visible.This enables multi-scale feature extraction, which leads to an improvement in performance in most cases [34].A control logic to check if all four body parts are detected exactly once by YOLOv3-SPP was also implemented.An image where a body part is not detected has not been considered for further analysis.If a body part is detected several times, the detection with the highest confidence score is chosen.To measure the performance of the detection model, the common metrices precision (P), recall (R), and mean average precision (mAP), which is the arithmetic mean of the average precision (AP), were used: where TP denotes the number of true positives, FP is the number of false positives, and FN is the number of false negatives for precision and recall.Furthermore, AP is calculated as the integral from 0 to 1 over the precision p of the recall r.These metrices were calculated with a confidence score = 0.1 and a non-maximum suppression (NMS) = 0.6 on the validation set.The best model was chosen using a fitness score f, f = 0.01R τ con f + 0.99mAP@0.5(8) where R (τ con f ) is the recall at the specific confidence threshold and mAP@0.5 is the mAP with an interception over union of ≥0.5.The computation of the final model works with and without a CUDA-compatible GPU for user-friendliness.
Table 1.Description and example images of body part annotation using bounding boxes with Roboflow annotation tool [32].

Annotation Description Examples
Piglet A rectangle bounding box fitting the whole piglet with legs, snout, and tail inside.
where  denotes the number of true positives,  is the number of false positives, and  is the number of false negatives for precision and recall.Furthermore, AP is calculated as the integral from 0 to 1 over the precision p of the recall r.These metrices were calculated with a confidence score = 0.1 and a non-maximum suppression (NMS) = 0.6 on the validation set.The best model was chosen using a fitness score f, f = 0.01  (  ) + 0.99@0.5(8) where R (  ) is the recall at the specific confidence threshold and mAP@0.5 is the mAP with an interception over union of ≥0.5.The computation of the final model works with and without a CUDA-compatible GPU for user-friendliness.
After inferring all 160 images with this model, 150 images possessed all five body parts and stayed in the dataset for further analysis.To generate accurate mean and maximum temperature values for the detected body parts, the body part shapes were segmented from the background using the Otsu algorithm [26].See Figure 2 for details.Whereby, the intersections of the detected bounding box area and the segmented foreground area contain the pixels that were used for further calculations.
Table 1.Description and example images of body part annotation using bounding boxes with Roboflow annotation tool [32].

Annotation Description Examples
Piglet A rectangle bounding box fitting the whole piglet with legs, snout, and tail inside. is the number of false negatives for precision and recall.Furthermore, AP is calculated as the integral from 0 to 1 over the precision p of the recall r.These metrices were calculated with a confidence score = 0.1 and a non-maximum suppression (NMS) = 0.6 on the validation set.The best model was chosen using a fitness score f, f = 0.01  (  ) + 0.99@0.5 where R (  ) is the recall at the specific confidence threshold and mAP@0.5 is the mAP with an interception over union of ≥0.5.The computation of the final model works with and without a CUDA-compatible GPU for user-friendliness.
After inferring all 160 images with this model, 150 images possessed all five body parts and stayed in the dataset for further analysis.To generate accurate mean and maximum temperature values for the detected body parts, the body part shapes were segmented from the background using the Otsu algorithm [26].See Figure 2 for details.Whereby, the intersections of the detected bounding box area and the segmented foreground area contain the pixels that were used for further calculations.
Table 1.Description and example images of body part annotation using bounding boxes with Roboflow annotation tool [32].

Annotation Description Examples
Piglet A rectangle bounding box fitting the whole piglet with legs, snout, and tail inside.as the integral from 0 to 1 over the precision p of the recall r.These metrices were calculated with a confidence score = 0.1 and a non-maximum suppression (NMS) = 0.6 on the validation set.The best model was chosen using a fitness score f, f = 0.01  (  ) + 0.99@0.5 where R (  ) is the recall at the specific confidence threshold and mAP@0.5 is the mAP with an interception over union of ≥0.5.The computation of the final model works with and without a CUDA-compatible GPU for user-friendliness.
After inferring all 160 images with this model, 150 images possessed all five body parts and stayed in the dataset for further analysis.To generate accurate mean and maximum temperature values for the detected body parts, the body part shapes were segmented from the background using the Otsu algorithm [26].See Figure 2 for details.Whereby, the intersections of the detected bounding box area and the segmented foreground area contain the pixels that were used for further calculations.
Table 1.Description and example images of body part annotation using bounding boxes with Roboflow annotation tool [32].

Annotation Description Examples
Piglet A rectangle bounding box fitting the whole piglet with legs, snout, and tail inside.After inferring all 160 images with this model, 150 images possessed all five body parts and stayed in the dataset for further analysis.To generate accurate mean and maximum temperature values for the detected body parts, the body part shapes were segmented from the background using the Otsu algorithm [26].See Figure 2 for details.Whereby, the intersections of the detected bounding box area and the segmented foreground area contain the pixels that were used for further calculations.shows the overlap of the white foreground area (c) and the detected "head bounding box area" from (a).The pixels inside the red area are used for further calculations of the head.

Implementation
The complete workflow (Figure 3) is implemented in the R package "ThermalPigR" [24] using Python 3.8 besides R.
The package has several integrated functions which perform the following functions: • Combine the data from the ambient climate of each pen and match it with the thermal images.

•
Adjust the distance from the camera to the object and adjust the emissivity value of the object´s surface (Formulas (1)-( 3)).

•
Generate the features for regression models.
The images were processed using the R package "thermimage" [37].The metadata of the radiometric JPEG images was imported using the function "flirsettings" and the radiometric values of each pixel were imported using the function "readflirJPG".The regressions are performed with lsfit [23].

Implementation
The complete workflow (Figure 3) is implemented in the R package "ThermalPigR" [24] using Python 3.8 besides R. shows the overlap of the white foreground area (c) and the detected "head bounding box area" from (a).The pixels inside the red area are used for further calculations of the head.

Implementation
The complete workflow (Figure 3) is implemented in the R package "ThermalPigR" [24] using Python 3.8 besides R.
The package has several integrated functions which perform the following functions: • Combine the data from the ambient climate of each pen and match it with the thermal images.

•
Adjust the distance from the camera to the object and adjust the emissivity value of the object´s surface (Formulas ( 1)-( 3)).

•
Generate the features for regression models.
The images were processed using the R package "thermimage" [37].The metadata of the radiometric JPEG images was imported using the function "flirsettings" and the radiometric values of each pixel were imported using the function "readflirJPG".The regressions are performed with lsfit [23].The package has several integrated functions which perform the following functions:

•
Combine the data from the ambient climate of each pen and match it with the thermal images.

•
Adjust the distance from the camera to the object and adjust the emissivity value of the object s surface (Formulas (1)-( 3)).

•
Generate the features for regression models.
The images were processed using the R package "thermimage" [37].The metadata of the radiometric JPEG images was imported using the function "flirsettings" and the radiometric values of each pixel were imported using the function "readflirJPG".The regressions are performed with lsfit [23].

Data Anaysis
First, a rough outlier detection of the thermal images was performed by considering the highest 10 % difference between max head surface temperature (highest correlation to the rectal temperature (Figure 4)) and rectal temperature (15 outliers found).Additionally, all piglets with a rectal temperature <34 • C were sorted out (one outlier found) (Figure 5).The remaining 134 images were used to fit the linear regression models.A total of 62 features were examined, including the maximum, median, and mean temperature values of the body parts (12 features), the square of maximum and mean for each body part (8 features), the 99th, 95th, and 90th quantiles for each body part (12 features), as well as the consideration of all pairwise absolute temperature differences as further features (e.g., |mb_mbe| for the difference between "max_back" and "max_be") (28 features).In cases where the algebraic sign of the differences does not change in the dataset, the three features   Additionally, the interpolated environmental variables, relative humidity, and ambient temperature were used (Table 2).To find the best combination, a Best Subset Selection (BSS) [38] for 1-9 variables plus an intercept was performed using the AICc as stop criterion.
For testing potential improvements in the regression, the manually measured features' body length and girth were additionally applied.Since length and girth are not automatically given, a simple linear regression of body length and girth with the Euclidean distance in pixels from the thermal images as features was also attempted.All regression models were checked for performance using the metrices root mean square error (RMSE), the coefficient of determination (R 2 ), and a variation of the Akaike information criterion for small sample sizes (AICc) ( 9)- (11).
Here, y i is the measured value and ŷi is the prediction of the model.Thus, the RMSE has the same unit and scale as the variable being estimated.Additionally, the coefficient of determination R 2 was computed to show how well the rectal temperature is explained by a model: where y i corresponds to the mean of the measured values y i .To address the substantial risk of AIC selecting models that have too many variables, we chose the model by taking the one with the lowest AICc (AIC for small sample sizes): where AIC is defined as AIC = −2ln(L) + 2p, L denotes the log-likelihood estimate, n denotes the sample size, and p denotes the number of variables.

Object Detection
The training progress of the YOLOv3-SPP model can be seen in Figure 6.The evaluation of the model is performed after each of the 300 epochs on the validation set (32 images).After 50 epochs, the model is already very good, with a precision of ~0.78, a recall of ~0.98, and a mAP of ~0.95.After 300 epochs, the best model was chosen by taking the model with the highest fitness score (8) for the validation set, which was the model after 238 epochs.Finally, YOLOv3-SPP was able to detect the trained body parts with a precision of 0.78, a recall of 0.98, and a mAP@0.5 of 0.97.
For final testing on the test set, the parameter's confidence score and NMS were adapted to conf = 0.3 and NMS = 0.1.In the 15 test set images, all the body parts were detected at one time except for one image, where the back was not detected.These settings are chosen for the final model.For 160 images, an 8GB ram storage laptop with Intel Core i5-8265U CPU @ 1.6 GHz needed 103 s to run the model (~0.64 s/image).

Rectal Temperature Regression
As can be seen in Table 3 the simple linear regression of the max values of the areas "head" and "piglet" show promising results.As mentioned before, the features "max head" and "max piglet" have highest correlation (both 0.81) with the rectal temperature (Figure 4).The best model, acording to AICc (436.7), was the model with eight features, including "max_head", "max_be", "max_be 2 ", "air humidity", "T_mb_mbe", "Q_pig99", "Q_head95", and "T_mp_ab".This model has an R 2 = 0.774 and an RMSE = 0.406.The model with nine features had an AICc of 437.5.The best models for a fixed number of features (1-8) plus an intercept can be found in Table 4.The regression results of the overall best model (8 features) can be seen in Figure 7.
238 epochs.Finally, YOLOv3-SPP was able to detect the trained body parts with a precision of 0.78, a recall of 0.98, and a mAP@0.5 of 0.97.
For final testing on the test set, the parameter's confidence score and NMS were adapted to conf = 0.3 and NMS = 0.1.In the 15 test set images, all the body parts were detected at one time except for one image, where the back was not detected.These settings are chosen for the final model.For 160 images, an 8GB ram storage laptop with Intel Core i5-8265U CPU @ 1.6 GHz needed 103 s to run the model (~0.64 s/image).Table 3. Results of simple linear regression of the max values of body features on rectal temperature.R 2 is the coefficient of determination, RMSE is the root mean square error, and AICc is a variation of the Akaike information criterion for small sample sizes.y represents the rectal temperature and x the max value of the feature.

Discussion
In this study, an approach toward a practicable assessment of a neonatal piglet´ body temperature using object detection on thermal images was presented.The ad vantages are that only one top-view image per piglet is needed and that the evaluation o the image is automated.The standardized setup can provide images when the physica circumstances during image acquisition (distance to the object, object emissivity, air hu midity, and air temperature) are stable and can be easily adapted, respectively, and meas

Discussion
In this study, an approach toward a practicable assessment of a neonatal piglet s body temperature using object detection on thermal images was presented.The advantages are that only one top-view image per piglet is needed and that the evaluation of the image is automated.The standardized setup can provide images when the physical circumstances during image acquisition (distance to the object, object emissivity, air humidity, and air temperature) are stable and can be easily adapted, respectively, and measured automatically by other sensors.The usage of object detection algorithms enables the automatic calculation of features for the regression of the rectal temperature.This can save time and reduce the manual effort for the practical or scientific surveillance and monitoring of pigs.All computations are integrated in the R package "ThermalPigR" [24].Nevertheless, this is only one possible approach, and the presented solution will be discussed in detail in the following sections.

Object Detection
In our opinion, the object detection results are sufficient to detect the piglet's body parts.They might be further improved by a larger dataset and better image quality (some body parts were cut off by the edge of the image).Unfortunately, data acquisition was terminated early by a fire at the experimental farm and could not be resumed.Originally, the collection of data on at least 300 piglets was planned.A different Deep Convolutional Neural Network (DCNN) could also provide improved results.YOLOv3 is now fairly old, and there are more recent DCNNs.Some of them are able to generate better results on typical competition datasets such as "Microsoft COCO" [35] or "ImageNet" [39], but some of them are also larger (have more layers) and thus need more computing time.We chose YOLOv3-SPP because it is well documented and has been published several times with similar tasks.Images of insufficient quality and those causing errors during detections were sorted out by the implemented control logic.
As described in Section 2.4 and shown in the examples in Table 1, bounding box detection has the disadvantage that the body parts and the background are located within a bounding box.This problem is solved by performing background segmentation using the established Otsu algorithm [26].Another approach could be via the use of polygonal annotation, where the objects are completely segmented by the DCNN.However, polygonal annotation has the disadvantage that it is much more time-consuming during the annotation process.

Region of Interest
As mentioned, many approaches to estimating the rectal temperature of pigs use so-called "thermal windows".A thermal window is a skin area perfused by blood that can be considered the "window" of body temperature [10].By contrast, a non-thermal window is a skin area that is covered with a thick layer of fat [2].By deciding to make our approach as practicable as possible and using only one image, no real "thermal window", such as the eye or the ear base, could be taken for the feature generation.However, [10] also stated that neonatal piglets, on the contrary to older pigs, may be looked upon as one large thermal window, where the skin temperature is almost uniform throughout the body surface due to their lack of insulation.This fact and the suggestion that nearly all piglets should be in a similar hormonal situation after 30 min of life because they have to overcome the same physiological challenges [40] might explain why the correlation of the maximum surface temperatures of the different body parts with the rectal temperature is very high in our study.
Tabuaciri et al. measured the average surface temperature of a crown to rump line and found a correlation of 0.75 with the rectal temperature of piglets (within 24 h of life), which is confirmed by the correlations of the max back (0.72) and the max back end (0.73) with the rectal temperature in our study.The highest correlations were found for the ear base (0.85 average of left and right) by [41].In our study, we had the highest correlations for the max head and max piglet (both 0.81); however, the ear base was not specifically annotated.This can be explained by the fact that the max temperature of the head or the whole piglet, of course, may contain the ear base temperature if it is visible.
For the other pigs, Ref. [10] stated the influence of physiological differences such as age, the hormonal cycle, or stress on the surface temperature.Especially for piglets, Ref. [42] found significant results for the highest surface temperature after vaccination (day 14) and a lower surface temperature for older piglets (24 days) than younger piglets (days 4-18).

Surface Temperature Influencing Factors
Besides physiological influencing factors, the environment also has an impact on the surface temperature of the pigs.For example, the environmental temperature or wind can reduce the surface temperature of areas that are not well supplied with blood through convection [2].The same is also true for, e.g., the resting areas of the animals.If there is high thermal conductivity in the materials, it can lead to a large deviation of the animal s surface temperature in the areas that are in contact with the material due to heat conduction, depending on the temperature difference [6].Nevertheless, the influence of wind and environmental surfaces can be ignored in our study.The experimental compartment was ventilated by a perforated ceiling, which has the beneficial attribute of low wind speeds.The heat conduction through the environmental surface is not relevant for the body parts that were examined (back, back end, and head from a top view).Thus, for settings other than ours, e.g., the outdoor climatic conditions in husbandry systems, these factors need to be integrated into the evaluation.

Regression
The simple linear regressions for the "max_head" and the "max_piglet" already provide good results (head: R 2 = 0.651, RMSE = 0.507; piglet: R 2 = 0.634, RMSE = 0.52).The authors of [2] found similar results for simple regression attempts for different body parts of sows where the RMSE was equal to 0.42 and 0.51 for the average ear base and max shoulder temperature, respectively.The variable selection for multiple linear regression was guided by the laborious but precise BSS method [38].To keep computing times reasonable, we performed the BSS up to seven features plus the intercept.For more than seven features we always kept two features fixed.These two fixed variables are either "max_head" and its square or "max_be" and its square, as either pair shows up in all top ten fits for 5-7 variables.As assistance to the original features, we calculated absolute temperature differences that were similar to [2].These new features provide additional information about the body core temperature, in cases where the temperature differences show different algebraic signs.When the temperature difference of two fixed body parts of all the pigs always has the same sign, the newly created feature might still be of interest as it is interpretable, but it might reduce the number of features in the model.In the best model, the feature "max_be" had the highest influence (−11.96),followed by features "Q_pig99" (+1.291), "Q_head95" (−0.9839), "max_head" (+0.9705), and "T_mb_mbe" (−0.7446).The features "max_be 2 " (+0.171), "T_mp_ab" (+0.1257), and "air humidity" (−0.00712) had the lowest influence.
Further approaches to improving the regression by supplying additional features (manually measured piglet body length and chest girth), which describe the body constitution of the piglets, improved the fit to R 2 = 0.778 and RMSE = 0.405 when only six features were included.Indeed, both the body length (R 2 = 0.29) and the chest girth (R 2 = 0.47) are positively correlated to the rectal temperature, and therefore, they are beneficial for the regression model.However, these manually measured features are not given under practical conditions; thus, these two quantities were not considered in this study.Approaches that estimated the body length and chest girth from the pixel distances measured from the images with a simple linear regression did not work well (R 2 = 0.11 (girth) and R 2 = 0.05 (length)).This might be a result of the inconsistent position of the piglets during the image acquisition.With a standardized body position, the regression of the body length and girth should perform much better, as Ref. [43] described in their study, concerning the estimation of the bodyweight of pigs from images.The model that includes the length and girth can be found in the Appendix A (Table A1).To sum up, a rather sophisticated feature selection was performed, and an R 2 = 0.774 was the best model found.The greatest limitation of a model with higher accuracy is the occurrence of inadequacies during the acquisition of the surface temperature.The measuring accuracy of the camera and the degree of moisture on the surface of the piglets should be the main factors.More data of the same quality should also not significantly improve the model anymore, since the overall performance seems to be limited to the inadequacies during the acquisition of the surface temperature.

Future Research and Usage
Generally, our approach is well suited for other studies that include the evaluation of thermal images.However, if a thermal camera from another manufacturer is included in new studies, the Formulas (1)-( 3) must be adapted.
Since the aim of this study was to create a practical measuring tool, we decided to use the comparatively cheap thermal imaging camera "FLIR One Pro".In addition to the lower accuracy compared to the more expensive thermal imaging cameras, the battery life and simultaneous charging and recording of the thermal images, or operation without a battery, are not possible, which renders continuous automatic data recording impossible.Furthermore, the camera is not suitable for permanent operation in a pig pen, as no protection against moisture or dust is guaranteed.However, a protection case could probably solve this problem.
In practical farming, for neonatal piglets, this approach might be used to detect piglets that are suffering from deep hypothermia (32-34 • C [44]) without the need to measure their rectal temperature, which can save additional stress on the fragile piglets.The ones who are suffering from hypothermia could be assisted in achieving access to the colostrum and placed under a heatlamp afterwards.Moreover, this approach might be used for continuously monitoring the body core temperature of fattening pigs in group pens with a sorting gate, individual feeding troughs, or drinking stations with an individual radiofrequency identification (RFID).For such an application of the algorithm presented, the following adaptations are necessary: As mentioned, the surface temperature of the pigs is altered in correlation to the body core temperature when they age, due to more fat layers and less peripheral blood flow.If the approach should remain a "one topview image" solution, more research on the surface temperature of pigs with different body weights and breeds is needed.Otherwise, an approach using "thermal windows" could also be a solution.Therefore, the object detector needs to be trained to detect, for example, the eyes of pigs.Whether a simple or multiple linear regression should be used depends on the feature selection.

Conclusions
In conclusion, a practicable approach for an automatic evaluation of the thermal images from neonatal piglets is presented.An R package has been developed and is available online.Integrated functions accumulate data from climate sensors with the original thermal images, while considering the specific emissivity values of the examined object's surfaces and the distances of the camera to the object to generate precise thermal images.It uses a CDNN (YOLOv3-SPP) to automatically detect the regions of interest of a single top-view image of piglets and separates the regions from the background using the Otsu algorithm.Finally, regression features for multiple regressions are generated, and regressions for the body core temperature can be computed.The whole approach can be navigated and controlled with R, while object detection is performed by Python.

Figure 1 .
Figure 1.(a) Setup of thermal imaging, animal's individual top-view thermal images were recorded in a standardized distance, and (b) manual measurement of body length (A to B) and chest girth (C).(c) Example of a thermal image, where dark-blue/purple indicates low temperature and yellow/white indicates high temperature.

Figure 1 .
Figure 1.(a) Setup of thermal imaging, animal's individual top-view thermal images were recorded in a standardized distance, and (b) manual measurement of body length (A to B) and chest girth (C).(c) Example of a thermal image, where dark-blue/purple indicates low temperature and yellow/white indicates high temperature.

HeadA
rectangle bounding box fitting the piglet's head.The ears and snout are inside the box if visible and the shoulders are building the border to the back.Back A rectangle bounding box fitting the piglet's back.The shoulders and the hips are building the borders for the box.Back end A rectangle bounding box fitting the piglet's back end.The Head A rectangle bounding box fitting the piglet's head.The ears and snout are inside the box if visible and the shoulders are building the border to the back.
Head A rectangle bounding box fitting the piglet's head.The ears and snout are inside the box if visible and the shoulders are building the border to the back.Back A rectangle bounding box fitting the piglet's back.The shoulders and the hips are building the borders for the box.Back end A rectangle bounding box fitting the piglet's back end.The Back A rectangle bounding box fitting the piglet's back.The shoulders and the hips are building the borders for the box.
Head A rectangle bounding box fitting the piglet's head.The ears and snout are inside the box if visible and the shoulders are building the border to the back.Back A rectangle bounding box fitting the piglet's back.The shoulders and the hips are building the borders for the box.Back end A rectangle bounding box fitting the piglet's back end.The Back end A rectangle bounding box fitting the piglet's back end.The hips and the tail are inside the box if visible.Agriculture 2023, 13, x FOR PEER REVIEW 6 of 17 hips and the tail are inside the box if visible.

17 Figure 2 .
Figure 2. Example Otsu algorithm for the head of a piglet: (a) shows the four bounding box detections of YOLOv3-SPP; (b) is an image of the area "piglet" after gray value conversion (0-255); (c) shows a binary image of the piglet, after thresholding with Otsu algorithm on image (b).Image (d) shows the overlap of the white foreground area (c) and the detected "head bounding box area" from (a).The pixels inside the red area are used for further calculations of the head.

Figure 2 .
Figure 2. Example Otsu algorithm for the head of a piglet: (a) shows the four bounding box detections of YOLOv3-SPP; (b) is an image of the area "piglet" after gray value conversion (0-255); (c) shows a binary image of the piglet, after thresholding with Otsu algorithm on image (b).Image (d) shows the overlap of the white foreground area (c) and the detected "head bounding box area" from (a).The pixels inside the red area are used for further calculations of the head.

Figure 2 .
Figure 2. Example Otsu algorithm for the head of a piglet: (a) shows the four bounding box detections of YOLOv3-SPP; (b) is an image of the area "piglet" after gray value conversion (0-255); (c) shows a binary image of the piglet, after thresholding with Otsu algorithm on image (b).Image (d) shows the overlap of the white foreground area (c) and the detected "head bounding box area" from (a).The pixels inside the red area are used for further calculations of the head.

Figure 3 .
Figure 3. Workflow chart of the R package "ThermalPigR".On the left is the original data from data acquisition.In the middle, the computation is performed by R and Python (object detection).After feature calculation the rectal temperature of the piglets is estimated (right site).

Figure 4 .
Figure 4. Pearson correlations of the rectal temperature (Rkt) and the body parts (mean and max) (upper right), and a histogram (diagonal) and scatterplot (bottom left) of the body parts.All captions are in °C.

Figure 4 .
Figure 4. Pearson correlations of the rectal temperature (Rkt) and the body parts (mean and max) (upper right), and a histogram (diagonal) and scatterplot (bottom left) of the body parts.All captions are in • C.

17 Figure 5 .
Figure 5. Outlier detection: blue cross indicates a rectal temperature < 34°C and red triangles indicate 10% of the biggest differences between rectal temperature and max surface temperature of the piglet's head.

Figure 5 .
Figure 5. Outlier detection: blue cross indicates a rectal temperature <34 • C and red triangles indicate 10% of the biggest differences between rectal temperature and max surface temperature of the piglet's head.

Figure 6 .
Figure 6.Progress of the fine-tuning process of the YOLOv3-SPP model on the training set (n = 316) for 300 epochs.The evaluation of the metrics precision, recall, and mAP is performed on the validation set (32 images).

Figure 6 .
Figure 6.Progress of the fine-tuning process of the YOLOv3-SPP model on the training set (n = 316) for 300 epochs.The evaluation of the metrics precision, recall, and mAP is performed on the validation set (32 images).

Figure 7 .
Figure 7. Results of the best multiple linear model (8 features) on all data.The blue lines highlight deviation of ± 0.5°C from the measured rectal temperature.

Figure 7 .
Figure 7. Results of the best multiple linear model (8 features) on all data.The blue lines highlight a deviation of ± 0.5 • C from the measured rectal temperature.

Table 2 .
Indexing of features.Where mb = max back, mbe = max back end, mh = max head, mp = max piglet, ab =average/mean back, abe = average/mean back end, ah = average/mean head, and ap = average/mean piglet.

Table 2 .
Indexing of features.Where mb = max back, mbe = max back end, mh = max head, mp = max piglet, ab =average/mean back, abe = average/mean back end, ah = average/mean head, and ap = average/mean piglet.

Table 4 .
The best multiple linear regression models of all features (62) on rectal temperature with varying numbers of features (1-8) plus intercept.R 2 is the coefficient of determination, RMSE is the root mean square error, and AICc is a variation of the Akaike information criterion for small sample sizes.
* Rounded to the 5th decimal place.Agriculture 2023, 13, x FOR PEER REVIEW 12 of 1