Pig pen transfer passage bidirectional counting method, system, device, medium and product

CN122888484APending Publication Date: 2026-10-09AGRI INFORMATION INST OF CHINESE ACAD OF AGRI SCI
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610752230.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-05-28
Publication Date
2026-10-09

AI Technical Summary

Technical Problem

[0003]本发明提供一种猪只转栏通道双向计数方法、系统、设备、介质及产品,用以解决现有技术在复杂场景下容易因检测框抖动或瞬态误检而产生重复计数或漏计,计数精度难以满足实际生产要求的缺陷

Benefits of technology

[0012]本发明还提供一种非暂态计算机可读存储介质,其上存储有计算机程序,该计算机程序被处理器执行时实现如上述任一种所述猪只转栏通道双向计数方法。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122888484A_ABST
    Figure CN122888484A_ABST
Patent Text Reader

Abstract

The application provides a pig crossing passageway bidirectional counting method, system, device, medium and product, and relates to the technical field of livestock management. The method comprises the following steps: acquiring a video frame sequence of a monitoring area of a pig crossing passageway, and the monitoring area is divided into a first area, a second area and a third area; determining the current area attribution of each pig based on the current position, the historical position determined by target detection and the first threshold and the second threshold for distinguishing the areas; if an effective area transfer event is triggered based on any current area attribution, and the displacement direction corresponding to the historical position is consistent with the transfer direction of the effective area transfer event, it is determined that the pig triggers a crossing event; determining the area transfer sequence of the pig based on the crossing event of the pig, and if the area transfer sequence indicates that the pig transfers from the first area to the third area or from the third area to the first area, counting is performed. The application can stably distinguish bidirectional crossing events in a complex passageway environment, and the counting precision is high.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of livestock management technology, and in particular to a method, system, equipment, medium and product for bidirectional counting in pig pen transfer channels. Background Technology

[0002] In the daily management of large-scale pig farms, accurate counting of pigs during pen transfers is a crucial step in achieving refined inventory management. Manual counting is inefficient and prone to errors. Existing automatic counting methods mainly fall into two categories: one is based on physical access control or infrared sensors, which has high hardware installation and maintenance costs and cannot distinguish between two-way passage; the other is based on visual detection lines and areas, which is prone to double counting or missed counting in complex scenarios such as dense occlusion, pigs lingering at boundaries, head swinging, and instantaneous reverse movement due to detection frame jitter or transient false detections, making it difficult to meet the counting accuracy requirements of actual production. Therefore, there is an urgent need for a high-precision automatic counting solution that can reliably distinguish between two-way pen transfer events in complex passage environments. Summary of the Invention

[0003] This invention provides a bidirectional counting method, system, device, medium, and product for pig transfer channels, which solves the defects of existing technologies in complex scenarios, such as repeated counting or missed counting due to detection frame jitter or transient false detection, and the counting accuracy is difficult to meet the requirements of actual production.

[0004] This invention provides a bidirectional counting method for pig transfer channels, comprising: The video frame sequence of the monitoring area of ​​the pig transfer passage is obtained. The monitoring area in the video frame includes a first area, a second area and a third area set in sequence. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig; Based on the current location, the historical location, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region, the current region affiliation of each pig is determined; If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical location is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event. If, based on the aforementioned pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

[0005] According to a bidirectional counting method for pig transfer channels provided by the present invention, the step of determining the current region affiliation of each pig based on the current position, the historical position, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region includes: Hysteresis bandwidths are set in the direction of the first threshold toward the first region and in the direction of the second threshold toward the third region, respectively. Determine a first difference between the first threshold and the hysteresis bandwidth, and determine a second difference between the second threshold and the hysteresis bandwidth; Based on the historical region affiliation corresponding to the historical location, the comparison result between the current location and the first threshold, the comparison result between the current location and the first difference, the comparison result between the current location and the second threshold, and the comparison result between the current location and the second difference, the current region affiliation of each pig is determined.

[0006] According to the bidirectional counting method for pig transfer channels provided by the present invention, the step of triggering a regional transfer event based on the determination of the current regional affiliation of any pig includes: Construct a finite state machine, which maintains stable states and candidate states. The stable states are used to represent the stable region affiliation of any pig before the current region affiliation, and the candidate states are used to represent regions other than the stable region affiliation. The current region affiliation of any pig is verified based on the finite state machine. If the cumulative counter corresponding to the candidate state reaches the preset confirmation threshold, the candidate state is taken as a new stable state and a region transfer event is triggered.

[0007] According to the present invention, a bidirectional counting method for pig transfer channels is provided, which counts pigs when it is determined, based on the transfer event, that the pigs have moved from a first area to a third area or from a third area to a first area. The method includes: If it is determined from the pen-crossing event that a pig has moved from the first area to the third area, the count is performed based on the first counter. If it is determined that a pig has moved from the third area to the first area based on the pen-crossing event, the count is performed based on the second counter.

