A pigsty pig sorting counting method and system based on a fusion multi-task model

By integrating a multi-task model that combines object detection, instance segmentation, and working condition classification, the problem of insufficient accuracy and miscounting in pig counting under complex environments with multiple pig pens is solved, achieving efficient and accurate pig quantity statistics.

CN123181802APending Publication Date: 2026-11-27GUILIN UNIV OF ELECTRONIC TECH +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611228805.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-13
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

Existing pig counting technologies suffer from several problems in complex multi-pen farming scenarios, including insufficient counting accuracy, easy miscounting between adjacent pens, difficulty in distinguishing individual pigs when they are covered up and stuck together, and inaccurate determination of pen affiliation.

Method used

A multi-task model integrating object detection, instance segmentation, and working condition classification is adopted. Image or video data of multiple pig pens in the farm are acquired through image acquisition equipment. Combined with tasks such as object detection, instance segmentation, and pen affiliation determination, the individual pigs and their pen areas are jointly analyzed. The midpoint of the bottom edge of the detection box is used for preliminary determination, and a secondary verification is performed through the pixel overlap triggering mechanism of the fence mask to improve the counting accuracy.

Benefits of technology

It significantly improves the accuracy and stability of pig counting, effectively solves the problem of miscounting in complex pigpen environments, and enhances the algorithm's adaptability to various complex environments such as changes in lighting, obstruction by debris, and dense crowding of pigs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123181802A_ABST
    Figure CN123181802A_ABST
Patent Text Reader

Abstract

The present application relates to pig breeding, computer vision and artificial intelligence technical field, propose a kind of pigsty in pig stall counting method and system of fusion multi-task model, the present application fuses three kinds of visual models of target detection, instance segmentation and binary classification, realize pigsty pig stall automatic counting.After obtaining pigsty image, each pig detection frame is output by target detection, and ground and railing mask are extracted by instance segmentation;With the midpoint of the bottom edge of detection frame as the determination coordinate, it is judged whether it is located in the ground contour, yes, it is counted as effective pig in stall, otherwise, it is invalid;When detection frame and the pixel overlap of inflated railing buffer zone exist, trigger binary classification model, distinguish the pig sheltered by railing and the railing sheltered by pig, count the situation in stall, and do not count outside the stall.The present application considers overall determination efficiency and edge complex scene precision, significantly reduces the miscounting of adjacent pigsty and the side of railing, improves the efficiency, accuracy and automation degree of pig counting in the environment of pig farm multiple pigsty.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of smart farming, computer vision, and artificial intelligence. Specifically, it relates to a method and system for counting pigs in a pigpen that integrates a multi-task model. Background Technology

[0002] With the development of large-scale pig farming and smart farming technologies, the demand for automated and real-time statistics on the number of pigs in pigpens is constantly increasing. Pig counting is a fundamental aspect of inventory management, feeding scheduling, anomaly early warning, and disease prevention. Traditional methods mainly rely on manual inspection and counting. While simple to operate, these methods are prone to problems such as missed counts, double counting, and statistical lags when there are many pigpens, densely distributed pigs, frequent movement of pigs, or pigs blocking each other's view. This makes it difficult to meet the needs of modern, sophisticated pig farm management.

[0003] Existing technologies also employ ear tags, RFID tags, weighing channels, or other sensing devices for pig identification and quantity counting. These methods typically require additional identification of pigs or rely on fixed data collection channels, resulting in high equipment costs, heavy maintenance workloads, tag detachment and damage, and limited data collection locations. Furthermore, in scenarios where pigs move freely, gather, or obstruct the view within their pens, these methods struggle to directly obtain real-time numbers within each pen.

[0004] In recent years, pig counting methods based on image acquisition and computer vision have been increasingly applied. These methods acquire images or videos of pigpens using cameras, and then utilize algorithms such as target detection, instance segmentation, or density estimation to identify pig targets, thus achieving non-contact counting. Compared with traditional sensing methods, visual counting has advantages such as convenient deployment, minimal interference with pigs, and a wider coverage area. However, in actual farm environments, cameras often cover multiple adjacent pigpens simultaneously, and the images contain complex factors such as fences, enclosures, passageways, shadows, ground stains, and interference from pigs in adjacent pens. Furthermore, pigs often stick together, occlude, exhibit changes in posture, and have partial visibility, making it easy to introduce errors when counting solely based on detection boxes or contour results.

