A method for identifying and dynamically counting the hair color of Rongchang pigs
By using a neural network model for pig identification and a multi-target tracking algorithm, combined with data augmentation technology and a lightweight YOLOv8 network, the accuracy and robustness issues of Rongchang pig coat color recognition were solved. This enabled efficient coat color statistics and recognition in complex environments, supporting the intelligent development of breeding management.
Patent Information
- Application Number
- CN202511415291.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-30
- Publication Date
- 2025-12-12
- Estimated Expiration
- 2045-09-30
AI Technical Summary
Existing identification systems suffer from low accuracy and poor robustness when identifying the coat color of Rongchang pigs. In particular, they are difficult to reliably extract key coat color features in complex breeding environments, and traditional methods are difficult to distinguish different coat color categories in mixed breeding environments.
A neural network model for pig identification, combined with Kalman filtering and the Hungarian algorithm, is used to track and identify coat color in real time by capturing video streams from a camera. The BoT-SORT multi-target tracking algorithm is used to construct an identity association system, and data augmentation technology and a lightweight YOLOv8 network model are combined to achieve high-precision coat color classification.
It achieves high-precision, stable identification and dynamic statistics of Rongchang pig coat color in complex breeding environments, improving identification accuracy and detection efficiency, and supporting the intelligent and scientific development of breeding management.
Smart Images