[0008] According to a bidirectional counting method for pig transfer channels provided by the present invention, the step of performing target detection on the video frame sequence to determine the current and historical positions of each pig includes: The YOLO network is optimized based on optimization strategies, which include replacing the C3k2 module in the backbone and neck network of the YOLO network with the C3k2_RepViT module, replacing the nearest neighbor upsampling operation in the neck network with the dynamic upsampling module, and determining one or more of the bounding box regression loss functions based on Shape-IoU. The optimized YOLO network is used to perform target detection on the video frame sequence to determine the current and historical positions of each pig.

[0009] The bidirectional counting method for pig transfer channels provided by the present invention further includes: Determine the inter-frame displacement velocity and direction for each pig; If the inter-frame displacement speed of a pig exceeds the preset speed limit or the inter-frame displacement direction is reversed, the pig's status information is reset.

[0010] The present invention also provides a bidirectional counting system for pig pen turning channels, comprising: A counting device is used to acquire a video frame sequence of a monitoring area in a pig transfer channel. The monitoring area in the video frame includes a first area, a second area, and a third area set sequentially. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig. Based on the current position, the historical position, a first threshold for distinguishing between the first and second areas, and a second threshold for distinguishing between the second and third areas, the current area affiliation of each pig is determined. If a region transfer event is triggered based on the current area affiliation of any pig, and the displacement direction corresponding to the historical position is consistent with the transfer direction of the region transfer event, a pig is determined to have triggered a pen transfer event. If the pen transfer event determines that a pig has transferred from the first area to the third area or from the third area to the first area, counting is performed.

[0011] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the bidirectional counting method for pig transfer channels as described above.

[0012] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the bidirectional counting method for pig transfer channels as described above.

[0013] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the bidirectional counting method for pig transfer channels as described above.

[0014] The present invention provides a bidirectional counting method, system, device, medium, and product for pig transfer channels. The method includes: acquiring a video frame sequence of a monitoring area of ​​a pig transfer channel, wherein the monitoring area in the video frame includes a first area, a second area, and a third area set sequentially; performing target detection on the video frame sequence to determine the current position and historical position of each pig; determining the current area affiliation of each pig based on the current position, the historical position, a first threshold for distinguishing between the first area and the second area, and a second threshold for distinguishing between the second area and the third area; determining that a pig has triggered a transfer event if a region transfer event is triggered based on the current area affiliation of any pig and the displacement direction corresponding to the historical position is consistent with the transfer direction of the region transfer event; and performing counting if a pig is determined to have transferred from the first area to the third area or from the third area to the first area based on the transfer event. This invention divides the monitoring area in the video frame into three regions to suppress the state fluctuations of pigs near the detection line caused by detection noise or head swinging. Through effective region transfer event triggering judgment and verification of transfer direction consistency and integrity, it can filter out misjudgments caused by momentary occlusion. It can stably distinguish bidirectional pen crossing events in complex channel environments and has high counting accuracy. Attached Figure Description

[0015] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0016] Figure 1 This is one of the flowcharts of the bidirectional counting method for pig transfer channels provided by the present invention.

[0017] Figure 2 This is a schematic diagram of the finite state machine provided by the present invention.

[0018] Figure 3 This is the second flowchart of the bidirectional counting method for pig transfer channels provided by the present invention.

[0019] Figure 4 This is a schematic diagram of the bidirectional counting system for pig transfer channels provided by the present invention.

[0020] Figure 5 This is a schematic diagram comparing the forward, backflow, and overall counting accuracy of the counting strategy proposed in this invention with existing baseline counting methods.

[0021] Figure 6 This is a schematic diagram of the structure of the electronic device provided by the present invention.

[0022] Figure label: 100 - Camera equipment, 200 - Counting equipment, 300 - Display equipment. Detailed Implementation

[0023] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0024] Figure 1 This is one of the flowcharts illustrating the bidirectional counting method for pig transfer channels provided by the present invention, such as... Figure 1 As shown, the present invention provides a bidirectional counting method for pig transfer channels, which is implemented based on a counting device and includes steps S110-S150.

[0025] Step S110: Obtain a video frame sequence of the monitoring area of ​​the pig transfer channel. The monitoring areas in the video frames include a first area, a second area, and a third area set sequentially.

[0026] The video frame sequence of the monitoring area of ​​the pig transfer passage is acquired by a camera device fixedly installed above the pig transfer passage. The counting device obtains the video frame sequence from the camera through wired or wireless communication.

[0027] A first detection line and a second detection line are set in the video frame. The two detection lines divide the monitoring area in the video frame into a first region, a second region, and a third region from top to bottom. The vertical coordinate of the first detection line in the video frame is the first threshold, and the vertical coordinate of the second detection line in the video frame is the second threshold.

[0028] Step S120: Perform target detection on the video frame sequence to determine the current and historical positions of each pig.