[0005] Especially when monitoring multiple pigpens simultaneously in the same image, existing methods typically focus more on detecting pig targets or estimating the overall number of pigs in the image, lacking a joint assessment of key areas within the pigpens, pen boundaries, and the spatial relationship between pigs and their pens. If pigs in adjacent pens visually overlap in the image, the classification of a target pen based on the center point of the detection box, the position of its circumscribed rectangle, or a manually defined area can be easily affected by pig posture, body length, occlusion, and perspective distortion, leading to miscounting of pigpens across different pens. For example, a pig may be mostly within a pen, but if the center point of the detection box is near the boundary, it may be incorrectly counted as belonging to an adjacent pen; resulting in inaccurate phase counts.

[0006] Therefore, existing pig counting technologies have a certain application foundation in single pigpens or simple scenarios. However, when facing the complex environment of multiple pigpens within a farm, problems still exist, such as insufficient pig instance separation, inadequate identification of key areas in pigpens, inaccurate pen affiliation determination, and miscounting of adjacent pigpens. To improve the accuracy and stability of pig counting in multi-pigpen scenarios, it is necessary to propose a pig pen counting system that integrates multi-task models such as object detection, instance segmentation, key area identification, and pen affiliation determination. Summary of the Invention

[0007] The purpose of this invention is to address the problems of insufficient counting accuracy, easy miscounting between adjacent pig pens, difficulty in distinguishing individual pigs when they are covered and stuck together, and inaccurate determination of pig pen ownership in existing pig counting technologies in complex multi-pigpen farming scenarios. The invention proposes a pig counting method and system that integrates a multi-task model.

[0008] This invention acquires images or video data of multiple pig pens in a farm using image acquisition equipment, and combines tasks such as target detection, instance segmentation, key area identification, and pen affiliation determination to perform joint analysis on individual pigs in the image and the pig pen area they belong to, thereby achieving automatic identification and statistics of the number of pigs in each pig pen.

[0009] This invention aims to solve the problems of low efficiency and statistical lag when relying solely on manual counting, as well as the difficulty of accurately distinguishing pigs in adjacent pigpens using existing visual counting methods, and to improve the accuracy, stability, and automation of pig counting in complex pigpen environments.

[0010] To achieve the above objectives, the present invention proposes the following technical solution: A method for counting pigs in pigpens by merging a multi-task model, characterized by comprising: Step 1: Obtain image data of the pig pens to be counted, and input the image data into the target detection model and the instance segmentation model respectively to obtain the bounding box of the whole image pig target detection and the segmentation result of the pig pen region; Step 2 involves post-processing the pigpen area segmentation results, including performing morphological dilation operations on the binary mask of the fence area and filtering connected components on the ground farming mask. Step 3: Based on the pig target detection bounding box, the midpoint of the bottom edge of the pig detection box is uniformly used as the unique determination coordinate of the pig area. The point contour position detection algorithm is called to determine whether the determination coordinate is located inside or on the contour of the target pig pen breeding boundary. If so, the pig is determined to be a pig raised inside the pen; otherwise, it is determined to be an invalid pig outside the pen. Step 4: For targets identified as pigs raised within the enclosure, determine whether a secondary classification verification is needed based on the target bounding box and fence mask pixel overlap trigger mechanism. Step 5: Use the working condition classification model to perform binary classification judgment on the pig targets that trigger the edge secondary classification verification; Step 6: Summarize the number of pigs identified as having valid inventory in each pigpen and output the total number of pigs in inventory in the corresponding pigpen in real time.

[0011] Furthermore, in step one, the target detection model employs a deep learning target detection algorithm, and the instance segmentation model uses a semantic segmentation algorithm to segment the pigpen area into three categories: ground, fence, and concrete wall. Furthermore, in step two, the morphological dilation operation includes: using the actual railing pixel area as a reference, uniformly expanding and defining the railing adjacent buffer zone to both sides, and using the railing adjacent buffer zone as the railing critical determination area for subsequent triggering edge verification logic; The connected component filtering of the ground-based aquaculture mask includes: filtering connected components with minute noise and invalid background connected components at the image edges, and selecting the effective surface connected component contour with the largest area as the final standard pigpen aquaculture boundary contour.

[0012] Furthermore, in step three, the calculation method for the determination coordinates is: horizontal midpoint y-coordinate of the bottom edge ; in The coordinates of the upper left corner of the pig detection enclosure are shown. The coordinates are the bottom right. Before calling the point contour position detection algorithm, the judgment coordinates are first subjected to boundary truncation to prevent them from going out of bounds, thus limiting the coordinate range to within the image pixel range.

