A method of pig tracking and detecting health status
By improving the YOLOv5 network and OCSort algorithm, and combining the CoordAttention and ASFF_Decet modules, the problem of poor recognition and tracking performance in traditional pig health monitoring was solved, achieving accurate identification and health status detection of pigs, and improving the recognition rate and tracking effect.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SOUTH CHINA AGRICULTURAL UNIVERSITY
- Filing Date
- 2023-08-15
- Publication Date
- 2026-05-19
AI Technical Summary
Traditional pig health monitoring relies on manual inspections and lacks intelligent management. Furthermore, existing technologies such as wearable sensors are prone to falling off and RFID devices are easily damaged, affecting the identification and tracking effect.
An improved YOLOv5 network is used for pig identification, combined with an improved OCSort multi-object tracking algorithm. The CoordAttention module is used to improve feature extraction capability, and the ASFF_Decet module is used in the detection network to suppress feature conflicts. A flexible weighting factor α is added to adjust the visual embedding ratio to achieve health status detection.
It improves the pig identification rate and tracking effect, enhances the robustness of the identification method, and can detect the health status of pigs in real time, making it practical.
Smart Images

Figure CN117218379B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of target detection, identification and tracking, and in particular to a method for tracking and detecting the health status of pigs. Background Technology
[0002] Pig health is related to many factors. Traditional pig health monitoring relies on manual inspections by farmers during the production process, observing the pigs' eating, drinking, excretion, posture, and facial expressions. If abnormalities are found, physiological tests such as body temperature measurement and blood tests are used to further diagnose the pigs' health. This method is highly dependent on the personal experience of the farmers and lacks intelligent management methods. Pig behavior is the language of pigs and one of the bases for diagnosing their health. Under normal environmental conditions, pigs exhibit normal behaviors, such as eating, drinking, and excretion. When pigs are sick, in addition to abnormal physiological data, their daily behaviors will also change accordingly, such as deviations in drinking and eating, and increased frequency of excretion. Automated pig behavior identification technology mainly uses sensor technology, RFID technology, and video monitoring technology. Wearable sensors have problems such as being prone to falling off and causing injury to pigs; Radio Frequency Identification (RFID) technology can identify specific targets and read and write data through radio signals. It can be used to detect fixed locations such as drinking and eating areas, but it can easily irritate pigs and even damage the equipment due to pig collisions. Summary of the Invention
[0003] The purpose of this invention is to overcome the shortcomings and deficiencies of the prior art and to propose a method for tracking and detecting the health status of pigs. This method can improve the recognition rate and tracking effect during the pig identification and tracking process, and can detect the health status of pigs, thus having certain practical value.
[0004] To achieve the above objectives, the technical solution provided by this invention is as follows: a method for tracking and detecting the health status of pigs. This method is based on an improved YOLOv5 network to achieve accurate identification of pigs, and then the identification results are fed into an improved OCSort multi-object tracking algorithm for tracking and health status determination. The improved YOLOv5 network is an improvement on the feature extraction and detection networks of the traditional YOLOv5 network. The improvement to the feature extraction network is achieved by adding a CoordAttention module after the CSPx module, which enhances the feature extraction capability. The improvement to the detection network is achieved by replacing the original Detect module with an ASFF_Decet module, aiming to suppress conflicting information between features at different levels and improve feature fusion performance. The improved OCSort multi-object tracking algorithm provides a flexible weighting factor α to adjust the ratio of visual embeddings from historical and current times when embedding the original appearance information into the tracking model, thereby improving the tracking performance of similar targets.
[0005] The specific implementation of this method includes the following steps:
[0006] S1. Obtain the initial images of pigs, label the pigs and behaviors in the initial images to obtain pig image data with labeled boxes, and then perform data augmentation processing on the pig image data to ensure that the image size is the same as the network input size. Divide the processed pig image data into training set, validation set and test set.
[0007] S2. Input the training set and validation set into the improved YOLOv5 network for training and validation. After training and validation, an optimal network that can automatically perform target detection and behavior detection on pigs in images is obtained.
[0008] S3. Input the test set into the optimal network to obtain pig videos with detection information, and then input them into the improved OCSort multi-object tracking algorithm. The same pigs between frames will be automatically tracked by the improved OCSort multi-object tracking algorithm.
[0009] S4. During the tracking process, a series of information on the pig's walking, eating, and lying down will be obtained as tracking results. Health judgment will be made based on the tracking results, and ultimately, real-time health monitoring of the pig will be achieved.
[0010] Furthermore, step S1 includes the following steps:
[0011] S101. The initial images for obtaining pig image data are collected by manual photography or video surveillance.
[0012] S102. Use the labelImg dataset format software to annotate the pig objects in the initial images. The behavioral annotations are divided into standing, eating, and lying down. Save the annotations in the YOLO image annotation format to obtain pig image data with annotation boxes.
[0013] S103. Perform data augmentation on the pig image data to ensure that the image size is the same as the network input size. Finally, segment the processed pig image data to generate a training set for training, a validation set for validation during training, and a test set for testing after training.
[0014] Furthermore, in step S2, the improved YOLOv5 network includes: a feature extraction network, a feature fusion layer network, and a detection network, as detailed below:
[0015] ① Feature Extraction Network: Includes CSPx module and CoordAttention module; CSPx module contains multiple CBS modules. The CBS module sequentially passes the input features through convolutional layers, normalization layers, and activation functions to obtain the output layer. The CBS module consists of a Conv layer, a BN layer, and a Silu activation function. Conv is a regular convolutional layer, and BN is the normalization process, represented as:
[0016]
[0017] In the formula, x is the input, y is the output, E is the sample mean, Var is the sample variance, ε is a value added for numerical stability, γ and β are the weights of BN, and * is the convolution calculation;
[0018] The Silu activation function is a variant of the Swish activation function; it is expressed as:
[0019]
[0020]
[0021] In the formula, The input is sigmoid, which refers to the activation function, and β′ is the weight.
[0022] The CSPx module in the feature extraction network consists of two parts: one part passes through the CBS module and the Res unit module, and the other part passes through the CBS module. These two parts are connected by a fully connected layer before the output passes through the CBS module. The Res unit module fuses the input features with the corresponding channels of the original input features through two consecutive CBS modules. The CoordAttention module embeds positional information into channel attention, enabling the lightweight network to perform attention over a larger area while avoiding significant computational overhead. It consists of two steps: coordinate information embedding and coordinate attention generation. Specifically, for coordinate information embedding, the input x of channel c is... c Two pooling kernels of sizes (h,1) and (1,w) are used to encode each channel along the horizontal coordinate w and the vertical coordinate h, respectively, which is the output of the c-th channel with height h. Represented as:
[0023]
[0024] In the formula, x c (h,j) is the two-dimensional spatial kernel of channel c with dimension (h,j); the output of the c-th channel with width w Represented as:
[0025]
[0026] In the formula, x c (i,w) is a two-dimensional spatial kernel of channel c with dimension (i,w);
[0027] The above two outputs and Feature aggregation is performed along two spatial directions respectively, and finally a pair of direction-aware feature maps are returned.
[0028] For coordinate attention generation, a pair of orientation-aware feature maps are embedded based on coordinate information, and then transformed using a shared 1×1 convolution, labeled F1, as follows:
[0029] f=δ(F1([z h ,z w ]))
[0030] In the formula, f is the intermediate feature map encoding spatial information in the horizontal and vertical directions, δ is the nonlinear activation function, and z h Z represents the output on all channels of height h. w The output represents all channels of width w; then, f is split into two feature maps of two dimensions. h and f w Then, two 1×1 convolutions are used for transformation, denoted as F. h and F w , feature map f h and f w Transform to the same number of channels as the input, using g h and g w The output representing two dimensions is expressed as:
[0031] g h =σ(F h (f h ))
[0032] g w =σ(F w (f w ))
[0033] In the formula, σ is the activation function; then, for g h and g w To expand, that is As attention weights, where 0≤i≤h, 0≤j≤w, the attention weights are fused with the input to calculate the final output y of the CoordAttention module. c (i,j) is represented as:
[0034]
[0035] In the formula, x c (i,j) indicates that the input x has dimensions (i,j) in channel c;
[0036] ② Feature fusion layer network: The feature pyramid is extracted and concatenated through multiple layers of upsampling and downsampling to output the extraction results of different layers;
[0037] ③ Detection Network: The outputs of different layers are processed through the ASFF_Decet module, which includes two parts: identity scaling and adaptive fusion. Identity scaling means that the feature map resolution of the m-th layer is written as x. m The feature maps of other layers are resized to match those of layer m; for upsampling, a 1×1 convolutional layer is used to compress the channels to be similar to layer m, and then interpolation is used to upsample to the resolution of layer m; downsampling simply uses a 3×3 convolutional layer to adjust the size and channels of the feature map; adaptive fusion means that, assuming the width a ranges from 0 to h, the height b ranges from 0 to w, and so on... This is the vector at spatial location (a, b) in the fused array at layer m resolution. It is a weighted fusion of the vectors at (a, b) from the three feature maps before fusion. The spatial importance weights of the feature maps are adaptively learned by the network and are shared across all channels, expressed as:
[0038]
[0039] In the formula, and These represent the spatial locations (a,b) at layer resolutions 1->m, 2->m, and 3->m, respectively. and The vector weights of these three feature maps, and These three parameters can be adaptively learned by the network;
[0040] After passing through the ASFF_Decet module, the confidence scores and regression coefficients at different sampling multiples are obtained, and the target detection results are output.
[0041] Furthermore, in step S3, the improvement of the improved OCSort multi-target tracking algorithm includes: adding a flexible weighting factor α, specifically: the visual embedding used to describe the trajectory is given by the exponentially moving average (EMA) of the detection embeddings frame by frame; the weighting factor α of the EMA is modified based on the detector confidence for each frame. This flexible α only allows selective embedding of the original appearance information into the tracking model under high-quality conditions, while using low detector confidence as a proxy to identify image degradation caused by occlusion or blurring, thereby rejecting embeddings that result in worse outcomes; let e t Let e be the trajectory embedded at time t. t-1For the trajectory embedded at time t-1, the standard EMA is represented as:
[0042] e t =αe t-1 +(1-α)e new
[0043] In the formula, e new It is the appearance value of the matching detection added to the tracking model; when α changes with time t, it is denoted as α t , is represented as:
[0044]
[0045] In the formula, s det α is the detector confidence level, σ′ is the detection confidence threshold used for noise filtering detection, and α is the detector confidence level. f It is a variable that can be modified; when for s det =σ′, therefore α t =1, so the new appearance embedding is completely ignored; in contrast, s det =1 means α t =α t-1 α t-1 Let e be the weighting factor from the previous time step. new This value is maximized and added to the update of the trajectory visual embedding, and it is linearly proportional to the detector confidence.
[0046] Furthermore, in step S4, a health assessment is performed on the tracking results. Specifically, it is determined whether the number of times the pigs eats or the amount of exercise is below a threshold. The thresholds for the number of times the pigs eat and the amount of exercise are an average of historical data. If the values are within a certain range of the average, they are considered normal. If they are not normal, a warning is issued, thereby determining that the pigs are in poor health.
[0047] Compared with the prior art, the present invention has the following advantages and beneficial effects:
[0048] 1. This invention adds annotations and data augmentation to the captured pig videos, thereby improving the robustness of the recognition method.
[0049] 2. This invention adds a CoordAttention module to the YOLOv5 network, which improves the ability to extract features and increases the accuracy of recognition.
[0050] 3. This invention replaces the Decet module with the ASFF_Decet module on the basis of the YOLOv5 network, which suppresses conflicting information between features at different levels during fusion, improves the fusion effect, and thus improves the target detection effect.
[0051] 4. This invention provides a weighting factor α to adjust the ratio of visual embeddings from the past and the present time on the basis of the OCSort multi-target tracking algorithm, which can improve the tracking effect of similar targets.
[0052] 5. This invention performs health status detection on the tracking result data, which is practical. Attached Figure Description
[0053] Figure 1 This is a network architecture diagram used in the method of the present invention.
[0054] Figure 2 This is a schematic diagram of the logical flow of the method of the present invention. Detailed Implementation
[0055] The present invention will be further described in detail below with reference to the embodiments and accompanying drawings, but the embodiments of the present invention are not limited thereto.
[0056] like Figure 1 and Figure 2 As shown, this embodiment discloses a method for tracking and detecting the health status of pigs. This method is based on an improved YOLOv5 network to achieve accurate identification of pigs, and then the identification results are fed into an improved OCSort multi-object tracking algorithm for tracking and health status determination. The improved YOLOv5 network is an improvement on the feature extraction and detection networks of the traditional YOLOv5 network. The improvement to the feature extraction network is achieved by adding a CoordAttention module after the CSPx module, which improves the feature extraction capability. The improvement to the detection network is achieved by replacing the original Detect module with an ASFF_Decet module, which aims to suppress conflicting information between features at different levels and improve feature fusion. The improved OCSort multi-object tracking algorithm provides a flexible weighting factor α to adjust the ratio of visual embeddings from historical and current times when embedding the original appearance information into the tracking model, improving the tracking effect for similar targets.
[0057] The specific implementation of this method includes the following steps:
[0058] S1. Obtain initial images of pigs, label the pigs and their behaviors in the initial images to obtain pig image data with labeled bounding boxes, then perform data augmentation processing on the pig image data to ensure that the image size is the same as the network input size, and divide the processed pig image data into training set, validation set, and test set; including the following steps:
[0059] S101. The initial images for obtaining pig image data are collected by manual photography or video surveillance.
[0060] S102. Use the labelImg dataset format software to annotate the pig objects in the initial images. The behavioral annotations are divided into standing, eating, and lying down. Save the annotations in the YOLO image annotation format to obtain pig image data with annotation boxes.
[0061] S103. Perform data augmentation on the pig image data to ensure that the image size is the same as the network input size. Finally, segment the processed pig image data to generate a training set for training, a validation set for validation during training, and a test set for testing after training.
[0062] S2. Input the training set and validation set into the improved YOLOv5 network for training and validation. After training and validation, an optimal network that can automatically perform target detection and behavior detection on pigs in images is obtained.
[0063] The improved YOLOv5 network includes a feature extraction network, a feature fusion layer network, and a detection network, as detailed below:
[0064] ① Feature Extraction Network: Includes CSPx module and CoordAttention module; CSPx module contains multiple CBS modules. The CBS module sequentially passes the input features through convolutional layers, normalization layers, and activation functions to obtain the output layer. The CBS module consists of a Conv layer, a BN layer, and a Silu activation function. Conv is a regular convolutional layer, and BN is the normalization process, represented as:
[0065]
[0066] In the formula, x is the input, y is the output, E is the sample mean, Var is the sample variance, ε is a value added for numerical stability, γ and β are the weights of BN, and * is the convolution calculation;
[0067] The Silu activation function is a variant of the Swish activation function; it is expressed as:
[0068]
[0069]
[0070] In the formula, The input is sigmoid, which refers to the activation function, and β′ is the weight.
[0071] The CSPx module in the feature extraction network consists of two parts: one part passes through the CBS module and the Res unit module, and the other part passes through the CBS module. These two parts are connected by a fully connected layer before the output passes through the CBS module. The Res unit module fuses the input features with the corresponding channels of the original input features through two consecutive CBS modules. The CoordAttention module embeds positional information into channel attention, enabling the lightweight network to perform attention over a larger area while avoiding significant computational overhead. It consists of two steps: coordinate information embedding and coordinate attention generation. Specifically, for coordinate information embedding, the input x of channel c is... c Two pooling kernels of sizes (h,1) and (1,w) are used to encode each channel along the horizontal coordinate w and the vertical coordinate h, respectively, which is the output of the c-th channel with height h. Represented as:
[0072]
[0073] In the formula, x c (h,j) is the two-dimensional spatial kernel of channel c with dimension (h,j); the output of the c-th channel with width w Represented as:
[0074]
[0075] In the formula, x c (i,w) is a two-dimensional spatial kernel of channel c with dimension (i,w);
[0076] The above two outputs and Feature aggregation is performed along two spatial directions respectively, and finally a pair of direction-aware feature maps are returned.
[0077] For coordinate attention generation, a pair of orientation-aware feature maps are embedded based on coordinate information, and then transformed using a shared 1×1 convolution, labeled F1, as follows:
[0078] f=δ(F1([z h ,z w ]))
[0079] In the formula, f is the intermediate feature map encoding spatial information in the horizontal and vertical directions, δ is the nonlinear activation function, and z h Z represents the output on all channels of height h. w The output represents all channels of width w; then, f is split into two feature maps of two dimensions. h and f wThen, two 1×1 convolutions are used for transformation, denoted as F. h and F w , feature map f h and f w Transform to the same number of channels as the input, using g h and g w The output representing two dimensions is expressed as:
[0080] g h =σ(F h (f h ))
[0081] g w =σF w (f w ))
[0082] In the formula, σ is the activation function; then, for g h and g w To expand, that is As attention weights, where 0≤i≤h, 0≤j≤w, the attention weights are fused with the input to calculate the final output y of the CoordAttention module. c (i,j) is represented as:
[0083]
[0084] In the formula, x c (i,j) indicates that the input x has dimensions (i,j) in channel c;
[0085] ② Feature fusion layer network: The feature pyramid is extracted and concatenated through multiple layers of upsampling and downsampling to output the extraction results of different layers;
[0086] ③ Detection Network: The outputs of different layers are processed through the ASFF_Decet module, which includes two parts: identity scaling and adaptive fusion. Identity scaling means that the feature map resolution of the m-th layer is written as x. m The feature maps of other layers are resized to match those of layer m; for upsampling, a 1×1 convolutional layer is used to compress the channels to be similar to layer m, and then interpolation is used to upsample to the resolution of layer m; downsampling simply uses a 3×3 convolutional layer to adjust the size and channels of the feature map; adaptive fusion means that, assuming the width a ranges from 0 to h, the height b ranges from 0 to w, and so on... This is the vector at spatial location (a, b) in the fused array at layer m resolution. It is a weighted fusion of the vectors at (a, b) from the three feature maps before fusion. The spatial importance weights of the feature maps are adaptively learned by the network and are shared across all channels, expressed as:
[0087]
[0088] In the formula, and These represent the spatial locations (a,b) at layer resolutions 1->m, 2->m, and 3->m, respectively. and The vector weights of these three feature maps, and These three parameters can be adaptively learned by the network;
[0089] After passing through the ASFF_Decet module, the confidence scores and regression coefficients at different sampling multiples are obtained, and the target detection results are output.
[0090] S3. Input the test set into the optimal network to obtain pig videos with detection information, and then input them into the improved OCSort multi-object tracking algorithm. The same pigs between frames will be automatically tracked by the improved OCSort multi-object tracking algorithm.
[0091] The improvements to the OCSort multi-object tracking algorithm include: adding a flexible weighting factor α, specifically: the visual embedding used to describe the trajectory is given by the exponentially moving average (EMA) of the detection embeddings frame by frame; the weighting factor α of the EMA is modified based on the detector confidence for each frame. This flexible α only allows selective embedding of the original appearance information into the tracking model under high-quality conditions, while using low detector confidence as a proxy to identify image degradation caused by occlusion or blurring, thereby rejecting embeddings that would worsen the results; let e t Let e be the trajectory embedded at time t. t-1 For the trajectory embedded at time t-1, the standard EMA is represented as:
[0092] e t =αe t-1 +(1-α)e new
[0093] In the formula, e new It is the appearance value of the matching detection added to the tracking model; when α changes with time t, it is denoted as α t , is represented as:
[0094]
[0095] In the formula, s det α is the detector confidence level, σ′ is the detection confidence threshold used for noise filtering detection, and α is the detector confidence level. f It is a modifiable variable with a default value of 0.95; when for s det =σ′, therefore α t=1, so the new appearance embedding is completely ignored; in contrast, s det =1 means α t =α t-1 α t-1 Let e be the weighting factor from the previous time step. new This value is maximized and added to the update of the trajectory visual embedding, and it is linearly proportional to the detector confidence.
[0096] After making the above improvements, the overall tracking steps are as follows:
[0097] S301. Predict the target tracking trajectory using Kalman filtering, denoted as trajectory T. Match the target detection result with the target tracking prediction result. In this process, consider the target's motion trend to obtain the trajectory T of the matched detection result per unit time. t matched Trajectory T that does not match any detection results t remain Detection results that do not match any trajectory loss function It can be represented as:
[0098]
[0099] In the formula, Z is the value predicted by Kalman filtering, and Z is the detection result. It is the historically determined trajectory, IOU is the cross-union ratio used by Kalman filtering, v is the velocity, and λ is a coefficient of the input.
[0100] S302, T t remain The trajectory of the previously matched detection results is converted into a series of detection boxes. The detection boxes are linearly allocated using the Hungarian algorithm, and the loss function is... It can be represented as:
[0101]
[0102] T t remain and matching detection values Perform a trajectory recovery and merge the recovered trajectory into T. t matched Detection boxes that still do not match the trajectory are denoted as... T t remain The trajectory of the detection box that still does not match is denoted as T. t unmatched .
[0103] S303, for Tt matched For each trajectory in the data, if T was not added previously... t matched In the middle, the trajectory between the time t′ (t′<t) when he last matched the detection result and t is virtualized.
[0104] S304, from Some new trajectories T are generated in the process. t new T t unmatched Delete the expired track and change it to T t reserved , will T t new T t reserved T t matched Update to T, and repeat steps S301 to S304.
[0105] S4. During the tracking process, a series of information on the pig's walking, eating, and lying down will be obtained as tracking results. Health judgment will be made on the tracking results. Specifically, it will be judged whether the number of times the pig eats or the amount of exercise is below the threshold. The threshold for the number of times the pig eats and the amount of exercise is an average of historical data. If it is within a range of the average, it is considered normal. If it is not normal, a warning will be issued, thereby judging that the pig's health is abnormal.
[0106] The above embodiments are preferred embodiments of the present invention, but the embodiments of the present invention are not limited to the above embodiments. Any changes, modifications, substitutions, combinations, or simplifications made without departing from the spirit and principle of the present invention shall be considered equivalent substitutions and shall be included within the protection scope of the present invention.
Claims
1. A method for tracking and detecting the health status of pigs, characterized in that, This method achieves accurate pig identification based on an improved YOLOv5 network, then feeds the identification results into an improved OCSort multi-object tracking algorithm for tracking and health status assessment. The improved YOLOv5 network modifies the feature extraction and detection networks of the traditional YOLOv5 network. The feature extraction network is improved by adding a CoordAttention module after the CSPx module, which enhances feature extraction capabilities. The detection network is improved by replacing the original Detect module with an ASFF_Decet module to suppress conflicting information between features at different levels and improve feature fusion. The improved OCSort multi-object tracking algorithm provides a flexible weighting factor α to adjust the ratio of visual embeddings from historical and current times when embedding existing appearance information into the tracking model, improving tracking performance for similar targets. The specific implementation of this method includes the following steps: S1. Obtain the initial images of pigs, label the pigs and behaviors in the initial images to obtain pig image data with labeled boxes, and then perform data augmentation processing on the pig image data to ensure that the image size is the same as the network input size. Divide the processed pig image data into training set, validation set and test set. S2. Input the training set and validation set into the improved YOLOv5 network for training and validation. After training and validation, an optimal network that can automatically perform target detection and behavior detection on pigs in images is obtained. S3. Input the test set into the optimal network to obtain pig videos with detection information, and then input them into the improved OCSort multi-object tracking algorithm. The same pigs between frames will be automatically tracked by this improved OCSort multi-object tracking algorithm. The improvements to the improved OCSort multi-object tracking algorithm include: adding a flexible weighting factor α. Specifically, the visual embedding used to describe the trajectory is given by the exponentially moving average (EMA) of the detection embeddings frame by frame; the weighting factor α of the EMA is modified based on the detector confidence for each frame. This flexible α only allows selective embedding of the original appearance information into the tracking model under high-quality conditions, while using low detector confidence as a proxy to identify image degradation caused by occlusion or blurring, thereby rejecting embeddings that worsen the results; let e t Let e be the trajectory embedded at time t. t-1 For the trajectory embedded at time t-1, the standard EMA is represented as: e t =αe t-1 +(1-a)e new In the formula, e new It is the appearance value of the matching detection added to the tracking model; when α changes with time t, it is denoted as α t , is represented as: In the formula, s det α is the detector confidence level, σ′ is the detection confidence threshold used for noise filtering detection, and α is the detector confidence level. f It is a variable that can be modified; when for s det =σ′, therefore α t =1, so the new appearance embedding is completely ignored; in contrast, s det =1 means α t =α t-1 α t-1 Let e be the weighting factor from the previous time step. new This value is maximized and added to the update of the trajectory visual embedding, and it is linearly proportional to the detector confidence. S4. During the tracking process, a series of information on the pig's walking, eating, and lying down will be obtained as tracking results. Health judgment will be made based on the tracking results, and ultimately, real-time health monitoring of the pig will be achieved.
2. The method for tracking and detecting the health status of pigs according to claim 1, characterized in that, Step S1 includes the following steps: S101. The initial images for obtaining pig image data are collected by manual photography or video surveillance. S102. Use the labelImg dataset format software to annotate the pig objects in the initial images. The behavioral annotations are divided into standing, eating, and lying down. Save the annotations in the YOLO image annotation format to obtain pig image data with annotation boxes. S103. Perform data augmentation on the pig image data to ensure that the image size is the same as the network input size. Finally, segment the processed pig image data to generate a training set for training, a validation set for validation during training, and a test set for testing after training.
3. The method for tracking and detecting the health status of pigs according to claim 2, characterized in that, In step S2, the improved YOLOv5 network includes: a feature extraction network, a feature fusion layer network, and a detection network, as detailed below: ① Feature Extraction Network: Includes CSPx module and CoordAttention module; CSPx module contains multiple CBS modules. The CBS module sequentially passes the input features through convolutional layers, normalization layers, and activation functions to obtain the output layer. The CBS module consists of a Conv layer, a BN layer, and a Silu activation function. Conv is a regular convolutional layer, and BN is the normalization process, represented as: In the formula, x is the input, y is the output, E is the sample mean, Var is the sample variance, ε is a value added for numerical stability, γ and β are the weights of BN, and * is the convolution calculation; The Silu activation function is a variant of the Swish activation function; it is represented as: In the formula, The input is sigmoid, which refers to the activation function, and β′ is the weight. The CSPx module in the feature extraction network consists of two parts: one part passes through the CBS module and the Res unit module, and the other part passes through the CBS module. These two parts are connected by a fully connected layer before the output passes through the CBS module. The Res unit module fuses the input features with the corresponding channels of the original input features through two consecutive CBS modules. The CoordAttention module embeds positional information into channel attention, enabling the lightweight network to perform attention over a larger area while avoiding significant computational overhead. It consists of two steps: coordinate information embedding and coordinate attention generation. Specifically, for coordinate information embedding, the input x of channel c is... c Two pooling kernels of sizes (h,1) and (1,w) are used to encode each channel along the horizontal coordinate w and the vertical coordinate h, respectively, which is the output of the c-th channel with height h. Represented as: In the formula, x c (h,j) is the two-dimensional spatial kernel of channel c with dimension (h,j); the output of the c-th channel with width w Represented as: In the formula, x c (i,w) is a two-dimensional spatial kernel of channel c with dimension (i,w); The above two outputs and Feature aggregation is performed along two spatial directions respectively, and finally a pair of direction-aware feature maps are returned. For coordinate attention generation, a pair of orientation-aware feature maps are embedded based on coordinate information, and then transformed using a shared 1×1 convolution, labeled F1, as follows: f=δ(F1([z h ,z w ])) In the formula, f is the intermediate feature map encoding spatial information in the horizontal and vertical directions, δ is the nonlinear activation function, and z h Z represents the output on all channels of height h. w The output represents all channels of width w; then, f is split into two feature maps of two dimensions. h and f w Then, two 1×1 convolutions are used for transformation, denoted as F. h and F w , feature map f h and f w Transform to the same number of channels as the input, using g h and g w The output representing two dimensions is expressed as: g h =σ(F h (f h )) g w =σ(F w (f w )) In the formula, σ is the activation function; then, for g h and g w To expand, that is As attention weights, where 0≤i≤h, 0≤j≤w, the attention weights are fused with the input to calculate the final output y of the CoordAttention module. c (i,j) is represented as: In the formula, x c (i,j) indicates that the input x has dimensions (i,j) in channel c; ② Feature fusion layer network: The feature pyramid is extracted and concatenated through multiple layers of upsampling and downsampling to output the extraction results of different layers; ③ Detection Network: The outputs of different layers are processed through the ASFF_Decet module, which includes two parts: identity scaling and adaptive fusion. Identity scaling means that the feature map resolution of the m-th layer is written as x. m The feature maps of other layers are resized to match those of layer m; for upsampling, a 1×1 convolutional layer is used to compress the channels to be similar to layer m, and then interpolation is used to upsample to the resolution of layer m; downsampling simply uses a 3×3 convolutional layer to adjust the size and channels of the feature map; adaptive fusion means that, assuming the width a ranges from 0 to h, the height b ranges from 0 to w, and so on... This is the vector at spatial location (a, b) in the fused array at layer m resolution. It is a weighted fusion of the vectors at (a, b) from the three feature maps before fusion. The spatial importance weights of the feature maps are adaptively learned by the network and are shared across all channels, expressed as: In the formula, and These represent the spatial locations (a,b) at layer resolutions 1->m, 2->m, and 3->m, respectively. and The vector weights of these three feature maps, and These three parameters can be adaptively learned by the network; After passing through the ASFF_Decet module, the confidence scores and regression coefficients at different sampling multiples are obtained, and the target detection results are output.
4. The method for tracking and detecting the health status of pigs according to claim 3, characterized in that, In step S4, a health assessment is performed on the tracking results. Specifically, it is determined whether the number of times the pigs eats or the amount of exercise is below a threshold. The thresholds for the number of times the pigs eats and the amount of exercise are an average of historical data. If the values are within a certain range of the average, they are considered normal. If they are not normal, a warning is issued, thereby determining that the pigs are in poor health.