Pig aisle checking method and computer equipment
By using the YOLOv5-s model and Byte-Track algorithm for pig detection and tracking, and combining dictionary data structure and multiple counting lines for counting, the problem of low counting accuracy and reliability in the existing pig aisle counting method is solved, and pig aisle counting with high accuracy and low computing energy consumption is achieved.
Patent Information
- Application Number
- CN202411828867.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2044-12-12
AI Technical Summary
The existing pig aisle counting method has the problem that counting accuracy depends on pig tracking and detection accuracy, and due to the limitation of a single calculation line, there is a possibility of multiple calculations and miscalculation, resulting in low reliability.
The YOLOv5-s model and Byte-Track algorithm are used to accurately detect and track pigs, and the pig numbers and positions are recorded through the dictionary data structure of Python, and the counting is combined with multiple counting lines to reduce the possibility of over-calculation and miscalculation.
The accuracy and reliability of pig aisle counting is achieved, the calculation amount and computing energy consumption are reduced, and practicality and reliability are improved.
Smart Images

Figure CN119785282A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of pig aisle counting, and in particular to a pig aisle inventory method and computer equipment. Background Art
[0002] Aisle counting of pigs is an important task in large-scale livestock production management and asset management in my country. Accurate aisle counting of pigs can improve the management of pig breeding, pig house construction, etc., and improve the production efficiency and economic benefits of pig farms.
[0003] Traditional pig aisle counting mainly relies on manual counting, which is time-consuming and labor-intensive, has errors, and has low reliability. At present, the existing pig aisle counting method usually uses a single counting line. If a pig passes through the counting line, the count increases. The above counting method has the following defects:
[0004] (1) Counting accuracy depends largely on the accuracy of pig tracking and detection. If the accuracy is to be improved, it is usually necessary to use a model with large computational complexity and high computing energy consumption, which cannot achieve the effect of real-time computing on the edge computing device.
[0005] (2) When counting pigs using a counting line, it is necessary to consider factors such as whether the pigs are stacked, blocked by other objects, and whether light affects the detection efficiency. In order to reduce the possibility of over-counting and under-counting, the position of the counting line needs to be adjusted during the actual counting process, resulting in poor practicality 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 pig aisle inventory method and computer equipment that is accurate in counting pigs in an aisle, strong in practicability and high in reliability.
[0007] The technical solution adopted by the present invention to solve the technical problem is: a method for counting pigs in an aisle, comprising the following steps:
[0008] S1. Obtaining images of pig passages: using a color camera lens to shoot a video sequence of pig passages;
[0009] S2. Detect the location of the pigs in the image: Use the YOLOv5-s model to select the location of the pigs in the image;
[0010] S3. Detecting pigs in tracking images: Using Byte-Track algorithm, the detected pigs are paired and numbered in different frames;
[0011] S4. Pig counting: Use Python’s dictionary data structure to record all detected pigs, draw several counting lines at intervals near the center of the screen, and count the number of pigs passing each counting line.
[0012] Furthermore, the specific steps of step S4 are as follows:
[0013] S41. Using a python dictionary data structure to record all detected pigs, wherein the "key" in the dictionary data structure is the pig number, and the "value" corresponding to the "key" in the dictionary data structure is the position of the pig detected three times in the last;
[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 its corresponding pig's current position as the "value", and add it to the dictionary data structure for tracking; if it exists, update the pig's position 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 to 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, takes the first position record and the last position record of each number, and determines whether the two position records cross the counting line after being connected. If they cross, it means that the pig of the number passes through the counting line. Then, the angle of the intersection of the two lines is used to determine the direction of the pig passing through the counting line. If it is from front to back, the count is increased by one, and if it is from back to front, the count is decreased by one;
[0017] S45. After the video playback is finished, count the count values of several count lines, sort the count values from small to large, and select the 75th percentile after sorting as the count result.
[0018] Furthermore, the specific steps of step S2 are as follows:
[0019] S21. Data preparation: read the acquired images into memory and perform preprocessing;
[0020] S22. Feature extraction: Using CSPDarknet as the backbone network for feature extraction, the image is processed in layers, feature maps of different scales are extracted, and information from low-level to high-level is gradually learned;
[0021] S23. Feature fusion: Use PANet to fuse the extracted features, integrate feature maps of different levels, and output multi-scale feature maps;
[0022] S24. Detection head: Generate multiple detection frames based on multiple anchor frames built into the model. The detection frames are gradually optimized until they approach the size and position of the target frame.
[0023] S25. Post-processing: retain the detection box with the highest confidence, traverse the remaining detection boxes, and calculate the IoU between them and the detection box with the highest confidence. If the IoU is higher than the preset threshold τ1, delete the detection box;
[0024] S26. Output a set of detection boxes, which include location information, category labels, and confidence scores.
[0025] Furthermore, the preprocessing operation in step S21 is as follows:
[0026] Resizing: Scale the image to the fixed size required by the model, keep the aspect ratio, and fill the excess part with black;
[0027] Normalization: Scale the image pixel values to the range of [0,1];
[0028] Conversion format: Convert image data into tensor format and improve the generalization ability of the model through data augmentation technology.
[0029] Furthermore, the data enhancement technology includes flipping, rotation, and brightness adjustment.
[0030] Furthermore, before post-processing in step S25, the detection boxes whose confidence is lower than a preset threshold τ2 are deleted.
[0031] Furthermore, the specific steps of step S3 are as follows:
[0032] S31. Data preparation: the detection box set, "active" trajectory and "inactive" trajectory output in step S2, where the "active" trajectory is the trajectory that matches the detection box in the previous frame, and the "inactive" trajectory is the trajectory that does not match the detection box in the previous frame;
[0033] S32. Target classification: Classify the detection box according to the confidence score. If the confidence score is higher than the set threshold τ high , it belongs to a high confidence detection frame. If the confidence score is between the set threshold τ low With τ high If it is between , it belongs to the low confidence detection box;
[0034] S33. Trajectory prediction: predict the next frame for all trajectories and generate prediction boxes;
[0035] S34. Data association: Match the high confidence detection box with the prediction box. If the match is successful, the trajectory of the detection box is updated, otherwise a new trajectory is established for the detection box.
[0036] S35. Unmatched trajectory processing: Match the low confidence detection box with the unmatched prediction box. If the match is successful, update the trajectory of the detection box, otherwise discard the detection box;
[0037] S36. Track update and screening: update the position and speed of the successfully matched track, convert the "active" track that failed to match to the "inactive" track, convert the successfully matched "inactive" track to the "active" track, and delete the "inactive" track that has 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] Furthermore, in step S34, the cost matrix is calculated using the position information and speed prediction of the high-confidence detection box and the prediction box, and the IoU is used as the cost function, and the Hungarian algorithm is used for matching based on the IoU.
[0040] Furthermore, in step S35, the correlation between the low confidence detection box and the unmatched prediction box is calculated using IoU.
[0041] A computer device comprises a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any one of the above methods when executing the computer program.
[0042] The beneficial effects of the present invention are:
[0043] (1) The present invention realizes accurate detection and tracking of pigs through the YOLOv5-s model and Byte-Track algorithm, with small calculation amount and low computing energy consumption. Combined with the application of dictionary data structure, the pig number and its position record are uniquely corresponding. Finally, multiple adjacent counting lines are used to calculate the number of pigs passing by respectively, forming a calculation area, which greatly reduces the possibility of over-counting and missing counting. There is no need to adjust the position of the counting line, and the method is highly practical and reliable.
[0044] (2) Compared with the existing YOLOX-x model, the present invention uses the YOLOv5-s model, which has smaller parameters and calculation amount, lower computing energy consumption, and faster data processing speed. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] The present invention is further described below in conjunction with the accompanying drawings and embodiments.
[0046] Figure 1 It is a flow chart of the present invention. DETAILED DESCRIPTION
[0047] The present invention will now be further described in conjunction with the accompanying drawings and preferred embodiments. These drawings are simplified schematic diagrams, which only illustrate the basic structure of the present invention in a schematic manner, and therefore only show the components related to the present invention.
[0048] Example 1
[0049] like Figure 1 As shown, a method for counting pigs in an aisle includes the following steps:
[0050] S1. Obtaining images of pig passages: using a color camera lens to shoot a video sequence of pig passages;
[0051] S2. Detect the location of the pigs in the image: Use the YOLOv5-s model to select the location of the pigs in the image;
[0052] S3. Detecting pigs in tracking images: Using Byte-Track algorithm, the detected pigs are paired and numbered in different frames;
[0053] S4. Pig counting: Use Python’s dictionary data structure to record all detected pigs, draw several counting lines at intervals near the center of the screen, and count the number of pigs passing each counting line.
[0054] The YOLOv5-s model and Byte-Track algorithm are used to accurately detect and track pigs, with small computational complexity and low computing energy consumption. Combined with the application of dictionary data structure, the pig number and its location record are uniquely corresponding. Finally, multiple adjacent counting lines are used to calculate the number of pigs passing by, forming a calculation area, which greatly reduces the possibility of over-counting and omissions. There is no need to adjust the position of the counting lines, which is highly practical and reliable.
[0055] The specific steps of step S2 are as follows:
[0056] S21. Data preparation: read the acquired images into memory and perform preprocessing;
[0057] S22. Feature extraction: Using CSPDarknet as the backbone network for feature extraction, the image is processed in layers, feature maps of different scales are extracted, and information from low-level (edge, texture) to high-level (shape, semantic) information is gradually learned;
[0058] S23. Feature fusion: Use PANet to fuse the extracted features, integrate feature maps of different levels, and output multi-scale feature maps; where feature maps of different scales are responsible for detecting objects of different sizes;
[0059] S24. Detection head: Generate multiple detection frames based on multiple anchor frames built into the model. The detection frames are gradually optimized until they approach the size and position of the target frame.
[0060] S25. Post-processing: retain the detection box with the highest confidence, traverse the remaining detection boxes, and calculate the IoU between them and the detection box with the highest confidence. If the IoU is higher than the preset threshold τ1, delete the detection box; where IoU is used to quantify the degree of overlap between detection boxes;
[0061] S26. Output a set of detection boxes, which include location information, category labels, and confidence scores.
[0062] Through the application of the YOLOv5-s model, compared with the existing YOLOX-x model, the number of parameters and calculations are smaller, the computing energy consumption is lower, and the data processing speed is faster.
[0063] The pre-processing operation in step S21 is as follows:
[0064] Resizing: Scale the image to the fixed size required by the model (640x640), keep the aspect ratio, and fill the excess part with black;
[0065] Normalization: Scale the image pixel values to the range of [0,1];
[0066] Conversion format: Convert image data into tensor format and improve the generalization ability of the model through data augmentation technology.
[0067] Data enhancement techniques include flipping, rotation, and brightness adjustment.
[0068] Before post-processing in step S25, the detection boxes with confidence levels lower than a preset threshold τ2 are deleted to reduce noise.
[0069] The specific steps of step S3 are as follows:
[0070] S31. Data preparation: the detection box set, "active" trajectory and "inactive" trajectory output in step S2, where the "active" trajectory is the trajectory that matches the detection box in the previous frame, and the "inactive" trajectory is the trajectory that does not match the detection box in the previous frame;
[0071] S32. Target classification: Classify the detection box according to the confidence score. If the confidence score is higher than the set threshold τ high , it belongs to a high confidence detection frame. If the confidence score is between the set threshold τ low With τ high If it is between , 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 box with the prediction box. If the match is successful, the trajectory of the detection box is updated, otherwise a new trajectory is established for the detection box.
[0074] S35. Unmatched trajectory processing: Match the low confidence detection box with the unmatched prediction box. If the match is successful, update the trajectory of the detection box, otherwise discard the detection box;
[0075] S36. Track update and screening: update the position and speed of the successfully matched track, convert the "active" track that failed to match to the "inactive" track, convert the successfully matched "inactive" track to the "active" track, and delete the "inactive" track that has 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, the cost matrix is calculated using the position information and speed prediction of the high-confidence detection box and the prediction box, and the IoU is used as the cost function. The Hungarian algorithm is used for matching based on the IoU.
[0078] In step S35, the correlation between the low confidence detection box and the unmatched prediction box is calculated using IoU.
[0079] The specific steps of step S4 are as follows:
[0080] S41. Using Python's dictionary data structure and list data structure to record all detected pigs, wherein the "key" in the dictionary data structure is the pig's number, and the "value" corresponding to the "key" in the list data structure is the position of the pig's most recent three detections;
[0081] 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 its corresponding pig's current position as the "value", and add it to the dictionary data structure for tracking; if it exists, update the pig's position detected in the current frame to the list data structure corresponding to the dictionary data structure;
[0082] S43. Draw several counting lines at intervals near the center of the screen to detect the time when the pigs pass through and record the number of pigs passing through; specifically, there are nine counting lines;
[0083] S44. Each counting line reads the pig number stored in the dictionary data structure and the position record stored in the list data structure, respectively, takes the first position record and the last position record of each number, and determines whether the two position records cross the counting line after being connected. If they cross, it means that the pig of the number passes through the counting line. Then, the angle of the intersection of the two lines is used to determine the direction of the pig passing through the counting line. If it is from front to back, the count is increased by one, and if it is from back to front, the count is decreased by one;
[0084] S45. After the video playback is finished, count the count values of several count lines, sort the count values from small to large, and select the 75th percentile after sorting as the count result.
[0085] Example 2
[0086] A computer device includes a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method described in Example 1 when executing the computer program.
[0087] The above embodiments are only for illustrating the technical concept and features of the present invention, and their purpose is to enable people familiar with this technology to understand the content of the present invention and implement it. They cannot be used to limit the protection scope of the present invention. Any equivalent changes or modifications made according to the spirit of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for counting pigs in an aisle, characterized in that: The steps include: S1. Obtaining pig aisle images: using a color camera lens to shoot a video sequence of pig aisles; S2. Detect the location of the pigs in the image: select the location of the pigs in the image using the YOLOv5-s model; S3. Detecting pigs in tracking images: Using Byte-Track algorithm, the detected pigs are paired and numbered in different frames; S4. Pig counting: Use Python’s dictionary data structure to record all detected pigs, draw several counting lines at intervals near the center of the screen, and count the number of pigs passing each counting line.
2. The pig aisle inventory method according to claim 1, characterized in that: The specific steps of step S4 are as follows: S41. Using a python dictionary data structure to record all detected pigs, wherein the "key" in the dictionary data structure is the pig number, and the "value" corresponding to the "key" in the dictionary data structure is the position of the pig detected three times in the last; 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 its corresponding pig's current position as the "value", and add it to the dictionary data structure for tracking; if it exists, update the pig's position 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 to detect the time when the pigs pass by and record the number of pigs passing by; S44. Each counting line reads the pig number and position record stored in the dictionary data structure, takes the first position record and the last position record of each number, and determines whether the two position records cross the counting line after being connected. If they cross, it means that the pig of the number passes through the counting line. Then, the angle of the intersection of the two lines is used to determine the direction of the pig passing through the counting line. If it is from front to back, the count is increased by one, and if it is from back to front, the count is decreased by one; S45. After the video playback is finished, count the count values of several count lines, sort the count values from small to large, and select the 75th percentile after sorting as the count result.
3. The pig aisle inventory method according to claim 1, characterized in that: The specific steps of step S2 are as follows: S21. Data preparation: read the acquired images into memory and perform preprocessing; S22. Feature extraction: Using CSPDarknet as the backbone network for feature extraction, the image is processed in layers, feature maps of different scales are extracted, and information from low-level to high-level is gradually learned; S23. Feature fusion: Use PANet to fuse the extracted features, integrate feature maps of different levels, and output multi-scale feature maps; S24. Detection head: Generate multiple detection frames based on multiple anchor frames built into the model. The detection frames are gradually optimized until they approach the size and position of the target frame. S25. Post-processing: retain the detection box with the highest confidence, traverse the remaining detection boxes, and calculate the IoU between them and the detection box with the highest confidence. If the IoU is higher than the preset threshold τ1, delete the detection box; S26. Output a set of detection boxes, which include location information, category labels, and confidence scores.
4. The pig aisle inventory method according to claim 3 is characterized in that: The pre-processing operation in step S21 is as follows: Resizing: Scale the image to the fixed size required by the model, keep the aspect ratio, and fill the excess part with black; Normalization: Scale the image pixel values to the range of [0,1]; Conversion format: Convert image data into tensor format and improve the generalization ability of the model through data augmentation technology.
5. The pig aisle inventory method according to claim 4, characterized in that: The data enhancement technology includes flipping, rotation, and brightness adjustment.
6. The pig aisle inventory method according to claim 3, characterized in that: Before post-processing in step S25, the detection boxes whose confidence is lower than the preset threshold τ2 are deleted.
7. The pig aisle inventory method according to claim 1, characterized in that: The specific steps of step S3 are as follows: S31. Data preparation: the detection box set, "active" trajectory and "inactive" trajectory output in step S2, where the "active" trajectory is the trajectory that matches the detection box in the previous frame, and the "inactive" trajectory is the trajectory that does not match the detection box in the previous frame; S32. Target classification: Classify the detection box according to the confidence score. If the confidence score is higher than the set threshold τ high , it belongs to a high confidence detection frame. If the confidence score is between the set threshold τ low With τ high If it is between , 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 box with the prediction box. If the match is successful, the trajectory of the detection box is updated, otherwise a new trajectory is established for the detection box. S35. Unmatched trajectory processing: Match the low confidence detection box with the unmatched prediction box. If the match is successful, update the trajectory of the detection box, otherwise discard the detection box; S36. Track update and screening: update the position and speed of the successfully matched track, convert the "active" track that failed to match to the "inactive" track, convert the successfully matched "inactive" track to the "active" track, and delete the "inactive" track that has not been matched for a long time; S37. Output a set of tracked detection boxes and their positions and numbers in the current frame.
8. The pig aisle inventory method according to claim 7, characterized in that: In step S34, the cost matrix is calculated using the position information and speed prediction of the high-confidence detection box and the prediction box, and the IoU is used as the cost function. The Hungarian algorithm is used for matching based on the IoU.
9. The pig aisle inventory method according to claim 7, characterized in that: In step S35, the correlation between the low confidence detection box and the unmatched prediction box is calculated using IoU.
10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 9 are implemented.
Citation Information
Patent Citations
Multi-target tracking and behavior statistics method for health-preserving pigs in group
CN115830490A
Remote pig selling supervision method, device and equipment based on image recognition
CN116453061A
Estimation method and device for daily average weight of pigs in same pigsty and storage medium
CN117789243A
Incoming and outgoing passenger counting program and information processing device
JP2024121867A
Computer implemented system, method and device for counting animals
WO2023064990A1