[0013] Furthermore, in step four, the process of determining whether a secondary classification verification is needed based on the overlap triggering mechanism between the target bounding box and the railing mask pixels includes: Extract the local fence mask area corresponding to the overall detection bounding box of the pig, traverse the pixels in this local area, and if any pixel within the detection bounding box overlaps with the pixels of the expanded fence buffer, it is determined that the pig target overlaps with the fence area and triggers edge secondary classification verification; if no pixels overlap, the pig is directly determined to be a valid pig and the count is accumulated.

[0014] Furthermore, in step five, the process of using a working condition classification model to perform binary classification judgment on the pig target that triggers the edge secondary classification verification includes: The detection box is expanded outwards and local feature images are cropped, preserving the contextual features of the pig, the fence, and the ground environment. These images are then fed into a trained work condition classification model for binary classification, distinguishing between the first category of pigs obscured by fences and the second category of pigs obscuring the fences. The model's output is used as the final determination, with valid work conditions counted cumulatively and invalid conditions not counted. The first type of fence obstructing the pig's body refers to the working condition where the corresponding pig is outside the target pen; the second type of pig obstructing the fence refers to the effective working condition where the corresponding pig is inside the pen.

[0015] Furthermore, the expanded detection frame is limited to preserving the contextual features of the pig, the fence, and the ground environment.

[0016] Furthermore, when the model input is video data, multiple frames of images are extracted from the video for recognition, the recognition results of multiple frames are fused and statistically analyzed, and the same pig that appears repeatedly in the same shooting segment is deduplicated based on the spatial location, outline region and short-term motion continuity of the pig.

[0017] This invention further proposes a pig pen counting system that integrates a multi-task model, comprising: The image acquisition module is used to acquire on-site images or video data of the pigpen; The image preprocessing module is used to perform size adjustment, image enhancement, blurry image filtering, and video frame extraction on the acquired data. The multi-task model analysis module uses a deep learning model that integrates object detection and instance segmentation to determine the location of each pig target in the image and obtain the contour region of the key area of ​​the pig house. The pen affiliation determination module is used to comprehensively determine the pen affiliation of pigs based on the spatial overlap between the pig detection area and the key area of ​​the pigpen, the location of the center point below the pig, and the visual characteristics between the pig and the boundary of the pigpen. The counting and statistics module is used to summarize the pig instances in each pigpen based on the pen ownership determination results and generate the corresponding number of pigpen results; The results output module is used to display the counting results.

[0018] Furthermore, the enclosure ownership determination module includes: The basic attribution determination unit is used to uniformly adopt the midpoint of the bottom edge of the pig detection frame as the unique determination coordinate of the pig area attribution, and call the point contour position detection algorithm to determine whether the determination coordinate is located inside or on the contour of the target pig pen breeding boundary. The fence overlap triggering unit is used to capture the local fence mask area corresponding to the overall detection bounding box of a single pig, traverse the pixels of this local area, and if any pixel within the detection bounding box overlaps with the pixels of the expanded fence buffer, then the edge secondary classification verification is triggered. The edge classification verification unit is used to expand the detection box range of the pig target that triggers the verification, crop the local feature image, and feed it into the trained working condition classification model for binary classification judgment. The output result of the classification model is used as the final judgment basis.

[0019] Compared with the prior art, the beneficial effects of the present invention are as follows: This invention integrates three visual task models—target detection, instance segmentation, and working condition classification—to construct a dual-standard hierarchical judgment mechanism. For non-fence areas, it employs a single-point efficient judgment based on the midpoint of the bottom edge of the detection box. For error-prone areas around fences, it uses a dual-layer verification mechanism combining target bounding box and fence mask pixel overlap triggering with AI classification. This effectively solves the problems of difficulty in distinguishing individual pigs when they are occluded and stuck together in complex multi-pen farming scenarios, miscounting between adjacent pens, and inaccurate pen affiliation determination. While maintaining algorithm inference speed, it significantly improves the accuracy of edge-complex scene recognition and greatly enhances the overall algorithm's adaptability and anti-interference capabilities to various complex on-site environments such as changes in pig farm lighting, obstruction by debris, and dense crowding of pigs. This achieves high efficiency and accuracy in pig counting in multi-pen environments. Attached Figure Description