[0029] The object detection model performs object detection on each video frame in the video frame sequence, outputting the bounding box and confidence score of each pig in each video frame. The current position of the pig refers to the coordinates of the bounding box of the pig in the current video frame, and the historical position refers to the coordinates of the bounding box of the pig in the previous video frames. By performing object detection on the video frame sequence, the position sequence of each pig can be obtained. Preferably, the object detection model can be an existing YOLO network, or a YOLO network optimized based on an optimization strategy.

[0030] During the target detection process of the video frame sequence, the target detection results corresponding to each video frame can be correlated across frames, a unique tracking number can be assigned to each pig, and a location history buffer corresponding to each unique tracking number can be maintained. The location history buffer is used to store the historical location of each pig.

[0031] Step S130: Based on the current location, the historical location, the first threshold for distinguishing the first region from the second region, and the second threshold for distinguishing the second region from the third region, determine the current region affiliation of each pig.

[0032] By using historical location as a priori condition and comparing the current location with a first or second threshold, the current region affiliation of each pig can be determined based on the comparison result. The current region affiliation is used to characterize the region to which the pig currently belongs, which can be the first region, the second region, or the third region.

[0033] Step S140: If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical position is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event.

[0034] A regional transfer event can be identified when the current regional affiliation of any pig differs from the regional affiliations of its most recent historical locations. By jointly determining the regional transfer event and the direction of transfer, the accuracy of pen-crossing event identification can be improved.

[0035] Step S150: If, based on the pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

[0036] After determining that a pig has triggered a pen-crossing event for the first time, a transfer sequence for the pig can be constructed. After the pig triggers a pen-crossing event again, the transfer sequence is updated. If the updated transfer sequence indicates that the pig has moved from the first area to the third area or from the third area to the first area, it is counted.

[0037] Let the movement from the first region to the third region be considered forward, and the movement from the third region to the first region be considered reverse. By counting the pigs that move from the first region to the third region, we can achieve forward counting, and by counting the pigs that move from the third region to the first region, we can achieve reverse counting.

[0038] Understandably, this invention divides the monitoring area in the video frame into three regions to suppress the state fluctuations of pigs near the detection line caused by detection noise or head swaying. Through effective region transfer event triggering judgment and verification of the consistency and integrity of transfer direction, it can filter out misjudgments caused by momentary occlusion and stably distinguish bidirectional pen-crossing events in complex channel environments with high counting accuracy.

[0039] As one embodiment, determining the current region affiliation of each pig based on the current location, the historical location, a first threshold for distinguishing the first region from the second region, and a second threshold for distinguishing the second region from the third region includes: Hysteresis bandwidths are set in the direction of the first threshold toward the first region and in the direction of the second threshold toward the third region, respectively. Determine a first difference between the first threshold and the hysteresis bandwidth, and determine a second difference between the second threshold and the hysteresis bandwidth; Based on the historical region affiliation corresponding to the historical location, the comparison result between the current location and the first threshold, the comparison result between the current location and the first difference, the comparison result between the current location and the second threshold, and the comparison result between the current location and the second difference, the current region affiliation of each pig is determined.

[0040] This invention proposes a hysteresis-based multi-frame temporal confirmation counting strategy (HMTC), which divides the monitoring area into three functional regions and designs hysteresis bandwidth to suppress the state jitter of pigs near the detection line caused by detection noise or head swaying.

[0041] A hysteresis bandwidth is set in the direction from the first threshold toward the first region, specifically, the hysteresis bandwidth is set in the region extending from the first detection line toward the first region. Similarly, a hysteresis bandwidth is set in the direction from the second threshold toward the third region, specifically, the hysteresis bandwidth is set in the region extending from the second detection line toward the third region. The hysteresis bandwidth value can be set to 5% to 15% of the total height of the first or third region.

[0042] The hysteresis in the hysteresis bandwidth proposed in this invention draws on the hysteresis comparison principle of the Schmitt trigger in electronic engineering. In a Schmitt trigger, the threshold used for switching the signal from low to high level is different from that used for switching from high to low level. A dead zone is formed between the two thresholds, so that the output state will not frequently flip due to small fluctuations in the input signal near the threshold, thereby achieving an anti-jitter effect.

[0043] This invention applies the above principle to pig zone state determination: when a pig moves from the second zone to the first zone, the boundary formed by the first detection line extended a certain distance into the first zone to trigger a state switch does not coincide with the boundary formed by the first detection line itself that the pig needs to cross when re-entering the second zone from the first zone. The distance between the two is the hysteresis bandwidth. If the pig only fluctuates slightly within this bandwidth, it will not trigger the back-and-forth switching of zone states, thereby suppressing state jitter caused by detection noise or head swaying.

