Pig passage inventory method and computer device
Through the YOLOv5-s model and Byte-Track algorithm combined with the dictionary data structure, the accuracy and reliability of pig aisle counting are improved, and the problems of poor counting accuracy and practicality in the existing technology are solved, reducing the calculation amount and energy consumption.
Patent Information
- Application Number
- CN202411828867.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2044-12-12
AI Technical Summary
The existing pig aisle counting method relies on models with high computational volume and high energy consumption, and has low counting accuracy and reliability and poor practicality.
The YOLOv5-s model and Byte-Track algorithm are used to combine the dictionary data structure to obtain pig images through the camera, detect and track pig positions, and count the number of pigs using the counting line to reduce the possibility of over-calculation and miscalculation.
The accuracy and reliability of pig counting is improved, with small calculation volume, low energy consumption, strong practicality, and no need to adjust the counting line position.
Smart Images

Figure CN119785282B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of pig aisle counting, and particularly relates to a method for counting pigs in an aisle and a computer device. Background Art
[0002] Counting pigs in an aisle is an important task in the large-scale livestock production management and asset management in China. Accurate counting of pigs in an aisle can improve the management of pig breeding, pigsty construction, etc., and increase the production efficiency and economic benefits of pig farms.
[0003] Traditional pig aisle counting mainly uses manual counting, which is time-consuming and laborious, and there are errors, resulting in low reliability. Currently, existing pig aisle counting methods usually use a single counting line. If a pig passes through this counting line, the count increases. The above counting method has the following defects:
[0004] (1) The counting accuracy depends to a large extent on the pig tracking and detection accuracy. If the accuracy is to be improved, a model with a large amount of calculation and high operation energy consumption is usually required, and the effect of real-time operation on an edge operator cannot be achieved.
[0005] (2) When using a single counting line for counting, it is necessary to consider factors such as whether pigs will stack up, be blocked by other objects, and the influence of light on the detection efficiency. In order to reduce the possibility of overcounting and undercounting, the position of the counting line needs to be adjusted continuously during the actual counting process, resulting in poor practicability and low reliability. Summary of the Invention
[0006] The technical problem to be solved by the present invention is: to overcome the deficiencies in the prior art and provide a method for counting pigs in an aisle and a computer device with accurate pig aisle counting, strong practicability and high reliability.
[0007] The technical solution adopted by the present invention to solve its technical problems is: a method for counting pigs in an aisle, including the following steps:
[0008] S1. Obtain the image of the pig aisle: Use a color camera lens to shoot the video sequence of the pig aisle;
[0009] S2. Detect the position of pigs in the image: Use the YOLOv5-s model to frame the position of pigs in the image;
[0010] S3. Track the detected pigs in the image: Use the Byte-Track algorithm to pair and number the detected pigs in different frames;
[0011] S4. Count the pigs: Use the dictionary data structure of python to record all detected pigs, and draw several counting lines at intervals near the center of the screen, and count the number of pigs passing through each counting line respectively.
[0012] Further, the specific steps of step S4 are as follows:
[0013] S41. Use the dictionary data structure in Python to record all detected pigs. Among them, the "key" in the dictionary data structure is the number of the pig, and the "value" corresponding to the "key" in the dictionary data structure is the positions where the pig was detected in the last three times.
[0014] S42. Traverse all the numbers, and check whether there is a "key" corresponding to the corresponding number in the dictionary data structure. If not, use this number as the "key" and the current position of the corresponding pig as the "value", and add it to the dictionary data structure for tracking; if it exists, update the position of the pig detected in the current frame to the "value" corresponding to the "key" in the dictionary data structure.
[0015] S43. Draw several counting lines at intervals near the center of the screen, respectively detect the time when the pigs pass by, and record the number of pigs passing by.
[0016] S44. Each counting line reads the pig number and position record stored in the dictionary data structure respectively, takes the first position record and the last position record of each number, and judges whether the connection of the two position records intersects with the counting line. If it intersects, it means that the pig with this number has passed the counting line. Then, use the angle of the intersection of the two lines to judge the direction of the pig passing the counting line. If it is from front to back, the count is incremented by one; if it is from back to front, the count is decremented by one.
[0017] S45. After the video playback ends, count the count values of several counting lines, sort the count values from small to large, and select the 75th percentile after sorting as the counting result.
[0018] Further, the specific steps of step S2 are as follows:
[0019] S21. Data preparation: Read the acquired video into the memory and perform preprocessing.
[0020] S22. Feature extraction: Use CSPDarknet as the backbone network for feature extraction, perform hierarchical processing on the video, extract feature maps of different scales, and gradually learn information from low-level to high-level.
[0021] S23. Feature fusion: Use PANet to perform feature fusion on the extracted features, integrate feature maps of different levels, and output multi-scale feature maps.
[0022] S24. Detection head: Generate multiple detection frames according to multiple anchor boxes built in the model, and gradually optimize the detection frames until they tend to the size and position of the target box.
[0023] S25. Post - processing: Keep the detection box with the highest confidence. Traverse the remaining detection boxes, calculate their IoU with the detection box with the highest confidence respectively. If the IoU is higher than the preset threshold , then delete this detection box;
[0024] S26. Output the set of detection boxes. The detection box includes position information, class label and confidence score.
[0025] Furthermore, the operations of pre - processing in step S21 are as follows:
[0026] Size adjustment: Scale the image to the fixed size required by the model, keep the aspect ratio of length and width, and fill the extra part with black;
[0027] Normalization: Scale the pixel values of the image to the range of [0, 1];
[0028] Format conversion: Convert the image data into tensor format and improve the generalization ability of the model through data augmentation techniques.
[0029] Furthermore, the data augmentation techniques include flipping, rotation, and brightness adjustment.
[0030] Furthermore, before the post - processing in step S25, delete the detection boxes with confidence lower than the preset threshold .
[0031] Furthermore, the specific steps of step S3 are as follows:
[0032] S31. Data preparation: The set of detection boxes output by step S2, "active" trajectories and "inactive" trajectories. Among them, an "active" trajectory is a trajectory that has a matching detection box in the previous frame, and an "inactive" trajectory is a trajectory that has no matching detection box in the previous frame;
[0033] S32. Object classification: Classify the detection boxes according to the confidence score. If the confidence score is higher than the set threshold , then it belongs to the detection box with high confidence. If the confidence score is between the set threshold and , then it belongs to the detection box with low confidence;
[0034] S33. Trajectory prediction: Predict the next frame for all trajectories and generate prediction boxes;
[0035] S34. Data association: Match the detection boxes with high confidence with the prediction boxes. If the match is successful, update the trajectory of this detection box, otherwise establish a new trajectory for this detection box;
[0036] S35. Unmatched Trajectory Processing: Match the low-confidence detection boxes with the unmatched prediction boxes. If the match is successful, update the trajectory of the detection box; otherwise, discard the detection box.
[0037] S36. Trajectory Update and Screening: Update the positions and speeds of the successfully matched trajectories. The "active" trajectories that fail to match are converted to "inactive" trajectories, and the "inactive" trajectories that match successfully are converted to "active" trajectories. Delete the "inactive" trajectories that have not been matched for a long time.
[0038] S37. Output a set of tracked detection boxes and their positions and numbers in the current frame.
[0039] Further, in step S34, the cost matrix is calculated using the position information of the high-confidence detection boxes and prediction boxes and speed prediction. The IoU is used as the cost function, and the Hungarian algorithm is used for matching based on the IoU.
[0040] Further, in step S35, the IoU is used to calculate the correlation between the low-confidence detection boxes and the unmatched prediction boxes.
[0041] A computer device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the method described in any one of the above.
[0042] The beneficial effects of the present invention are:
[0043] (1) The present invention realizes the precise detection and tracking of pigs through the YOLOv5-s model and the Byte-Track algorithm, with small computational complexity and low operation energy consumption. Combining the application of the dictionary data structure makes the pig numbers and their position records uniquely corresponding. Finally, multiple adjacent counting lines are used to calculate the number of pigs passing by respectively, forming a calculation area, greatly reducing the possibility of overcounting and undercounting, and there is no need to adjust the position of the counting lines, with strong practicability and high reliability.
[0044] (2) Through the application of the YOLOv5-s model, compared with the existing YOLOX-x model, the present invention has fewer parameters and less computational complexity, lower operation energy consumption, and faster data processing speed. Description of the Drawings
[0045] The present invention will be further described below with reference to the drawings and embodiments.
[0046] Figure 1 is the flowchart of the present invention. Detailed Embodiments
[0047] The present invention will now be further described in conjunction with the accompanying drawings and preferred embodiments. These drawings are all simplified schematic diagrams, only illustrating the basic structure of the present invention in a schematic manner, so they only show the components related to the present invention.
[0048] Embodiment 1:
[0049] As Figure 1 shown, a method for inventory checking of pig passageways includes the following steps:
[0050] S1. Obtain the image of the pig passageway: Use a color camera lens to capture the video sequence of the pig passageway;
[0051] S2. Detect the positions of pigs in the image: Use the YOLOv5-s model to frame the positions of pigs in the image;
[0052] S3. Track the detected pigs in the image: Use the Byte-Track algorithm to pair and number the detected pigs in different frames;
[0053] S4. Pig counting: Use the dictionary data structure in Python to record all detected pigs, draw several counting lines at intervals near the center of the screen, and respectively count the number of pigs passing through each counting line.
[0054] Through the YOLOv5-s model and the Byte-Track algorithm, precise detection and tracking of pigs are achieved, with small computational complexity and low operation energy consumption. Combining the application of the dictionary data structure makes the pig numbers and their position records uniquely corresponding. Finally, using multiple adjacent counting lines to calculate the number of pigs passing through respectively forms a calculation area, greatly reducing the possibility of overcounting and undercounting, without the need to adjust the position of the counting lines, with strong practicability and high reliability.
[0055] The specific steps of step S2 are as follows:
[0056] S21. Data preparation: Read the obtained image into the memory and perform preprocessing;
[0057] S22. Feature extraction: Use CSPDarknet as the backbone network for feature extraction, perform hierarchical processing on the image, extract feature maps of different scales, and gradually learn information from low-level (edges, textures) to high-level (shapes, semantics);
[0058] S23. Feature fusion: Use PANet to perform feature fusion on the extracted features, integrate feature maps of different levels, and output multi-scale feature maps; among them, feature maps of different scales are responsible for detecting targets of different sizes;
[0059] S24. Detection head: Generate multiple detection frames according to various anchor boxes built into the model, and gradually optimize the detection frames until they tend to the size and position of the target box;
[0060] S25. Post - processing: Keep the detection box with the highest confidence, traverse the remaining detection boxes, calculate their IoU with the detection box with the highest confidence respectively. If the IoU is higher than the preset threshold , then delete this detection box; where IoU is used to quantify the overlap degree between detection boxes;
[0061] S26. Output the set of detection boxes, where the detection box includes position information, class label, and confidence score.
[0062] By applying the YOLOv5 - s model, compared with the existing YOLOX - x model, it has fewer parameters and computational costs, lower operation energy consumption, and faster data processing speed.
[0063] The operations of the pre - processing in step S21 are as follows:
[0064] Size adjustment: Scale the image to the fixed size required by the model (640x640), keep the aspect ratio, and fill the extra part with black;
[0065] Normalization: Scale the pixel values of the image to the range of [0, 1];
[0066] Format conversion: Convert the image data into tensor format and enhance the generalization ability of the model through data augmentation techniques.
[0067] The data augmentation techniques include flipping, rotation, and brightness adjustment.
[0068] Before the post - processing in step S25, delete the detection boxes with confidence lower than the preset threshold to reduce noise.
[0069] The specific steps of step S3 are as follows:
[0070] S31. Data preparation: The set of detection boxes, "active" trajectories, and "inactive" trajectories output by step S2, where the "active" trajectory is the trajectory with a detected box matched in the previous frame, and the "inactive" trajectory is the trajectory without a detected box matched in the previous frame;
[0071] S32. Object classification: Classify the detection boxes according to the confidence score. If the confidence score is higher than the set threshold , it belongs to the high - confidence detection box. If the confidence score is between the set threshold and , it belongs to the low - confidence detection box;
[0072] S33. Trajectory prediction: Predict the next frame for all trajectories and generate prediction boxes;
[0073] S34. Data Association: Match the high-confidence detection boxes with the prediction boxes. If the match is successful, update the trajectory of the detection box; otherwise, establish a new trajectory for the detection box.
[0074] S35. Unmatched Trajectory Processing: Match the low-confidence detection boxes with the unmatched prediction boxes. If the match is successful, update the trajectory of the detection box; otherwise, discard the detection box.
[0075] S36. Trajectory Update and Screening: Update the positions and speeds of the successfully matched trajectories. The "active" trajectories with failed matches are converted to "inactive" trajectories, and the "inactive" trajectories with successful matches are converted to "active" trajectories. Delete the "inactive" trajectories that have not been matched for a long time (more than 30 frames).
[0076] S37. Output a set of tracked detection boxes and their positions and numbers in the current frame.
[0077] In step S34, use the position information of the high-confidence detection boxes and prediction boxes and speed prediction to calculate the cost matrix, use IoU as the cost function, and perform matching based on IoU using the Hungarian algorithm.
[0078] In step S35, use IoU to calculate the association degree between the low-confidence detection boxes and the unmatched prediction boxes.
[0079] The specific steps of step S4 are as follows:
[0080] S41. Use the dictionary data structure and list data structure of Python to record all detected pigs. Among them, the "key" in the dictionary data structure is the number of the pig, and the "value" corresponding to the "key" in the list data structure is the positions where the pig has been detected in the last three times.
[0081] S42. Traverse all numbers, check whether there is a corresponding "key" in the dictionary data structure. If not, use this number as the "key" and the current position of the corresponding pig as the "value", and add it to the dictionary data structure for tracking; if it exists, update the position of the pig detected in the current frame to the corresponding list data structure in the dictionary data structure.
[0082] S43. Draw several counting lines at intervals near the center of the screen, detect the time when the pigs pass by respectively, and record the number of pigs passing by; specifically, there are nine counting lines.
[0083] S44. Each counting line reads the pig ID stored in the dictionary data structure and the position record stored in the list data structure respectively. The first position record and the last position record of each ID are taken, and it is judged whether the connection of the two position records intersects with the counting line. If they intersect, it means that the pig with this ID has passed through the counting line. Then, the angle of the intersection of the two lines is used to judge the direction of the pig passing through the counting line. If it is from front to back, the count is incremented; if it is from back to front, the count is decremented.
[0084] S45. After the image playback ends, the count values of several counting lines are statistically analyzed, and the count values are sorted from small to large. The 75th percentile after sorting is selected as the counting result.
[0085] Embodiment 2:
[0086] A computer device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, the steps of the method described in Embodiment 1 are implemented.
[0087] The above embodiments are only for illustrating the technical concept and features of the present invention, and their purpose is to enable those who are familiar with this technology to understand the content of the present invention and implement it. It cannot be used to limit the protection scope of the present invention. Any equivalent changes or modifications made according to the spirit and essence of the present invention should be covered within the protection scope of the present invention.
Claims
1. A method for inventorying pigs in the aisle, characterized in that, The steps are as follows: S1. Obtain the video of the pig passageway: Use a color camera lens to capture the video sequence of the pig passageway; S2. Detect the positions of pigs in the video: Use the YOLOv5-s model to frame the positions of pigs in the video; S3. Track the detected pigs in the video: Use the Byte-Track algorithm to pair and number the detected pigs in different frames; S4. Pig counting: Use the dictionary data structure in Python to record all the detected pigs, draw several counting lines at intervals near the center of the screen, and count the number of pigs passing through each counting line respectively; Among them, the specific steps of step S4 are as follows: S41. Use the dictionary data structure in Python to record all the detected pigs. Among them, the "key" in the dictionary data structure is the number of the pig, and the "value" corresponding to the "key" in the dictionary data structure is the positions where the pig was detected in the last three times; S42. Traverse all the numbers, check whether there is a "key" corresponding to the corresponding number in the dictionary data structure. If not, use this number as the "key" and the current position of the corresponding pig as the "value", and add it to the dictionary data structure for tracking; if it exists, update the position of the pig detected in the current frame to the "value" corresponding to the "key" in the dictionary data structure; S43. Draw several counting lines at intervals near the center of the screen, detect the time when the pigs pass by respectively, and record the number of pigs passing by; S44. Each counting line reads the pig numbers and position records stored in the dictionary data structure respectively, takes the first position record and the last position record of each number, and judges whether the connection of the two position records intersects with the counting line. If it intersects, it means that the pig with this number passes through the counting line. Then use the angle of the intersection of the two lines to judge the direction of the pig passing through the counting line. If it is from front to back, the count is incremented by one; if it is from back to front, the count is decremented by one; S45. After the video playback ends, count the count values of several counting lines, sort the count values from small to large, and select the 75th percentile after sorting as the counting result.
2. The pig passage inventory method according to claim 1, characterized in that The specific steps of step S2 are as follows: S21. Data preparation: Read the obtained video into the memory and perform preprocessing; S22. Feature extraction: Use CSPDarknet as the backbone network for feature extraction, perform hierarchical processing on the video, extract feature maps of different scales, and gradually learn information from low-level to high-level; S23. Feature fusion: Use PANet to perform feature fusion on the extracted features, integrate feature maps of different levels, and output multi-scale feature maps; S24. Detection head: Generate multiple detection boxes according to various anchor boxes built in the model, and gradually optimize the detection boxes until they tend to the size and position of the target box; S25. Post-processing: Keep the detection box with the highest confidence, traverse the remaining detection boxes, calculate their IoU with the detection box with the highest confidence respectively, and if the IoU is higher than the preset threshold , then delete this detection box; S26. Output the set of detection boxes. The detection boxes include position information, class labels, and confidence scores.
3. The pig passage inventory method according to claim 2, characterized in that The operations of preprocessing in step S21 are as follows: Size adjustment: Scale the video to the fixed size required by the model, keep the aspect ratio of length and width, and fill the extra part with black; Normalization: Scale the pixel values of the video to the range of [0, 1]; Conversion format: Convert the image data into a tensor format and improve the generalization ability of the model through data augmentation techniques.
4. The method for inventorying pig passageways according to claim 3, wherein The data augmentation techniques include flipping, rotation, and brightness adjustment.
5. The pig passage inventory method according to claim 2, characterized in that, Before the post-processing in step S25, detection boxes with confidence levels lower than a preset threshold are deleted. 6. The method for inventorying pig passageways according to claim 1, wherein, The specific steps of step S3 are as follows: S31. Data preparation: The set of detection boxes, "active" trajectories, and "inactive" trajectories output by step S2, where "active" trajectories are trajectories that matched a detection box in the previous frame, and "inactive" trajectories are trajectories that did not match a detection box in the previous frame; S32. Object Classification: Classify the detection boxes according to the confidence scores. If the confidence score is higher than the set threshold , it belongs to the high-confidence detection box. If the confidence score is between the set threshold and , it belongs to the low-confidence detection box; S33. Trajectory prediction: Predict the next frame for all trajectories and generate prediction boxes; S34. Data association: Match the high-confidence detection boxes with the prediction boxes. If the match is successful, update the trajectory of the detection box; otherwise, establish a new trajectory for the detection box; S35. Handling of unmatched trajectories: Match the low-confidence detection boxes with the unmatched prediction boxes. If the match is successful, update the trajectory of the detection box; otherwise, discard the detection box; S36. Trajectory update and screening: Update the positions and speeds of the successfully matched trajectories, convert the "active" trajectories that failed to match into "inactive" trajectories, convert the "inactive" trajectories that were successfully matched into "active" trajectories, and delete the "inactive" trajectories that have not been matched for a long time; S37. Output a set of tracked detection boxes and their positions and numbers in the current frame.
7. The method for inventory taking of pig passageways according to claim 6, characterized in that, In step S34, use the position information of the high-confidence detection boxes and the prediction boxes and the speed prediction to calculate the cost matrix, use IoU as the cost function, and perform matching using the Hungarian algorithm based on IoU.
8. The method for inventorying pig passageways according to claim 6, wherein In step S35, use IoU to calculate the association degree between the low-confidence detection boxes and the unmatched prediction boxes.
9. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method described in any one of claims 1-8.
Citation Information
Patent Citations
Multi-target tracking and behavior statistics method for health-preserving pigs in group
CN115830490A