[0020] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. In the drawings: Figure 1 This is a schematic diagram of the overall process of the pig counting method in a pigpen that integrates a multi-task model according to the present invention; Figure 2 This is a diagram of the actual pigpen as input to the model in this embodiment of the invention; Figure 3 This is a graph showing the pig counting results output by the multi-task model in this embodiment of the invention. Detailed Implementation

[0021] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the disclosure to those skilled in the art. It should be noted that, unless otherwise specified, the embodiments and features described herein can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.

[0022] This embodiment proposes a method for counting pigs in pigpens by integrating multiple task models. Specifically, this embodiment integrates multiple task models, including an object detection model, an instance segmentation model, and a binary classification model. The object detection model is trained using a total of 709 images (567 in the training set, 72 in the validation set, and 70 in the test set); the instance segmentation model is trained using 130 images (104 in the training set and 26 in the test set); and the binary classification model is trained using 290 images (231 in the training set, including 142 images of Class I (pigs obscured by fences) and 89 images of Class II (pigs obscuring fences); and 59 in the validation set, including 36 images of Class I and 23 images of Class II).

[0023] According to the actual situation ( Figure 1 The detection model predicts the location of all pigs in the entire image and marks them with bounding boxes. The segmentation model divides a pigpen into three parts: the ground, the fence, and the concrete wall, to achieve different pigpen classifications. After segmentation, the method of extracting the pigpen mask and comparing it with the midpoint of the bottom edge of the bounding box is used to determine whether it belongs to a specific pigpen. Figure 2 The location / localization feature method can solve the problem of identifying and counting most pigs in the same pigpen. However, pigs that are close to the sides of the fence are prone to misclassification using only the location feature method. To address this, this embodiment extracts images of pigs whose bodies overlap with the fence to train a classifier. The classifier determines whether a pig is inside the pen by classifying whether it is outside or inside the fence. The method used by the classifier is called visual feature / deep learning. Figure 2 ).

[0024] System structure such as Figure 2 As shown, the input image is first fed into the detection and segmentation model to obtain the full-image pig detection result and the input image segmentation result. The segmented result is then processed in two steps: (1) Morphological dilation of the railing region: The original railing lines output by semantic segmentation are too thin and cannot cover the critical edge region; in this embodiment, morphological dilation operation is performed on the railing binary mask, and the railing adjacent buffer is uniformly expanded to both sides based on the real railing pixel region. This buffer serves as the critical judgment region of the railing and is used to trigger the edge verification logic in the future.

[0025] (2) Pig pen breeding contour extraction: Connected component screening is performed on the segmented ground breeding mask to filter out small noise connected components and invalid background connected components at the image edge. The effective surface connected component contour with the largest area is selected as the final standard pig pen breeding boundary contour, which is recorded as the target judgment contour. This contour serves as the unique boundary benchmark for the pig pen belonging to the entire image.

[0026] Then, based on the detection results obtained through inference by the detection model, the rectangular bounding boxes of all pig targets in the image are output, obtaining the top-left coordinates of each pig. Lower right coordinates All parameters of the pig detection frame are retained for subsequent location logic determination.

[0027] Determining the territorial ownership of pigs: This embodiment uniformly uses the midpoint of the bottom edge of the pig detection frame as the unique coordinate for determining the pig's region affiliation, and does not use easily drifting determination points such as the object's center point or head feature points; calculation method: horizontal midpoint. y-coordinate of the bottom edge .

[0028] First, the coordinates are truncated to prevent them from crossing the boundary, limiting the coordinate range to within the image pixel range. Then, the point contour position detection algorithm is called to determine whether the midpoint of the bottom edge is located inside the pigpen target breeding contour: if the coordinates are inside the contour or on the contour line, the pig is directly determined to be a pig raised inside the pen; if the coordinates are outside the contour, it is determined to be an invalid pig outside the pen.

[0029] This step is the basic decision-making logic for the entire map: for all pigs in the non-fence area of ​​the pigpen and all pigs in the upper and lower walls, the final ownership is determined only by the midpoint of the bottom edge, without initiating any secondary model verification.