[0044] In this embodiment of the invention, the historical regions corresponding to multiple historical positions preceding the current position are assigned as the preceding stable regions. If the preceding stable region is the second region and the current position is less than the first difference, the current region is determined to belong to the first region. If the preceding stable region is the second region and the current position is greater than or equal to the second threshold, the current region is determined to belong to the third region. If the preceding stable region is the first region and the current position is greater than or equal to the first threshold, the current region is determined to belong to the second region. If the preceding stable region is the second region and the current position is less than the second difference, the current region is determined to belong to the second region. In other cases, the current region is assigned the same as the preceding stable region.

[0045] It is understood that the embodiments of the present invention determine the current region affiliation of a pig based on the current position, the historical position, the first threshold, the second threshold, and the hysteresis bandwidth, which can suppress the state jitter of the pig near the detection line caused by detection noise or head swaying, thereby improving the counting accuracy.

[0046] like Figure 2 As shown in the illustration, as an example, the triggering of a region transfer event based on the determination of the current region affiliation of any pig includes: Construct a finite state machine, which maintains stable states and candidate states. The stable states are used to represent the stable region affiliation of any pig before the current region affiliation, and the candidate states are used to represent regions other than the stable region affiliation. The current region affiliation of any pig is verified based on the finite state machine. If the cumulative counter corresponding to the candidate state reaches the preset confirmation threshold, the candidate state is taken as a new stable state and a region transfer event is triggered.

[0047] When the historical regions corresponding to multiple historical positions preceding the current position are assigned as the preceding stable regions, the stable state is used to characterize the preceding stable regions of the pig.

[0048] The verification of the current region affiliation of any pig based on the finite state machine specifically refers to detecting whether the current region affiliation of any pig is consistent with the stable state corresponding to that pig. If an inconsistency is detected between the current region affiliation and the stable state, or if inconsistencies are detected consecutively, an accumulation counter corresponding to a candidate state is activated. When the count of the accumulation counter for a candidate state reaches a preset confirmation threshold, the candidate state is adopted as the new stable state, and a region transfer event is triggered.

[0049] It is understandable that this invention introduces a finite state machine and a candidate confirmation mechanism, requiring that the region transformation remain stable over multiple consecutive frames before a counting event can be triggered, effectively filtering out misjudgments caused by instantaneous occlusion.

[0050] As one embodiment, when it is determined based on the pen-crossing event that pigs have moved from the first area to the third area or from the third area to the first area, counting is performed, including: If it is determined from the pen-crossing event that a pig has moved from the first area to the third area, the count is performed based on the first counter. If it is determined that a pig has moved from the third area to the first area based on the pen-crossing event, the count is performed based on the second counter.

[0051] When a pig moves from the first area to the third area (forward) and from the third area to the first area (reverse), the first counter is a forward counter and the second counter is a reverse counter. When a pig is determined to move from the first area to the third area, the first counter increments by one, and when a pig is determined to move from the third area to the first area, the second counter increments by one.

[0052] It is understood that by setting a bidirectional counter, the present invention can stably distinguish and count bidirectional barrier crossing events in complex channel environments.

[0053] As one embodiment, the step of performing target detection on the video frame sequence to determine the current and historical positions of each pig includes: The YOLO network is optimized based on optimization strategies, which include replacing the C3k2 module in the backbone and neck network of the YOLO network with the C3k2_RepViT module, replacing the nearest neighbor upsampling operation in the neck network with the Dynamic upsample (DySample) module, and one or more of the bounding box regression loss functions based on Shape-IoU. The optimized YOLO network is used to perform target detection on the video frame sequence to determine the current and historical positions of each pig.

[0054] The C3k2_RepViT module replaces the Bottleneck unit in the C3k2 module of the YOLO11s backbone and neck network with the RepViT block. The RepViT block realizes spatial feature interaction through 3×3 depthwise separable convolution, realizes channel feature mixing through 1×1 pointwise convolution, and parsely merges multi-branch convolutional paths into a single equivalent convolutional layer during the inference stage, thereby reducing the number of model parameters without increasing inference latency.

[0055] The neck network of the YOLO network uses the DySample dynamic upsampling module instead of the conventional nearest neighbor interpolation upsampling. The DySample module predicts the content-aware spatial sampling offset through linear projection, scales the offset with a fixed scaling factor, and generates the final sampling coordinates by combining a predefined base grid. The upsampled feature map is obtained through the grid_sample operation, achieving more accurate multi-scale feature fusion.

[0056] The bounding box regression loss function of the YOLO network adopts Shape-IoU shape-aware loss. The loss function introduces orientation-aware weighted coefficients based on the width and height scale of the ground truth box. On the basis of standard IoU, a shape consistency penalty term and a weighted center distance penalty term are added to improve the localization accuracy of the model under the conditions of variable pig poses and occlusion.

[0057] It is understood that the embodiments of the present invention optimize the YOLO network, and then perform target detection on the video frame sequence based on the optimized YOLO network, thereby improving the counting accuracy in complex channel environments by improving the target detection accuracy. To verify the contributions of the three improved modules Shape-IoU, C3k2_RepViT, and DySample, and their combinations, the embodiments of the present invention use YOLO11s as the baseline model to conduct ablation experiments, and the experimental results are shown in Table 1.