Figure CN120894579B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer vision, in particular to a Rongchang pig hair color recognition and dynamic statistical method. BACKGROUND
[0002] Rongchang pig is one of the famous excellent local pig breeds in China, which belongs to a meat and dual-purpose breed, and has the advantages of good reproductive performance, fast growth rate and excellent meat quality, etc. It has important economic value in animal husbandry. The whole body of the breed is white, except for the obvious black patches around the eyes, which looks like a giant panda, so it is also known as "panda pig". This unique hair color feature not only has the advantage of high identification, but also has important reference value in pig selection and breed purity identification. Therefore, in the process of breeding management and genetic selection, how to accurately identify and record the hair color characteristics of Rongchang pigs has become a key problem to be solved.
[0003] (1) Ignoring the individual differences of hair color characteristics. The existing recognition system is mainly based on target detection, focusing on the statistics of the number of pigs, and the classification ability of individual characteristics, especially the hair color type, is weak, which cannot effectively distinguish pigs of different hair color categories, especially in the environment of mixed breeding of multiple breeds.
[0004] (2) Rongchang pig feature area is limited. The body shape characteristics of Rongchang pig are relatively uniform, and only the head region has obvious color difference, so if the whole body is detected, it is easy to cause color recognition error due to the lack of features when the head is not exposed.
[0005] (3) The phenomenon of occlusion is serious. In the breeding environment, pigs frequently move, lie down and stack, which often leads to the head being blocked or unstable posture, thus reducing the accuracy of recognition methods based on head features, causing unstable color recognition results, even missing statistics, affecting the credibility of the overall data.
[0006] In summary, the existing hair color recognition method still has problems such as low accuracy and poor robustness when dealing with pig breeds with relatively simple features and key parts that are easily blocked. Therefore, there is an urgent need for a method that can stably extract key hair color features in complex scenes and achieve efficient and accurate classification to improve breeding management efficiency and meet the practical needs of breed selection and conservation.
[0007] CN119904730A discloses a cross-breed target detection method, although the number of required samples is reduced, but its practicality is low, and it ignores that in large-scale intensive livestock breeding, the features of each individual cannot be completely obtained. The typical individual features of different livestock are distributed in different parts of the body, and a large and complete recognition system needs to capture complete and unblocked images of livestock bodies, which is very difficult in real breeding environment and has low practical value. SUMMARY
[0008] The present application realizes the effective identification of different pig color varieties of Rongchang pigs through machine vision and a method for continuous recording. Specifically, a Rongchang pig color identification and dynamic statistical method is used. A camera is arranged in the stall of the pig house to collect video streams of the daily activities of Rongchang pigs and input a preset pig identification neural network model. The preset pig identification neural network model obtains pig video data, frames the pig activity video in time sequence to obtain a plurality of pig images, and based on the pig images, a pig identification data set is made. The initial pig target detection network is trained through the pig identification data set, more pig image data is continuously provided, and a pig identification neural network model is obtained.
[0009] The preset pig identification neural network model includes a pig target detection network model and a pig target color identification network model. The pig target detection network model tracks the recognized pig target in real time according to the extracted frames of the video stream and crops the pig image that needs to be identified, generating a pig color continuous tracking task. The continuously tracked pig target is combined with its motion information and appearance features to construct an identity association system and generate a pig tracking ID. During the tracking process, the color category information identified by the pig target color identification network model is higher than the confidence threshold, and the bound color category information is dynamically updated.
[0010] The pig image is input into the pig target color identification network model, and the image with a clear head is selected as an effective sample, and the sample with an incomplete exposed head is excluded for judgment. The pig head position is labeled if the image has identifiable color information; the color category information is labeled according to the position in the region to form a structured pig color category label data, which is aggregated to form a data set to train a pig color identification network model suitable for Rongchang pigs. The pig target detection network model adopts an end-to-end target detection network, including a backbone network, a feature fusion network, and a detection head. The backbone network extracts the semantic and spatial features of the image, including a multi-layer convolution module and a lightweight residual structure. The detection head is a decoupled head structure (Decoupled Head), which is designed in parallel through a classification branch and a regression branch to predict the target category and regress the position of the target frame, respectively. The decoupled head structure reduces the mutual interference between multiple tasks and improves the overall detection accuracy of the model.
[0011] The pig target detection network model adopts a YOLO (You Only Look Once) detection network model, and a Faster Block (also referred to as FasterNet Block) module is added to construct the recognition network model. The pig target detection network model also has a Kalman filtering and Hungarian algorithm cooperative mechanism, so that the method can realize high-precision and multi-frame stable tracking of each pig in the breeding pen without relying on appearance features, and provide continuous target identity support for subsequent coat updating and behavior analysis.
[0012] The Kalman filtering and Hungarian algorithm cooperative mechanism enables the system to realize high-precision and multi-frame stable tracking of each pig in the breeding pen without relying on appearance features, and provides continuous target identity support for subsequent coat updating and behavior analysis.
[0013] The pig recognition neural network model is used to realize target detection and positioning of pig individuals in the breeding pen, and the Rongchang pig pig target coat recognition network focuses on coat classification of the detected pig target area. Each has its own function, and the calculation and processing amount is reduced after classification, so that even edge (Edge) devices can effectively complete different recognition tasks.
[0014] Since multiple pigs usually exist in the pig house at the same time, more practical methods are needed to improve the multi-target recognition rate for multi-pig recognition. The BoT-SORT multi-target tracking algorithm is used, and a multi-target tracking strategy combining Kalman filtering and Hungarian algorithm is used to construct a stable identity association system by combining the motion information and appearance features of the target pig. The detected pig targets in the video are tracked in real time. The tracking technology ensures the continuity and stability of the coat information, realizes the binding of the head coat information and the tracking ID, and avoids data interruption or individual confusion in the continuous recognition process.
[0015] A dictionary (Map) data structure is used to establish a mapping relationship between the pig tracking ID and the corresponding head coat category information in the form of key-value pairs Key-Value, to realize the storage and management of coat category information data. The structured pig coat category annotation data not only includes the tracking ID, but also includes coat category information and feature position data, forming multi-element structured data. By continuously and dynamically maintaining the dictionary data, the real-time statistics and preservation of the Rongchang pig coat categories in the pig house are realized.
[0016] The coat dynamic recording mechanism designed by the application supports information updating and long-term preservation. This module automatically updates the original record when higher confidence coat information is recognized, ensuring the accuracy and timeliness of the record, and effectively supporting long-term statistical analysis.
[0017] The data set is subjected to data enhancement processing, at least two images are randomly selected from the video stream and the batch images to generate mixed images, and multiple images can be randomly cropped and spliced into one image. The enhanced parameters include hue, saturation, brightness, resolution, and are assisted by scaling and horizontal flipping.
[0018] The pig images and coat color images are subjected to mixed enhancement processing with a specified probability, two images are randomly selected from each batch, and new images are generated by mixing at a set ratio;
[0019] The hue parameters of the pig images and coat color images are randomly adjusted with a specified probability to realize HSV hue enhancement;
[0020] The saturation parameters of the pig images and coat color images are randomly adjusted with a specified probability to realize HSV saturation enhancement;
[0021] The brightness value parameters of the pig images and coat color images are randomly adjusted with a specified probability to realize HSV value (lightness) enhancement;
[0022] The pig images and coat color images are subjected to scaling processing within a specified scaling range;
[0023] The pig images and coat color images are subjected to horizontal flipping processing with a specified probability. Through the above processing, the recognizability of the images in machine vision is improved, and the diversity of the viewing angle of the images is enhanced.
[0024] The data set extracts image frames from the video stream at fixed time intervals. This effectively avoids the situation where the pig is in a static state for a short period of time, and the same picture is repeatedly identified, wasting computational resources.
[0025] A confidence threshold is set to determine whether the pig belongs to Rongchang pigs. The confidence threshold evaluation includes the precision (P) of the coat color category information, the recall (R), and the mean average precision (mAP).
[0026] The precision is calculated by , where is the evaluation index of the target category, is the number of samples correctly identified by the model as the category, is the number of samples incorrectly identified as the category.
[0027] The recall is calculated by , where represents the recall, is the number of samples that actually belong to the category but are not identified.
[0028] The average precision mean is a commonly used comprehensive evaluation index in target detection tasks. When the intersection over union (IoU) threshold is 0.5, it is denoted as mAP@0.5. IoU is used to measure the degree of overlap between the predicted box and the true box, and the larger the value, the closer the predicted box to the true box. Generally, when IoU is greater than or equal to 0.5, the predicted box can be considered as a correct detection.
[0029] On this basis, first, according to the calculation results of precision and recall, the precision-recall (PR) curve of the detection results of each class is drawn, and the area under the curve is calculated to obtain the average precision (AP) of the class, which can comprehensively reflect the comprehensive performance of the model in detection positioning and classification. Then, the average precision of all classes is taken as the arithmetic mean, that is, mAP@0.5 is obtained. The average detection precision of the model for all pig target classes is an important indicator for measuring the overall detection ability of the model. The higher the mAP, the more accurate the model is in identifying the position and class of pigs.
[0030] Under the loose positioning standard of IoU=0.5, the AP of all classes is integrated, which evaluates both the classification accuracy and the positioning accuracy, that is, improves the success rate of identifying pigs by boxing them.
[0031] Since the calculation of the image is a floating point operation, FLOPs represents the total number of floating point operations required by the model when processing an input image. The higher the value, the more complex the model and the greater the computational overhead. For the pig identification task, the FLOPs directly affects the running speed and deployment efficiency of the model, especially in the video stream monitoring of actual farms or edge devices, a lightweight model with lower FLOPs is more easily deployed in real time.
[0032] This method combines the breed characteristics of Rongchang pigs, overcomes the time-consuming and error-prone shortcomings of traditional manual identification, greatly improves the identification accuracy, detection precision and statistical efficiency, and promotes the intelligent and scientific development of breeding management. This two-stage detection mechanism separates the overall pig detection and head color identification, focuses on extracting key part features, and solves the problem of difficult identification caused by similar pig features and local occlusion. Through continuous identification and tracking data, researchers can clearly find the characteristics of high-quality breeding pigs, and through the scientific means of combining machine vision and algorithm model, the eugenics and breeding of Rongchang pigs are realized, and the breed advantage of Rongchang pigs is consolidated. BRIEF DESCRIPTION OF DRAWINGS
[0033] Figure 1 is a flowchart of the Rongchang pig color identification and dynamic statistical method described in the embodiments of the present application;
[0034] Figure 2 is a flowchart of the training method of the pig target detection network model described in the embodiments of the present application;
[0035] Figure 3 is a flowchart of the training method of the pig target color recognition network model described in the embodiments of the present application;
[0036] Figure 4 is a structure diagram of the YOLOv8 target detection network with the Faster Block module added according to the embodiments of the present application;
[0037] Figure 5 is a schematic diagram of identifying pigs from a monitoring video stream according to the embodiments of the present application;
[0038] Figure 6 is a Rongchang pig color recording schematic diagram according to the embodiments of the present application;
[0039] Figure 7 is a Rongchang pig color updating schematic diagram according to the embodiments of the present application;
[0040] Figure 8 is a nursing 1 shed drinking water wall surface situation schematic diagram according to the embodiments of the present application;
[0041] Figure 9 is a boar shed 3 situation schematic diagram according to the embodiments of the present application;
[0042] Figure 10 is a nursing 3 shed drinking water wall surface situation schematic diagram according to the embodiments of the present application. DETAILED DESCRIPTION
[0043] With reference to specific embodiments, a Rongchang pig color recognition and dynamic statistical method adopted by the present application is further described. The present application is divided into three units to realize the Rongchang pig color recognition, wherein the first unit (U100) is a color recognition and dynamic statistical method, the steps are shown in Figure 1 ; the second unit (U200) is a training method of a pig target detection network model, the specific steps are shown in Figure 2 ; and the third unit (U300) is a training method of a pig target color recognition network model, the specific steps are shown in Figure 3 . The three units and their sub-units are further described as follows:
[0044] In U100, U101: A camera is arranged in the pig house to collect video stream of the daily activities of Rongchang pigs, and input into a preset pig identification neural network model. The pig target detection network model tracks and extracts the frame in the video stream in real time, and crops the pig image to be identified, to generate a pig color continuous recording task. Preferably, the video stream can be collected in real time by a camera or other monitoring equipment arranged in the pig house, or can be imported through a storage medium. The video stream content covers the natural activity state of Rongchang pigs in the pig house, providing a data basis for intelligent breeding tasks such as quantity statistics and color identification.
[0045] U102: According to the extracted frame of the video stream, the pig image is input into the pig target color identification network model to label the pig head position. The extracted frame of the video stream is input for detection, which is used to detect the individual pig target in the image and extract its positioning area in the image. The images in which the head is clearly visible are selected as effective samples, and the samples in which the head is not completely exposed are excluded.
[0046] U103: After U200 and U300 complete the event, U100 starts U103, the system combines multi-target tracking and color dynamic recording mechanism to build a pig color continuous recording task to realize accurate archiving and dynamic updating of the color information of each pig in the breeding pen. Specifically, in each frame of image, the detection and color identification of the pig target are first completed, and then the BoT-SORT (Bag of Tricks - Simple Online and Realtime Tracking) algorithm is used to track the detected pig target. Combining the BoT-SORT multi-target tracking algorithm, the pig appears in the video continuous frame is associated with the identity, and a unique tracking label (ID) is assigned to ensure that the identity of the same pig in different frame images is consistent. Then, the color dynamic recording mechanism is built to bind the color category identified in the current frame and the corresponding tracking ID, and if higher confidence color information appears in the subsequent identification, the record is automatically updated to maintain the accuracy and timeliness of the data. This algorithm combines the motion prediction of Kalman filter and the matching strategy of pedestrian (pig) ID re-identification appearance feature, through constructing the IoU and appearance cosine distance matrix and using the Hungarian algorithm for optimal allocation, so that the system can stably track each pig in the complex breeding environment and assign a globally unique ID to it, realizing the time sequence consistency of the pig identity.
[0047] Further, the models of U200 and U300 are consistent in structure, both of which use the superior YOLOv8 network as the basic architecture, but the weight parameters are different, respectively applicable to pig target detection and coat color recognition tasks. The model can also be a YOLOv8 series model trained by other training methods, or other efficient pig target detection network models suitable for this task, which can realize accurate recognition and stable tracking of pig individual and coat information in actual breeding environment.
[0048] As shown in U200, U201 adds a Faster Block module in the YOLOv8 target detection network to build an initial pig target detection network. Figure 2
[0049] Specifically, according to Figure 4 , a YOLOv8 improved network diagram is shown, which is mainly divided into a YOLOv8 target detection model and a Faster Block module. Among them, the YOLOv8 model architecture is an end-to-end target detection network designed based on a convolutional neural network (CNN). The model is composed of a backbone, a feature fusion network (Neck), and a detection head (Head), which is used to extract multi-scale features from input images and perform target positioning and classification tasks. The backbone is used to extract the semantic and spatial features of the image, mainly composed of multiple convolution modules and lightweight residual structures. The basic convolution unit used in YOLOv8 is called Conv module (ConvModule), which is usually composed of a 3x3 convolution layer (Conv2d), batch normalization (Batch Normalization, BN), and SiLU (Sigmoid Linear Unit) activation function. The residual structure part uses an improved C2f module, which is a variant structure of the Cross Stage Partial with Two-way Fusion, which is optimized in terms of information transmission path and feature fusion efficiency compared to the C3 module in YOLOv5, to further reduce the parameter amount and improve the expression ability.
[0050] The Neck part uses a combination of FPN (Feature Pyramid Network) and PAN (Path Aggregation Network) structure for multi-scale feature fusion and transmission, so that the model can pay attention to both local details and global context information of the target, improving the detection effect of targets of different sizes.
[0051] The head part is a decoupling head structure, and the prediction of the target category and the position regression of the target frame are respectively performed through the parallel design of the classification branch and the regression branch, so that the mutual interference between multi-tasks is reduced, and the overall detection accuracy of the model is improved. Each output feature layer is independently responsible for a detection task of a specific scale to realize full-coverage detection of large, medium and small targets.
[0052] Faster Block is a lightweight and efficient residual structure module, which aims to significantly reduce the parameter quantity and computational overhead while ensuring the model expression ability, and is suitable for visual tasks with high requirements for real-time performance and resource occupation. In terms of structure, Faster Block is composed of PConv (Partial Convolution) module and standard Conv module. Among them, the PConv module only performs convolution operation on part of the channels, and the other part of the channels remains identity mapping, thereby reducing the overall computational quantity while retaining key semantic information and having good feature transmission capability. The PConv is followed by a standard 3x3 convolution BN and a SiLU activation function, which further enhances the non-linear expression. In order to introduce Faster Block into YOLOv8, the present application replaces the Bottleneck module in the original C2f (Concatenate to Feature) module with Faster Block to form a new C2f-F module (C2f with Faster Block). The C2f module is originally stacked with multiple Bottleneck, while the C2f-F replaces it with an equal number of Faster Block, thereby significantly reducing the model parameter quantity and FLOPs while maintaining the network depth and structure, and improving the feature extraction efficiency.
[0053] Since the calculation of the image is a floating point operation, FLOPs represents the total number of floating point operations required by the model when processing an input image, and the higher the value, the more complex the model and the greater the computational overhead. For pig identification tasks, the high and low of FLOPs directly affects the running speed and deployment efficiency of the model, especially in the video stream monitoring of actual farms or edge devices, a lightweight model with lower FLOPs is more easily deployed in real time.
[0054] Taking an image of 640x640 size as an example, the whole process of the image from input to the improved YOLOv8 network structure and output of the detection result is described in detail.
[0055] Layer 1 (input layer): The input image (size 640x640x3, corresponding to RGB channels) is input to a 2D convolutional layer (CBS module) with a kernel size of 3x3, a stride of 2, and padding of 1, and the output channel number is 64. This convolutional layer reduces the image size to 320x320, and the output feature map has a size of 320x320x64.
[0056] Layer 2 (downsampling + feature extraction): The output of the previous layer is input to a Down module, which contains a convolutional layer with a stride of 2 and a C2f-F module, which increases the channel number to 128 while reducing the spatial size to 160x160, and the output feature map has a size of 160x160x128.
[0057] Layer 3 (mid-level feature extraction): The output is input to another Down+C2f-F module, which increases the channel number to 256 and further reduces the spatial size to 80x80. The output feature map at this layer is 80x80x256, and this layer mainly extracts medium-scale image features.
[0058] Layer 4 (deep feature extraction): The output of the previous layer is input to the next Down+C2f-F module, which increases the channel number to 512 and reduces the spatial size to 40x40, and the output feature map has a size of 40x40x512, which is used to extract deep semantic features and enhance the perception of larger targets.
[0059] Layer 5 (high-level semantic abstraction): Continue to input to a Down+C2f-F module, with an output channel number of 1024 and a spatial size of 20x20, and the output feature map has a size of 20x20x1024, which is used to detect large targets and extract global context information.
[0060] Layer 6 (SPPF module): The 20x20x1024 feature is input to the SPPF (Spatial Pyramid Pooling-Fast) module, which fuses context information through multi-scale maximum pooling, and the output remains 20x20x1024.
[0061] Layers 7-9, Neck: FPN+PAN structure, the next stage is the feature fusion part, the model fuses three key feature maps (20x20x1024, 40x40x512, 80x80x256 from the backbone) from different depths from top to bottom and bottom to top, forming multi-scale semantic enhanced features.
[0062] The 10th layer, Head: decoupling detection head, the fused three scale feature maps are respectively sent into the decoupling detection head, and each scale is independently responsible for the detection task of a group of different size targets. Each head module includes a classification branch and a regression branch, which respectively output class probability and bounding box coordinates, and finally output feature maps of 80x80, 40x40 and 20x20 respectively corresponding to the detection of small targets, medium targets and large targets, to realize accurate identification and positioning of targets of different scales.
[0063] Final output: The improved YOLOv8 network outputs a result set containing multi-target detection information, including the position (bounding box coordinates) of each target, class probability and confidence, which is used for subsequent target screening, tracking or color classification tasks.
[0064] U202 obtains pig video data, frames the pig activity video in time sequence to obtain a plurality of pig images, and makes a pig recognition data set based on the pig images.
[0065] Preferably, the data set is subjected to data enhancement processing, and at least two mixed images are generated by randomly selecting video streams and batch images. A plurality of images can also be randomly cropped and spliced into one image. The enhanced parameters include hue, saturation, brightness, resolution, and are assisted by scaling and horizontal flipping.
[0066] The pig images and color images are subjected to mixed enhancement processing with a specified probability, two images are randomly selected from each batch, and new images are generated by mixing at a set ratio;
[0067] The hue parameters of the pig images and color images are randomly adjusted with a specified probability to realize HSV hue enhancement;
[0068] The saturation parameters of the pig images and color images are randomly adjusted with a specified probability to realize HSV saturation enhancement;
[0069] The brightness value parameters of the pig images and color images are randomly adjusted with a specified probability to realize HSV value enhancement;
[0070] The pig images and color images are subjected to scaling processing within a specified scaling range;
[0071] The pig images and color images are subjected to horizontal flipping processing with a specified probability.
[0072] Preferably, the image is mosaicked with a probability of 0.9, image blending is enhanced with a probability of 0.01, the image is adjusted in HSV hue with a probability of 0.015, the HSV saturation parameter of the image is adjusted with a probability of 0.7, the HSV value (brightness) parameter of the image is adjusted with a probability of 0.4, the image is randomly scaled in the (0.5, 4) interval, and horizontal flipping is performed with a probability of 0.1.
[0073] The pig target detection network model is obtained by training the initial pig target detection network through the pig identification data set. The pig target detection network model includes:
[0074] The evaluation of the confidence threshold includes the precision (P) of the coat category information, the recall (R), and the mean average precision.
[0075] The precision is calculated by P = TP / (TP+FP), wherein P is an evaluation index of the target category, TP is the number of samples correctly identified as the category (pig) by the model, and FP is the number of samples incorrectly identified as the category.
[0076] The recall is calculated by R = TP / (TP+FN), wherein R represents the recall, and FN is the number of samples that actually belong to the category but are not identified. It represents the proportion of pig targets that are successfully identified by the model.
[0077] The mean average precision is a commonly used comprehensive evaluation index in target detection tasks. When the intersection over union (IoU) threshold is 0.5, it is denoted as mAP@0.5. IoU is used to measure the degree of overlap between the predicted box and the true box, and the larger the value, the closer the predicted box to the true box. In the present application, when the IoU is greater than or equal to 0.5, the predicted box is considered to be a correct detection.
[0078] On this basis, first, according to the calculation results of the precision and the recall, the detection results of each category are plotted into a precision-recall (PR) curve, and the area under the curve is calculated to obtain the average precision (AP) of the category, which can comprehensively reflect the comprehensive performance of the model in detection positioning and classification. Subsequently, the average precisions of all categories are taken as the arithmetic mean, i.e., mAP@0.5. The average detection precision of the model for all categories of pig targets is an important index for measuring the overall detection ability of the model. The higher the mAP, the more accurate the model is in identifying the position and category of the pig.
[0079] Since the calculation of the image is a floating-point operation, FLOPs represents the total number of floating-point operations required by the model when processing an input image. The higher the value, the more complex the model and the greater the computational overhead. For pig identification tasks, the FLOPs directly affects the running speed and deployment efficiency of the model, especially in real-life farming video stream monitoring or edge devices, a lighter model with lower FLOPs is more easily deployed in real time.
[0080] After adding the Faster Block to the original YOLOv8 model, in order to demonstrate the advantages of adding this module, YOLOv8 target detection network model was used as the baseline (Baseline), each model was trained for 300 epochs, and the trained model was used to predict the test set. The P, R, mAP50 and GFLOPs of each model on the test set are as shown in Table 1: 、 、average precision 50 (mAP50) and GFLOPs as shown in Table 1:
[0081]
[0082] Table 1: P, R, mAP50, GFLOPs comparison table
[0083] As shown in a pig detection result diagram, using the trained improved YOLOv8 model embedded with the Faster Block module to predict pigs in the image can obtain Figure 5 detection results. Figure 5
[0084] U204 combines with BoT-SORT multi-target tracking algorithm to track the detected pig targets across frames, realizing continuous tracking of individual identity.
[0085] Specifically, the Kalman filter takes the state vector of the pig target in the previous frame as the basis:
[0086]
[0087] wherein, is the center point coordinate of the detection box, are the width and height of the detection box, respectively, represent the target's speed in the horizontal and vertical directions and the change rate of width and height, respectively. By combining the position and size and their change rates into a state vector, the Kalman filter can estimate the current position and motion trend of the target at the same time, thereby improving the tracking accuracy and stability.
[0088] Through the following state transition formula, the coordinate position, detection box width, height and speed information of the target in the current frame k are predicted:
[0089]
[0090]
[0091] where, is the predicted state vector at current time, is the state transition matrix, which describes the linear change of target state over time, the upper half block advances the position by the velocity term; the lower half block maintains the velocity. is the estimated state of the last frame. This prediction process takes into account the linear motion trend of the target, effectively compensating for the target interruption problem caused by occlusion or temporary detection failure. At the same time, the Kalman filter calculates the error covariance matrix of the prediction to measure the uncertainty of the current state prediction:
[0092]
[0093]
[0094]
[0095] where, is the error covariance matrix of the last time, the diagonal elements represent the variance of each state component, that is, the uncertainty of each component prediction; the elements of the non-diagonal line are represented by “ ”, which is the covariance between each state component, used to describe the uncertainty correlation of each component. represents the process noise covariance matrix, which is used to reflect the uncertainty in the state prediction process.
[0096] When the detection results of the current frame arrive, the Kalman filter further updates the prediction results by combining the state vector measurement value. According to the value of the prediction result and the measurement value, the Kalman gain is calculated, which is used to adjust the weight of the prediction and measurement value:
[0097]
[0098] where, is the measurement noise covariance matrix, which is usually represented by a diagonal matrix, the variance of each element is set according to the detector accuracy or experience, and the non-diagonal line is 0, indicating that the measurement errors are independent of each other. Then update the state vector and covariance matrix in turn:
[0099]
[0100]
[0101]
[0102] in, For the updated state estimate, For the observation vector, it contains
[0103] The observed values, H is the observation matrix, which maps the 8-dimensional predicted state to 4 observed values; This is an identity matrix, with diagonal elements set to 1 and off-diagonal elements set to 0, used to maintain matrix dimension consistency in matrix operations. (The last part, "covariance," appears to be unrelated and likely refers to a different concept.) In the update, for use with Kalman gain and observation matrix The system combines the predicted covariance to obtain the updated state uncertainty. Through this process, the system fuses the predicted results with the actual observations, making the estimation of the target state more accurate and stable.
[0104] To achieve cross-frame target association, the system employs the Hungarian algorithm to match the tracked targets in the previous frame with the detection results in the current frame. This algorithm constructs a cost matrix using the distance between the Kalman filter-predicted position and the current detection result, and then performs optimal matching allocation based on this matrix. Assuming the current frame contains... One prediction target and Each detection target, through construction Matching cost matrix ,in Indicates the first The first prediction target and the second The Hungarian algorithm aims to find an allocation scheme that minimizes the total cost of matching between detection boxes. This algorithm guarantees globally optimal allocation during multi-target matching, thereby maintaining temporal consistency of target identities.
[0105] Through the collaborative mechanism of Kalman filtering and Hungarian algorithm, the system can achieve high-precision, multi-frame stable tracking of each pig in the pen without relying on appearance features, and provide continuous target identity support for subsequent coat color updates and behavior analysis.
[0106] U300 Figure 3 The flowchart shown below illustrates the training method for the pig target coat color recognition network model, which includes the following steps:
[0107] U301 incorporates the Faster Block module into the YOLOv8 object detection network to construct the initial object detection network model. Following the same network construction method as U201, U301 embeds the Faster Block into YOLOv8, and the resulting model is used for detecting the hair color on the head of Rongchang pigs.
[0108] U302, based on the pig video images or photos collected by the farm, the improved pig target detection network model is used to detect the pig target, and the detected pig area is cropped to generate individual image samples.
[0109] Preferably, the input to the improved pig target detection network model is completed to detect the pig target in the image. The network model can output the boundary box position and confidence information of each pig. According to the boundary box coordinates marked in the detection result, the corresponding area in the original image is cropped to extract the individual image of a single pig. This operation can be used to build the subsequent Rongchang pig color data set. The required individual sample set. In order to ensure the quality of cropping, a minimum confidence threshold can be set to filter out detection results with low confidence or abnormal boundary boxes, and ensure the accuracy and representativeness of the obtained sample images.
[0110] U303, for the pig individual image, further mark the position of the pig head in the image, and mark the color category information in the area, to build a pig target color recognition data set suitable for Rongchang pigs.
[0111] Preferably, the images in the pig target color recognition data set are labeled and divided into training set, validation set and test set. According to the color characteristics of the pig head in the image, the head is labeled, which is divided into the following five categories: large black head, large black eye, small black eye, two black heads and foreign eye. Each category of image corresponds to a typical color distribution type of Rongchang pig. And according to the category, mark and count. Then, according to the ratio of 70%:20%:10%, the data set is divided into training set, validation set and test set, to ensure the balanced distribution of each category of data in each subset. The number of images in each category in different subsets is shown in the following table 2:
[0112]
[0113] Table 2 Rongchang pig color data set distribution
[0114] After generating the Rongchang pig data set, the Rongchang pig color data set can also be processed by data enhancement, and the data enhancement method is similar to HSV enhancement and horizontal flip.
[0115] U304, input the color data set into the target detection network model for training, and obtain a pig target color recognition network model suitable for Rongchang pigs. The processing mode of U304 is the same as that of U203, but in this subunit, the recognition effect of the pig target color recognition network model is also evaluated by the above index, and the calculation overhead and deployment adaptability are further measured by FLOPs.
[0116] To verify the performance of the constructed Rongchang pig suitable pig target detection network model, in this embodiment, the YOLOv8 network before the Faster Block is introduced as the Baseline, and the above-mentioned indexes are used to evaluate the performance of each model in the pig detection task, and the test set results are shown in Table 3.
[0117]
[0118] Table 3 target detection results
[0119] According to the position in the region, the pig coat color class information is labeled to form a structured pig coat color class labeling data, which is summarized to form a data set, and a pig target coat color recognition model network is trained. The recognition network can use the YOLO detection network model, and the Faster Block (also called FasterNet Block) module is added in the C2f (Coordinates-To-Features) module to construct a lightweight recognition network model.
[0120] Preferably, the pig target detection network model and the pig target coat color recognition network model are both based on the YOLOv8 target detection algorithm, and the improved Faster Block structure is introduced in the backbone network. The Faster Block structure introduced on the basis of YOLOv8 is a lightweight and efficient residual module that can significantly reduce the parameter quantity and calculation quantity while ensuring the deep semantic expression ability. The Faster Block enhances the feature extraction efficiency by compressing the channel number and using parallel convolution paths, so that the network model can more accurately capture the target boundary and coat color texture and other detailed features when processing pig images. By applying the improved YOLOv8 network model described above to pig detection and coat color recognition tasks, the system can accurately recognize and label individual pigs and their coat color categories, improve the robustness and real-time performance of the entire recognition system, and provide a solid technical foundation for subsequent individual tracking, coat color recording, and group structure analysis (such as large black head, large black eye, small black eye, etc.).
[0121] Preferably, the Faster Block module replaces the bottleneck (Bottleneck) module in the C2f module in the YOLOv8 target detection network.
[0122] The above network structure model uses a series of convolution layers and improved C2f-F modules to extract features from input images at different spatial scales. In summary, the model first extracts multi-scale semantic information through multiple downsampling layers, and integrates high and low layer information through a feature fusion module, and finally outputs the target position and class information through a decoupling detection head in the detection task, realizing accurate recognition of pigs.
[0123] The continuously tracked pig target is combined with its motion information and appearance features to construct an identity association system, and a pig tracking ID is generated, which is bound with the coat color category information. In the tracking process, if the coat color category information identified by the pig target coat color recognition network model exceeds the confidence threshold, the bound coat color category information is dynamically updated.
[0124] Therefore, through the cooperation of the three units, the pig targets detected in the video by the BoT-SORT algorithm are tracked in real time, and a stable identity association system is constructed by combining the motion information and appearance features of the target pigs. The tracking technology ensures the continuity and stability of the coat color information. The BoT-SORT multi-target tracking algorithm is introduced to manage the identity of individual pigs and achieve the binding of head coat color information and tracking ID, avoiding data interruption or individual confusion in continuous identification. The BoT-SORT algorithm is used to track the detected pig targets. This algorithm combines the motion prediction of Kalman filter and the matching strategy of pedestrian (pig) ID re-identification appearance features, and uses the Hungarian algorithm for optimal allocation by constructing the intersection-over-union and appearance cosine distance matrix, so that the system can stably track each pig in a complex breeding environment and assign a globally unique ID to each pig, achieving the temporal consistency of pig identity. Through this mechanism, the system realizes the continuous identification, stable maintenance, and dynamic archiving of pig coat color information, and further completes the automatic statistics and structured output of the coat color distribution of pigs in different pens based on the combination of pen number information, providing data support for the group structure management and intelligent decision-making of the breeding farm.
[0125] As Figure 6 Rongchang pig coat color recording schematic. The pig coat color recording schematic is as follows: Figure 6 Taking the pig with ID 1 as an example, in the initial stage, its back is facing the camera, and its head is not visible, so the lightweight YOLOv8 model only completes individual detection and basic tracking, and cannot extract coat color information. After a few seconds, when the head is exposed and the model identifies the coat color category as large black eye (DHY), if the recognition confidence exceeds the set threshold, the coat color information is bound with the current tracking ID, and the "ID:1 DHY" label is generated. The system internally uses a dictionary structure to save the identification results, with ID as the key and coat color category and confidence as the value, recording all successfully identified individuals.
[0126] As the detection and tracking continue, the model can gradually obtain the head information of all pigs in the pen and complete the coat color recording, achieving the statistics of the number of individuals with different coat colors. When an individual is lost due to occlusion or tracking interruption, its record in the dictionary will be removed; if the individual is detected again, a new ID will be assigned and the coat color identification and recording process will be restarted.
[0127] Figure 7is the pig color update schematic diagram of Rongchang. In the process of continuous recording, the color information of Rongchang pig can be dynamically updated. The system improves the accuracy and comprehensiveness of statistics by integrating the recognition results at different times and angles to make multiple color judgments for the same pig. When the head of the recorded pig is recognized again, the system compares the confidence of the current recognition with the existing data in the dictionary: if the current value is higher, the color information is updated; otherwise, the original record remains unchanged.
[0128] At the same time Figure 7 The effect of the update mechanism in the actual scene is shown. Taking the pig with ID 3 as an example, the color of the first detected head is small black eye (XHY), and the confidence is 0.81, which reaches the threshold value and is recorded. In the subsequent identification, the color detected is large black eye (DHY), and the confidence is 0.87, which is higher than the original record, and the system updates the color information accordingly. This mechanism effectively avoids single recognition error and missed detection problem, ensures that the most reliable recognition result is always retained in the dictionary, thereby improving the accuracy of the overall color statistics.
[0129] Figure 8-10 is the pig color statistics schematic diagram in the experimental production scene. After a long time of Rongchang pig color recognition and statistical algorithm evaluation, the accuracy rate of pig color statistics is 93.4%, which shows that the proposed recognition algorithm has good stability and recognition ability. Based on the statistical results, the system can feed back the distribution of various colors in the pig house to the breeding management platform in real time, assist the breeding personnel to make intelligent management decisions such as breed structure optimization and breeding screening, and improve the breeding efficiency and scientific level.
[0130] The dictionary (Map) data structure is adopted to establish the mapping relationship between the pig tracking ID and the corresponding head color category information, so as to realize the storage and management of the color category information data. The structured pig color category annotation data not only includes the tracking ID, but also includes the color category information and the feature position data, forming a multi-element structure data. By continuously and dynamically maintaining the dictionary data, the real-time statistics and preservation of the color category of Rongchang pig in the pig house are realized.
[0131] The data set extracts image frames from the video stream at fixed time intervals. The interval of time is between 0.01-3s according to the activity habit of the pig, and is dynamically adjusted according to the season and the number of pigs.
[0132] The color dynamic recording mechanism designed by the application supports information updating and long-term preservation. The module automatically updates the original record when a higher confidence color information is recognized, ensuring the accuracy and timeliness of the record, and effectively supporting long-term statistical analysis.
[0133] The preferred embodiments of the present application have been described. It is to be understood that the application is not limited to the specific embodiments described above, in which steps and methods not described in detail are to be understood as being carried out in an ordinary way in the art; any person skilled in the art, without departing from the scope of the technical solutions of the present application, can make many possible changes and modifications to the technical solutions of the present application disclosed above, or modify them into equivalent embodiments of equivalent changes, which do not affect the essential content of the present application. Therefore, any simple modification, equivalent change and modification made to the above embodiments according to the technical essence of the present application, without departing from the content of the technical solutions of the present application, still belongs to the scope of protection of the technical solutions of the present application.
Claims
1. A method for identifying and dynamically statistically analyzing the coat color of Rongchang pigs, characterized by installing cameras in the pens of the pigsty: Video streams of Rongchang pigs' daily activities are collected and input into a pre-set pig recognition neural network model, which includes a pig target detection network model and a coat color recognition network model. The pig target detection network model tracks the identified pig targets in real time, extracts frames from the video stream, and crops out the pig images to be identified, generating a continuous pig coat color tracking task. Once the pig image is identified, it is input into the pig target coat color recognition network model to determine whether the pig image has coat color information for recognition. Pig images with coat color information are labeled with the pig's head position; based on this position, coat color category information is labeled within the region, forming structured pig coat color category labeled data. After being summarized, a dataset is formed, and the pig target coat color recognition network model is trained. By combining the continuously tracked pig target with its movement information and appearance characteristics, an identity association system is constructed to generate a pig tracking ID, which is bound to the coat color category information. During the tracking process, if the coat color category information identified by the pig target coat color recognition network model is higher than the confidence threshold, the bound coat color category information is dynamically updated. Then, by setting a confidence threshold, it is determined whether the pigs belong to Rongchang pigs.
2. The Rongchang pig coat color identification and dynamic statistical method according to claim 1, characterized in that: Data augmentation is performed on the dataset by randomly selecting at least two images from the same batch of video streams and mixing them to generate a composite image. The augmentation parameters include hue, saturation, brightness, and resolution, and are further enhanced by scaling and horizontal flipping.
3. The Rongchang pig coat color identification and dynamic statistical method according to claim 1, characterized in that: The dataset extracts image frames from the video stream at fixed time intervals.
4. The Rongchang pig coat color identification and dynamic statistical method according to claim 1, characterized in that: The pig target detection network model adopts an end-to-end target detection network, including a backbone network, a feature fusion network, and a detection head.
5. The method for identifying and dynamically statistically analyzing the coat color of Rongchang pigs according to any one of claims 1-3, characterized in that: The evaluation of confidence thresholds includes precision, recall, and mean precision of coat color category information.
6. The Rongchang pig coat color identification and dynamic statistical method according to claim 4, characterized in that: The backbone network extracts semantic and spatial features of images, including multi-layer convolutional modules and lightweight residual structures.
7. The Rongchang pig coat color identification and dynamic statistical method according to claim 4, characterized in that: The detection head has a decoupled structure, which uses a parallel design of classification and regression branches to predict the target category and regress the location of the target box, respectively.
8. The Rongchang pig coat color identification and dynamic statistical method according to claim 5, characterized in that: Accuracy through Calculation, where Evaluation indicators for the target category, The number of samples that the model correctly identifies as belonging to this category. The recall rate is the number of samples incorrectly identified as belonging to that category; Calculate, where, Indicates recall rate, This represents the number of samples that actually belong to this category but have not been identified.
9. The method for identifying and dynamically statistically analyzing the coat color of Rongchang pigs according to claim 1, 2, or 8, characterized in that: The continuous tracking employs a multi-target tracking strategy that combines Kalman filtering with the Hungarian algorithm, and the Kalman filtering updates the previous prediction results.
10. The Rongchang pig coat color identification and dynamic statistical method according to claim 9, characterized in that: The Kelvin filter uses the pig target's state vector from the previous frame: Based on, among which, The coordinates of the center point of the detection box. These are the width and height of the detection box, respectively. These represent the target's velocity in the horizontal and vertical directions, as well as the rate of change of its width and height. Through state transition, the target's position in the current frame is predicted. The coordinate position, detection box width, height, and velocity information in the data: in, This represents the predicted state vector at the current moment. This is the state transition matrix, used to describe the linear change of the target state over time. The upper half advances the position using the velocity term; the lower half maintains the velocity. This is the estimated state from the previous frame; Kalman filtering calculates the predicted error covariance moment To measure the uncertainty of the current state prediction: in, This is the error covariance matrix from the previous time step. The diagonal elements represent the variance of each state component, i.e., the uncertainty of the prediction for each component; the off-diagonal elements are represented by "". "" indicates that the covariance between the various state components is denoted as . Represents the process noise covariance matrix; Once the detection result of the current frame arrives, the Kalman filter further updates the prediction result by combining the measurement value. Based on the prediction result and the measurement value, the Kalman gain is calculated. Used to adjust the weights of forecasts and measurements: in, To measure the noise covariance matrix, the state vector and covariance matrix are then updated sequentially. These are represented as diagonal matrices, with the variance of each element set based on detector accuracy or experience. Off-diagonal elements are set to 0, indicating independent measurement errors. The state vector and covariance matrix are then updated sequentially. in, For the updated state estimate, For the observation vector, it contains The observed values, The observation matrix maps the 8-dimensional predicted state to 4 observations. It is an identity matrix; diagonal elements are 1s and off-diagonal elements are 0s, used to maintain matrix dimension consistency in matrix operations, and in terms of covariance. In the update, for use with Kalman gain and observation matrix combination.
Citation Information
Patent Citations
Cross-variety target detection method and device, equipment and storage medium
CN119904730A
Pig state analysis system and method based on visual AI
CN113269109A
Living pig counting and multi-target tracking method based on convolutional neural network
CN116051488A