[0030] Railing area verification trigger determination: This invention abandons the flawed logic of traditional single-point touch railing trigger verification and full-contour edge distance trigger verification; it uses a target bounding box and railing mask pixel overlap trigger mechanism: Extract the local fence mask area corresponding to the overall detection bounding box of a single pig, and traverse the pixels in this local area; if any pixel within the pig detection box coincides with a pixel in the expanded fence buffer area, it is determined that the pig target and the fence area overlap, and subsequent secondary edge classification verification is automatically triggered; if there is no pixel overlap, skip the classification model inference directly.

[0031] This logic only initiates verification for pigs living around the fence.

[0032] For pig targets whose fences overlap and trigger verification, the detection box is expanded outwards to a moderate extent and local feature images are cropped to preserve the contextual features of the pig, fence, and ground environment. The images are then fed into the trained work condition classification model for binary classification: distinguishing between the first type of pig whose body is obscured by the fence (pigs outside the target pen, not counted) and the second type of pig whose body is obscured by the fence (pigs inside the pen / valid pigs, counted).

[0033] This invention establishes complete branch execution logic: 1. If there is no pixel overlap between the live pigs in the pen and the detection frame and the fence, they are directly determined as valid live pigs, and the count is accumulated. 2. There is pixel overlap between the pigs in the pen and the detection frame and the fence: The final judgment is based on the output of the classification model. Valid working conditions are counted cumulatively, and invalid working conditions are not counted. 3. Pigs outside the pen: If they touch the fence, the classifier will determine their status; if they do not touch the fence, they will be considered invalid pigs and will not be counted. Finally, the image visualization outputs a green valid detection box inside the circle and a red invalid detection box outside the circle, outputting the total number of pigs in a single pigpen in real time. (See the result image) Figure 3 ) The differentiating features of the counting core of this invention are as follows: 1. Dual-standard layered judgment: For non-railing areas, a high-efficiency single-point judgment is adopted, while error-prone areas around the railings adopt a dual-layer verification of area overlap combined with AI classification, which takes into account both algorithm reasoning speed and accuracy in complex scenarios. 2. Added fence verification trigger logic: The traditional single-point touch trigger has been upgraded to target bounding box area overlap trigger, which can effectively solve the problem of unclear ownership of pig pens on both sides of the pigpen fence during the model recognition process; Based on the above methods, this embodiment further provides a pig counting system in pigpens that integrates a multi-task model, which is applied to scenarios where counting personnel use mobile phones to take pictures of different pigpens in pig farms and complete the counting of the number of pigs.

[0034] During inspections of pig farms, counting personnel use their mobile phone cameras to take photos or short videos of the pig pens to be counted. The collected images or videos can include a single pig pen or multiple adjacent pig pens simultaneously. The mobile phone then inputs the collected image or video data into the pig counting system, which automatically identifies the individual pigs in the image, key areas of the pig pens, and the relationship between each pig and its pen, and outputs the number of pigs in the corresponding pen. In this embodiment, the system includes an image acquisition module, an image preprocessing module, a multi-task model analysis module, a pen ownership determination module, a counting and statistics module, and a result output module. The image acquisition module uses a mobile phone camera used by the counting personnel to acquire images or video data of the pigpen. The image preprocessing module performs processing on the acquired data, including resizing, image enhancement, blur filtering, and video frame extraction, to reduce the impact of changes in lighting, shooting angle, slight shaking, or insufficient image clarity during mobile phone shooting on the recognition results.

[0035] The multi-task model analysis module employs a deep learning model that integrates object detection and instance segmentation. The object detection task determines the approximate location of each pig target in the image, while the instance segmentation task obtains the contour regions of key areas in the pigpen. Through the joint output of these tasks, the system can not only determine whether pig targets exist in the image but also further determine the spatial range of the pigpen.

[0036] The pen affiliation module is used to determine whether each detected pig belongs to a pen to be counted. Specifically, the system comprehensively determines the pen affiliation of a pig based on the spatial overlap between the pig detection area and the key area of ​​the pen, the position of the pig's center point, and the visual characteristics between the pig and the pen boundary. When the main outline of a pig is located within the statistical area of ​​a certain pen, the system determines that it belongs to that pen; when a pig is located near the boundary of an adjacent pen, with only part of its body visible, or is obscured by other pigs, the system determines the pen affiliation based on the pen affiliation confidence level to reduce false counts between adjacent pens.

[0037] For short video input from mobile phones, the system can extract multiple frames from the video for recognition and then fuse and statistically analyze the recognition results. For the same pig appearing repeatedly in the same video clip, the system performs deduplication based on its spatial location, outline region, and short-term motion continuity to avoid duplicate counting due to repeated recognition across multiple frames. For single image input, the system directly counts the number of pigs in the image and their pen affiliation. If multiple pigpens exist in the image, the system counts the number of pigs identified as belonging to each pen separately.