[0058] Table 1. Effects of the improved YOLO11s pig detection model

[0059] As shown in Table 1, Model 5 is the target detection model optimized by three optimization strategies simultaneously, while Model 1 is the baseline model. The target detection model of this invention achieves an accuracy of 0.970 and a recall of 0.943, representing improvements of 1.0 percentage point and 0.4 percentage point respectively compared to the baseline model. Simultaneously, the number of model parameters decreased from 9.40M to 8.28M, and the computational cost decreased from 21.5 GFLOPs to 19.4 GFLOPs, representing reductions of approximately 11.9% and 9.8% respectively. These results demonstrate that the target detection model provided by this invention achieves both improved detection accuracy and lightweight design, making it more suitable for real-time deployment on edge devices and providing an accurate and efficient target detection foundation for subsequent HMTC counting strategies. As an embodiment, the bidirectional counting method for pig transfer channels provided by this invention further includes the following steps: Determine the inter-frame displacement velocity and direction for each pig; If the inter-frame displacement speed of a pig exceeds the preset speed limit or the inter-frame displacement direction is reversed, the pig's status information is reset.

[0060] The reset pig's status information includes the historical buffer of the location corresponding to the reset pig and the cumulative counter corresponding to the candidate status.

[0061] It is understood that the embodiments of the present invention continuously monitor the inter-frame displacement velocity and inter-frame displacement direction of each pig to realize trajectory displacement verification and anomaly self-recovery, which can ensure that the directional judgment of bidirectional pen crossing events is accurate and reliable.

[0062] Figure 3 This is the second flowchart of the bidirectional counting method for pig transfer channels provided by the present invention. The following is a combination of... Figure 3 The preferred embodiments of the present invention will be described in detail below, and the preferred embodiments of the present invention include steps S1-S7.

[0063] S1. Acquiring Channel Video Sequences: Continuously acquire video frame sequences containing individual pigs using camera equipment deployed above the pig transfer lanes. Industrial cameras with a video frame rate of 25fps can be used.

[0064] S2. Target Detection and Tracking: Perform target detection on each frame in the video frame sequence to obtain the bounding box and confidence score of each individual pig; perform cross-frame association on the target detection results, assign a unique tracking number to each pig, and maintain the location history buffer corresponding to each number.

[0065] For each video frame, an improved object detection model based on the YOLO11s network is used to perform object detection. The object detection model replaces all C3k2 modules in the backbone and neck network with C3k2_RepViT modules, with each module containing a RepViT block replacing the original Bottleneck unit. The RepViT block consists of 3×3 depthwise separable convolutions and 1×1 pointwise convolutions. During training, the multi-branch structure is maintained, while during inference, structural reparameterization merges the branches into a single convolutional layer, reducing the number of parameters by approximately 12.3% without increasing inference time. All upsampling operations in the neck network are replaced with the DySample dynamic upsampling module to linearly project the input features to generate channel-dimensional offset predictions. After scaling factor constraints, these are superimposed on the base grid to generate content-adaptive sampling coordinates. High-quality upsampled features are then obtained through grid_sample, improving multi-scale fusion accuracy. The bounding box regression loss function adopts Shape-IoU, which utilizes the aspect ratio information of the ground truth bounding boxes to construct a shape consistency penalty term and a weighted center distance penalty term, enabling the object detection model to accurately locate pigs even when their poses are varied and they are mutually occluded. The object detection results are then substituted into a cross-frame object association algorithm to assign a unique tracking number to each pig and maintain its position history buffer.

[0066] S3. Hysteresis Region Status Classification: Using the fixed horizontal position of the turntable channel floor as a reference in the video frame, a first detection line and a second detection line are set at approximately one-third and two-thirds of the image height, respectively. These detection lines are used as a baseline to divide the image into a pre-line zone, a transition zone, and a post-line zone. and The vertical coordinates of the image corresponding to the upper and lower boundaries of the transition zone are given. The difference between the two thresholds represents the height of the transition zone. The hysteresis bandwidth Δ is approximately 5% to 15% of the total height of the monitored area. The position coordinates of the individual pig in the current frame are used as the basis for this calculation. and its preceding stable region The current region affiliation is determined according to the following asymmetric rules. : ; in, This determines the region to which the previous video frame belongs. For the first area, For the second area, This is the third region.

[0067] The hysteresis design proposed in this step can effectively suppress repeated jumps in pigs near the detection line caused by head swaying or detection noise.

[0068] S4. Multi-frame timing verification: A finite state machine is used to verify the timing of region transitions and maintain a stable state. and candidate states When the current region affiliation is continuously detected and In case of inconsistency, start the candidate state accumulation counter. Only when The count reached the preset confirmation threshold. (For example, in case 3), the candidate state is promoted to a new stable state, a valid region transfer event is determined to be triggered, and the candidate state and the cumulative counter are cleared at the same time; if it is detected during the accumulation process that the current region affiliation is consistent with the stable state again, the region transfer is determined to be a false jitter, the candidate state and the cumulative counter are cleared, and the system is restored to the stable state.

[0069] S5. Trajectory Direction Verification: For each valid area transfer event, extract the historical position sequence of the individual pig from the location history buffer, perform linear fitting or head-to-tail displacement calculation on the ordinate sequence, and calculate the overall displacement trend. Only when If the direction of the sign matches the current region transfer direction, the transfer is considered a valid border crossing event; otherwise, it is discarded. This step can eliminate false border crossings caused by lingering near the boundary.

[0070] S6. Count trigger: When a complete valid transfer sequence from the first region through the second region to the third region is detected, the first counter is incremented by one; when a complete valid transfer sequence from the third region through the second region to the first region is detected, the second counter is incremented by one.

[0071] S7. Anomaly Detection and Self-Recovery: Continuously monitor the inter-frame displacement velocity of each tracked target. ;when Exceeding the physical speed limit set at 2 to 3 times the normal movement speed of pigs or continuous When the direction is abruptly reversed (for example, 5 frames or more), the tracking state of the target is reset, and the candidate state, position history buffer and candidate state accumulation counter are cleared to prevent accumulation counting errors.

[0072] In summary, regarding object detection, this invention uses YOLO11s as the base network and introduces three targeted improvements: First, the C3k2 module in the backbone and neck network is replaced with the C3k2_RepViT module. By introducing a lightweight RepViT block, the feature extraction capability is improved. At the same time, the multi-branch structure is fused into a single convolutional layer during the inference stage using the structural reparameterization mechanism, reducing the number of model parameters by 12.3% without increasing inference latency. Second, the conventional nearest neighbor upsampling operation in the neck network is replaced with the DySample dynamic upsampling module. More accurate multi-scale feature fusion is achieved through a content-aware dynamic sampling offset generation mechanism. Third, Shape-IoU is introduced as the bounding box regression loss function. By explicitly modeling the shape and scale information of the real bounding box, the localization accuracy of the model under conditions of variable pig poses and occlusion is improved.

[0073] In bidirectional counting, this invention proposes a Hysteresis-based Multi-frame Temporal Confirmation Counting Strategy (HMTC). The monitoring area is divided into three functional regions with a designed hysteresis bandwidth to suppress state fluctuations in pigs near the detection line caused by detection noise or head swaying. A finite state machine and candidate confirmation mechanism are introduced, requiring that region transitions remain stable over multiple consecutive frames before a counting event can be triggered, effectively filtering out misjudgments caused by momentary occlusion. Simultaneously, trajectory displacement verification and anomaly self-recovery mechanisms are combined to ensure accurate and reliable directional judgment of bidirectional pen-crossing events.

[0074] To verify the effectiveness of this invention, the HMTC counting strategy provided by this invention was compared with two baseline methods—one based on detection lines and the other based on regions—on a video feed from an actual farm's transfer channel. The test results are shown in Table 2. Figure 5 As shown.

[0075] Table 2. Accuracy of Pig Entry Inventory Count

[0076] As shown in Table 2, the forward pass counting accuracy of this invention reaches 99.53%, the backflow counting accuracy reaches 100.00%, and the overall counting accuracy is 99.56%. Compared with the YOLO line-crossing counting method (overall accuracy 85.61%) and the YOLO region counting method (overall accuracy 88.32%), this represents an improvement of 13.95 percentage points and 11.24 percentage points respectively, significantly outperforming existing counting methods based on detection lines and regions. Furthermore... Figure 5As can be seen, the present invention maintains a stable advantage in counting accuracy across various test video scenarios, especially in complex scenarios with pig return traffic and dense occlusion, where it still maintains near 100% accuracy, verifying the robustness and generalization ability of the HMTC counting strategy provided by the present invention. The bidirectional counting system for pig transfer channels provided by the present invention is described below, and the bidirectional counting system for pig transfer channels described below can be referred to in conjunction with the bidirectional counting method for pig transfer channels described above.

[0077] Figure 4 This is a schematic diagram of the bidirectional counting system for pig transfer channels provided by the present invention, as shown below. Figure 4 As shown, the present invention also provides a two-way counting system for pig transfer channels, including a camera device 100 and a counting device 200. Optionally, the system may further include a display device 300 for displaying the counting results. Preferably, the display device 300 is used to display video frames and counting results. The counting device 200 may be constructed using edge computing devices to achieve low-latency processing of real-time video streams, meeting the real-time counting requirements in a farm environment.