[0038] The counting and statistics module summarizes the pig instances in each pen based on the pen ownership determination results, generating the corresponding number of pigs in each pen. The results output module displays the counting results on a mobile device for subsequent inventory management, inspection records, and anomaly monitoring.

[0039] During model training, images or videos of pigpens from different pig farms, with different pen structures, under different lighting conditions, and at different shooting angles are collected in advance. The bounding boxes for pigs, key areas of the pigpens, and the pen to which each pig belongs are labeled in the data. A multi-task model is trained using this labeled data to adapt to common scenarios in mobile phone photography, such as densely packed pigs, occlusion and adhesion, fence interference, uneven lighting, and interference from adjacent pigpens. After training, the model can be deployed locally on a mobile device or on a server, allowing the mobile device to upload images or videos and receive the recognition results.

[0040] Through the above implementation method, counting personnel do not need to enter the pigpen or count pigs one by one manually. They only need to use their mobile phones to take pictures of the pigpen to be counted, and the system can automatically complete the pig detection, instance segmentation, identification of key areas of the pigpen, pen ownership determination and quantity statistics, thereby improving the efficiency, accuracy and automation of pig counting in multi-pigpen environments of pig farms.

[0041] It is understandable that this invention perfectly integrates three task models, realizing both column counting and pig counting, and has the following advantages: 1. Layered and collaborative task management, with each performing its own function without interference. This invention integrates three types of visual task models: semantic segmentation, object detection, and image classification, clearly defining their respective functional boundaries: semantic segmentation is used to complete pixel-level region division of the pigpen floor and fence area; object detection is used to quickly locate the target position of pigs throughout the farm; and image classification is used to identify complex conditions at the fence edge. The three types of models have clear division of labor and smooth process connection, avoiding the recognition shortcomings caused by the functional limitations of a single model.

[0042] 2. Balancing global judgment accuracy with local detail correction capability: By integrating multiple models, a combination mode of global coarse judgment and local fine judgment is achieved. The detection model is used to quickly complete the screening of large batches of pig targets, the segmentation model is used to delineate the breeding area and the dangerous area of ​​the fence, and the classification model is used to accurately correct difficult samples such as those that are pressed on the line, occluded, or crossed the fence. This ensures both overall counting efficiency and improves the accuracy of recognition in complex edge scenes.

[0043] 3. Significantly improve the overall algorithm's scene adaptability: A single vision task is difficult to meet multiple requirements such as region division, target localization, and state differentiation at the same time. After the three types of models are integrated, they can effectively adapt to various complex on-site environments such as changes in lighting in pig farms, obstruction by debris, dense crowding of pigs, random placement of limbs, and confusion of wall and ground textures. The anti-interference ability is far higher than that of a single algorithm structure, and the practicality on site is stronger.

[0044] 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 it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.

Claims

1. A method for counting pigs in a pigpen by integrating a multi-task model, characterized in that, include: Step 1: Acquire image data of the pig pens to be counted, and input the image data into the target detection model and the instance segmentation model respectively to obtain the bounding box of the whole image pig target detection and the segmentation result of the pig pen region; Step 2: Post-process the pigpen area segmentation results, including performing morphological dilation operations on the binary mask of the fence area and filtering the connected components of the ground farming mask; Step 3: Based on the pig target detection bounding box, the midpoint of the bottom edge of the pig detection box is uniformly used as the unique determination coordinate of the pig area. The point contour position detection algorithm is called to determine whether the determination coordinate is located inside or on the contour of the target pig pen breeding boundary. If so, the pig is determined to be a pig raised inside the pen; otherwise, it is determined to be an invalid pig outside the pen. Step 4: For targets identified as pigs raised within the enclosure, determine whether a secondary classification verification is needed based on the overlap triggering mechanism between the target bounding box and the fence mask pixels; Step 5: Use the working condition classification model to perform binary classification judgment on the pig targets that trigger the edge secondary classification verification; Step 6: Summarize the number of pigs identified as having valid inventory in each pigpen and output the total number of pigs in inventory in the corresponding pigpen in real time.

2. The method according to claim 1, characterized in that, In step one, the target detection model uses a deep learning target detection algorithm, and the instance segmentation model uses a semantic segmentation algorithm to segment the pigpen area into three categories: ground, fence, and cement wall.