[0078] The counting device 200 is used to acquire a video frame sequence of the monitoring area of ​​the pig transfer channel. The monitoring area in the video frame includes a first area, a second area, and a third area set sequentially. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig. Based on the current position, the historical position, a first threshold for distinguishing between the first and second areas, and a second threshold for distinguishing between the second and third areas, the current area affiliation of each pig is determined. If a region transfer event is triggered based on the current area affiliation of any pig, and the displacement direction corresponding to the historical position is consistent with the transfer direction of the region transfer event, a pig is determined to have triggered a pen transfer event. If a pen transfer event determines that a pig has moved from the first area to the third area or from the third area to the first area, counting is performed.

[0079] As one embodiment, the counting device 200 is used for: Hysteresis bandwidths are set in the direction of the first threshold toward the first region and in the direction of the second threshold toward the third region, respectively. Determine a first difference between the first threshold and the hysteresis bandwidth, and determine a second difference between the second threshold and the hysteresis bandwidth; Based on the historical region affiliation corresponding to the historical location, the comparison result between the current location and the first threshold, the comparison result between the current location and the first difference, the comparison result between the current location and the second threshold, and the comparison result between the current location and the second difference, the current region affiliation of each pig is determined.

[0080] As one embodiment, the counting device 200 is used for: Construct a finite state machine, which maintains stable states and candidate states. The stable states are used to represent the stable region affiliation of any pig before the current region affiliation, and the candidate states are used to represent regions other than the stable region affiliation. The current region affiliation of any pig is verified based on the finite state machine. If the cumulative counter corresponding to the candidate state reaches the preset confirmation threshold, the candidate state is taken as a new stable state and a region transfer event is triggered.

[0081] As one embodiment, the counting device 200 is used for: If it is determined from the pen-crossing event that a pig has moved from the first area to the third area, the count is performed based on the first counter. If it is determined that a pig has moved from the third area to the first area based on the pen-crossing event, the count is performed based on the second counter.

[0082] As one embodiment, the counting device 200 is used for: The YOLO network is optimized based on optimization strategies, which include replacing the C3k2 module in the backbone and neck network of the YOLO network with the C3k2_RepViT module, replacing the nearest neighbor upsampling operation in the neck network with the DySample dynamic upsampling module, and one or more of the bounding box regression loss functions based on Shape-IoU. The optimized YOLO network is used to perform target detection on the video frame sequence to determine the current and historical positions of each pig.

[0083] As one embodiment, the counting device 200 is used for: Determine the inter-frame displacement velocity and direction for each pig; If the inter-frame displacement speed of a pig exceeds the preset speed limit or the inter-frame displacement direction is reversed, the pig's status information is reset.

[0084] In other embodiments, the counting device 200 may include the following functional modules: Image acquisition module: used to acquire continuous video frame sequences from the pig transfer channel; The target detection and tracking module is used to perform frame-by-frame target detection on the video frame sequence to obtain the bounding boxes of individual pigs; and to assign a unique tracking number to each individual through a cross-frame association algorithm, and maintain the position history buffer of each number; Hysteresis Region Classification Module: Used to divide the channel monitoring area into the pre-line zone, transition zone, and post-line zone based on the virtual detection line, using asymmetric dual thresholds. 、 And the hysteresis bandwidth Δ, combined with the current position of the individual pig and the previous stable region, determine the region affiliation of the current frame; Timing verification module: Used to run a finite state machine, and perform multi-frame timing verification of region transfer events by maintaining stable state and candidate state counters, filtering out false region transfers caused by brief occlusion or detection jitter; The trajectory verification module is used to calculate the overall displacement trend based on the location history buffer for regional transfer events after time-series confirmation, verify the consistency of the transfer direction, and filter out false overpass events caused by boundary dwelling. The counting and statistics module is used to accumulate counts based on valid overpass events in different directions and output the statistical results of the number of overpasses in both forward and reverse directions in real time. Anomaly self-recovery module: used to monitor inter-frame velocity anomalies and continuous directional reversals, triggering a state reset when tracking failure is detected, and maintaining the counting accuracy of the system during long-term operation.

[0085] It should be noted that the bidirectional counting system for pig transfer channels provided by this invention has the same technical effects as the bidirectional counting method for pig transfer channels, which will not be elaborated further.

[0086] Figure 6 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 6 As shown, the electronic device may include: a processor 610, a communication interface 620, a memory 630, and a communication bus 640, wherein the processor 610, the communication interface 620, and the memory 630 communicate with each other through the communication bus 640. The processor 610 can call logical instructions in the memory 630 to execute a bidirectional counting method for pig transfer channels, which includes: The video frame sequence of the monitoring area of ​​the pig transfer passage is obtained. The monitoring area in the video frame includes a first area, a second area and a third area set in sequence. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig; Based on the current location, the historical location, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region, the current region affiliation of each pig is determined; If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical location is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event. If, based on the aforementioned pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

[0087] Furthermore, the logical instructions in the aforementioned memory 630 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0088] On the other hand, the present invention also provides a computer program product, the computer program product comprising a computer program that can be stored on a non-transitory computer-readable storage medium, wherein when the computer program is executed by a processor, the computer is able to execute the bidirectional counting method for pig transfer channels provided by the above methods, the method comprising: The video frame sequence of the monitoring area of ​​the pig transfer passage is obtained. The monitoring area in the video frame includes a first area, a second area and a third area set in sequence. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig; Based on the current location, the historical location, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region, the current region affiliation of each pig is determined; If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical location is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event. If, based on the aforementioned pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

[0089] In another aspect, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the bidirectional counting method for pig transfer channels provided by the methods described above, the method comprising: The video frame sequence of the monitoring area of ​​the pig transfer passage is obtained. The monitoring area in the video frame includes a first area, a second area and a third area set in sequence. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig; Based on the current location, the historical location, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region, the current region affiliation of each pig is determined; If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical location is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event. If, based on the aforementioned pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

[0090] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0091] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0092] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A bidirectional counting method for pig transfer channels, characterized in that, include: The video frame sequence of the monitoring area of ​​the pig transfer passage is obtained. The monitoring area in the video frame includes a first area, a second area and a third area set in sequence. Target detection is performed on the video frame sequence to determine the current and historical positions of each pig; Based on the current location, the historical location, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region, the current region affiliation of each pig is determined; If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical location is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event. If, based on the aforementioned pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

2. The bidirectional counting method for pig transfer channels according to claim 1, characterized in that, The process of determining the current region affiliation of each pig based on its current location, historical location, a first threshold for distinguishing between the first region and the second region, and a second threshold for distinguishing between the second region and the third region includes: Hysteresis bandwidths are set in the direction of the first threshold toward the first region and in the direction of the second threshold toward the third region, respectively. Determine a first difference between the first threshold and the hysteresis bandwidth, and determine a second difference between the second threshold and the hysteresis bandwidth; Based on the historical region affiliation corresponding to the historical location, the comparison result between the current location and the first threshold, the comparison result between the current location and the first difference, the comparison result between the current location and the second threshold, and the comparison result between the current location and the second difference, the current region affiliation of each pig is determined.

3. The bidirectional counting method for pig transfer channels according to claim 1, characterized in that, The determination of the current regional affiliation of any pig to trigger a regional transfer event includes: Construct a finite state machine, which maintains stable states and candidate states. The stable states are used to represent the stable region affiliation of any pig before the current region affiliation, and the candidate states are used to represent regions other than the stable region affiliation. The current region affiliation of any pig is verified based on the finite state machine. If the cumulative counter corresponding to the candidate state reaches the preset confirmation threshold, the candidate state is taken as a new stable state and a region transfer event is triggered.

4. The bidirectional counting method for pig transfer channels according to claim 1, characterized in that, In cases where it is determined, based on the pen-moving event, that pigs have moved from the first area to the third area or from the third area to the first area, a count is performed, including: If it is determined from the pen-crossing event that a pig has moved from the first area to the third area, the count is performed based on the first counter. If it is determined that a pig has moved from the third area to the first area based on the pen-crossing event, the count is performed based on the second counter.

5. The bidirectional counting method for pig transfer channels according to any one of claims 1 to 4, characterized in that, The step of performing target detection on the video frame sequence to determine the current and historical positions of each pig includes: The YOLO network is optimized based on optimization strategies, which include replacing the C3k2 module in the backbone and neck network of the YOLO network with the C3k2_RepViT module, replacing the nearest neighbor upsampling operation in the neck network with the dynamic upsampling module, and determining one or more of the bounding box regression loss functions based on Shape-IoU. The optimized YOLO network is used to perform target detection on the video frame sequence to determine the current and historical positions of each pig.

6. The bidirectional counting method for pig transfer channels according to any one of claims 1 to 4, characterized in that, Also includes: Determine the inter-frame displacement velocity and direction for each pig; If the inter-frame displacement speed of a pig exceeds the preset speed limit or the inter-frame displacement direction is reversed, the pig's status information is reset.

7. A bidirectional counting system for pig pen transfer channels, characterized in that, include: A counting device is used to acquire a video frame sequence of a monitoring area in a pig transfer passage. The monitoring area in the video frame includes a first area, a second area, and a third area set sequentially. Target detection is performed on the video frame sequence to determine the current position and historical position of each pig. Based on the current position, the historical position, a first threshold for distinguishing between the first area and the second area, and a second threshold for distinguishing between the second area and the third area, the current area affiliation of each pig is determined. If a regional transfer event is triggered based on the current regional affiliation of any pig and the displacement direction corresponding to the historical location is consistent with the transfer direction of the regional transfer event, then the pig is determined to have triggered a pen transfer event. If, based on the aforementioned pen-crossing event, it is determined that a pig has moved from the first area to the third area or from the third area to the first area, a count is performed.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the bidirectional counting method for pig transfer channels as described in any one of claims 1 to 6.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the bidirectional counting method for pig transfer channels as described in any one of claims 1 to 6.

10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the bidirectional counting method for pig transfer channels as described in any one of claims 1 to 6.