3. The method according to claim 1, characterized in that, In step two, the morphological dilation operation includes: using the real railing pixel area as a reference, uniformly expanding and defining the railing adjacent buffer zone to both sides, and using the railing adjacent buffer zone as the railing critical determination area for subsequent triggering edge verification logic; The connected component filtering of the ground-based aquaculture mask includes: filtering connected components with minute noise and invalid background connected components at the image edges, and selecting the effective surface connected component contour with the largest area as the final standard pigpen aquaculture boundary contour.

4. The method according to claim 1, characterized in that, In step three, the determination coordinates are calculated as follows: horizontal midpoint y-coordinate of the bottom edge ; in The coordinates of the upper left corner of the pig detection enclosure are shown. The coordinates are the bottom right. Before calling the point contour position detection algorithm, the judgment coordinates are first subjected to boundary truncation to prevent them from going out of bounds, thus limiting the coordinate range to within the image pixel range.

5. The method according to claim 1, characterized in that, In step four, the process of determining whether a secondary classification verification is needed based on the overlap triggering mechanism between the target bounding box and the railing mask pixels includes: Extract the local fence mask area corresponding to the overall detection bounding box of the pig, traverse the pixels in this local area, and if any pixel within the detection bounding box overlaps with the pixels of the expanded fence buffer, it is determined that the pig target overlaps with the fence area and triggers edge secondary classification verification; if no pixels overlap, the pig is directly determined to be a valid pig and the count is accumulated.

6. The method according to claim 1, characterized in that, In step five, the process of using a working condition classification model to perform binary classification judgment on the pig target that triggers the edge secondary classification verification includes: The detection box is expanded outward and the local feature image is cropped. The local feature image retains the contextual features of the pig body, fence, and ground environment. It is then fed into the trained working condition classification model for binary classification to distinguish between the first type of fence occlusion of the pig body and the second type of pig occlusion of the fence. The output result of the classification model is used as the final judgment basis. Valid working conditions are counted cumulatively, while invalid working conditions are not counted. The first type of fence obstructing the pig's body refers to the working condition where the corresponding pig is outside the target pen; the second type of pig obstructing the fence refers to the effective working condition where the corresponding pig is inside the pen.

7. The method according to claim 6, characterized in that, The expanded detection frame is limited to preserving the contextual features of the pig, the fence, and the ground environment.

8. The method according to claim 1, characterized in that, When the model input is video data, multiple frames of images are extracted from the video for recognition. The recognition results of multiple frames are fused and statistically analyzed. Based on the spatial location, outline region, and short-term motion continuity of the pigs, the same pig that appears repeatedly in the same shooting segment is deduplicated.

9. A pig pen counting system integrating a multi-task model, characterized in that, include: The image acquisition module is used to acquire on-site images or video data of the pigpen; The image preprocessing module is used to perform size adjustment, image enhancement, blurry image filtering, and video frame extraction on the acquired data. The multi-task model analysis module uses a deep learning model that integrates object detection and instance segmentation to determine the location of each pig target in the image and obtain the contour region of the key area of ​​the pig house. The pen affiliation determination module is used to comprehensively determine the pen affiliation of pigs based on the spatial overlap between the pig detection area and the key area of ​​the pigpen, the location of the center point below the pig, and the visual characteristics between the pig and the boundary of the pigpen. The counting and statistics module is used to summarize the pig instances in each pigpen based on the pen ownership determination results and generate the corresponding number of pigpen results; The results output module is used to display the counting results.

10. The system according to claim 9, characterized in that, The enclosure ownership determination module includes: The basic attribution determination unit is used to uniformly adopt the midpoint of the bottom edge of the pig detection frame as the unique determination coordinate of the pig area attribution, and call the point contour position detection algorithm to determine whether the determination coordinate is located inside or on the contour of the target pig pen breeding boundary. The fence overlap triggering unit is used to capture the local fence mask area corresponding to the overall detection bounding box of a single pig, traverse the pixels of this local area, and if any pixel within the detection bounding box overlaps with the pixels of the expanded fence buffer, then the edge secondary classification verification is triggered. The edge classification verification unit is used to expand the detection box range of the pig target that triggers the verification, crop the local feature image, and feed it into the trained working condition classification model for binary classification judgment. The output result of the classification model is used as the final judgment basis.