Method for quantifying duration of aggressive behavior of pigs

CN123016360APending Publication Date: 2026-10-30ANHUI AGRICULTURAL UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611495764.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-24
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

然而,短时漏检、预测抖动以及交互行为本身的间歇性,往往导致事件边界难以稳定确定,进而造成事件片段碎片化、持续时间统计偏差以及个体归因不准确等问题,最终影响攻击持续时间估计的准确性

Benefits of technology

[0018]本发明实施例提供的技术方案与现有技术相比具有如下优点:在多目标跟踪方面,本文将遮挡周期作为建模对象,通过遮挡证据提取和遮挡轨迹优先重连的机制,显著提升密集遮挡与短时消失场景下的轨迹连续性与身份一致性。验证了其在遮挡恢复阶段抑制身份切换错误的有效性,为后续感兴趣区域对齐时空建模奠定了可靠输入基础。在行为识别方面,本文提出在双时间尺度建模的基础上,于感兴趣区域级隐式注入“猪—猪交互”关系推理,通过上下文记忆库与注意力聚合将交互证据写回个体表征,从而更有效地区分攻击与外观相似的非攻击性交互。加入P2P交互建模与该任务的判别本质高度一致。在个体级量化输出方面,本文基于轨迹编号将帧级预测整理为事件片段集合并进行集合/事件两级评估,得到该闭环流程能够在真实群养场景下提供具有可用精度的个体攻击时长统计。综合来看,本发明不仅提升了攻击行为识别性能,也实现了从识别结果到管理指标的可解释转换,为群养猪舍攻击行为的智能监测与精准干预提供了可落地的技术支撑。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123016360A_ABST
    Figure CN123016360A_ABST
Patent Text Reader

Abstract

The application relates to the technical field of image data processing, and discloses a pig attack behavior duration quantification method, which comprises the following steps: identifying each pig individual in a monitoring video and giving an identity mark, generating the motion track of each pig individual according to the identity mark, and constructing the space-time segment of each pig individual corresponding to each identity mark according to the motion track; inputting the space-time segment into a P2P-SlowFast network to obtain the behavior judgment result of each pig individual corresponding to each identity mark in each frame; the behavior judgment result comprises the identity mark and the corresponding attack behavior category; the probability distribution of the attack behavior category and the non-attack behavior category is obtained through a classification head, and the time dimension information in a shared feature representation is combined to output the behavior judgment result and the corresponding attack behavior duration; and the pig attack behavior duration quantification method can improve the attack behavior recognition performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of image data processing technology, and in particular to a method for quantifying the duration of aggression behavior in pigs. Background Technology

[0002] In large-scale pig farming, group rearing can improve pen utilization and reduce per-head feeding costs, but it also significantly increases the probability of fighting and aggressive behavior among pigs. Such aggressive behavior not only causes external injuries such as bites and scratches, and induces infections and stress responses, but also leads to a chain reaction of negative effects such as decreased feed intake, reduced growth performance, increased medication costs, and higher culling rates, ultimately resulting in direct and indirect economic losses. Currently, pig farms still rely mainly on manual inspections to detect fighting incidents, but this method requires continuous investment of manpower, has high management costs, and is difficult to maintain stable coverage in the long term. Furthermore, it is affected by the intermittent nature and subjectivity of inspections, making it difficult to achieve real-time monitoring and consistent early warning. More importantly, manual observation makes it difficult to accumulate traceable and comparable individual-level quantitative records, which is insufficient to support the construction of long-term profiles for problematic individuals and precise intervention decisions.

[0003] In recent years, computer vision methods have been increasingly applied to pig behavior monitoring, and significant progress has been made in the automatic identification of aggressive pig behaviors. Early studies often treated aggressive behavior as a segment-level or short-sequence classification task, determining whether aggressive behavior existed in video clips. In recent years, related research has further begun to attempt to estimate the time interval or duration of aggressive behavior. However, most of these studies still remain at the group level of behavior assessment and have not yet achieved reliable quantification of the individuals involved in the attack, thus making it difficult to support individual-level behavior tracking and event attribution. Some related works have attempted to detect the behavior of individual pigs, but this usually relies on manually assigning different individuals pre-numbered or labeled, and then observing the behavior of specific individuals based on this, which is complex to implement and has limited scalability.

[0004] Furthermore, aggressive behaviors are visually highly similar to various normal social behaviors. In group settings, normal interactions such as approaching, crowding, chasing, or brief physical contact often closely resemble real aggressive behaviors in appearance and movement patterns, making existing behavior recognition models prone to high false positives in such scenarios.

[0005] Furthermore, even if the system can detect attack behavior, converting frame-by-frame or segment-level predictions into reliable event statistics remains a key challenge. Practical aquaculture management focuses not only on whether an attack occurs, but also on when it begins, when it ends, and how long it lasts. However, short-term missed detections, prediction jitter, and the intermittent nature of the interaction itself often make it difficult to stably determine event boundaries, leading to fragmented event segments, statistical biases in duration, and inaccurate individual attributions, ultimately affecting the accuracy of attack duration estimation.

[0006] In summary, existing methods typically only address one part of the problem chain. Current systems still struggle to answer the three most critical questions in livestock management simultaneously within a unified framework: which pigs were involved in the attack, whether the observed interactions truly constitute an attack, and how long the event lasted. Summary of the Invention

[0007] To address the aforementioned technical problems, this invention provides a method for quantifying the duration of aggression behavior in pigs, which can improve the performance of aggression behavior recognition.

[0008] This invention provides a method for quantifying the duration of aggressive behavior in pigs, comprising the following steps: Each individual pig in the surveillance video is identified and assigned an identity tag. The movement trajectory of each individual pig is generated based on the identity tag, and then the spatiotemporal segment of each individual pig corresponding to the identity tag is constructed based on the movement trajectory. The spatiotemporal segment is input into the P2P-SlowFast network to obtain the behavior judgment result of each pig individual corresponding to each identity in each frame; the behavior judgment result includes the identity and its corresponding attack behavior category. The P2P-SlowFast network comprises a cascaded SlowFast dual-path backbone network, a context module, a P2P relationship reasoning module, and a classification head. The SlowFast dual-path backbone network extracts and fuses multi-scale spatiotemporal features of spatiotemporal segments to form a shared feature representation. The context construction module extracts and compresses a fixed number of context tags from the shared feature representation. The P2P relationship reasoning module uses the subject tags extracted from the shared feature representation as queries, with the context tags as keys and values, and employs a multi-head attention mechanism to retrieve interaction cues related to the current individual. These cues are then integrated into the subject tags using residual connections, resulting in enhanced subject tags. The classification head maps the enhanced subject tags to probability distributions of attack and non-attack behavior categories, and, combined with the temporal dimension information in the shared feature representation, outputs the behavior judgment result and its corresponding attack duration.

[0009] Furthermore, the contextual tags are constructed by performing temporal mean pooling and spatial adaptive pooling on the shared feature representations in sequence to obtain a fixed number of contextual tags.

[0010] Furthermore, the enhanced subject label is input into the classifier, and the output behavior judgment result is as follows: The enhanced subject markers are input into the fully connected layer and mapped to the dimension of the number of categories. The probability distributions of attack behavior categories and non-attack behavior categories are calculated using the Softmax function; The category with the highest probability is taken as the behavioral judgment result of the pig individual corresponding to the spatiotemporal segment.

[0011] Furthermore, the P2P relationship reasoning module is configured as follows: The main tag and the context tag are used as input; The main tag and the context tag are mapped to a unified feature space through a linear projection layer to generate queries, keys, and values. The query and key are input into the multi-head attention layer, the attention weights are calculated and normalized by Softmax to obtain the attention distribution, the values ​​are weighted and aggregated according to the attention distribution, and the interaction cues related to the current individual are retrieved; wherein, when calculating the attention weights, the multi-head attention layer injects the geometric prior bias generated by the normalized bounding box of the current individual through a lightweight multilayer perceptron. The retrieved interaction cues are multiplied by an adjustable coefficient and then superimposed onto the original subject markers using a residual connection method to obtain intermediate features; The intermediate features are sequentially input into the layer normalization layer, the feedforward network layer, and the second layer normalization layer to obtain the enhanced subject label.

[0012] Furthermore, the output behavior judgment result and its corresponding attack behavior duration are as follows: Using trajectory numbers as the unit, the judgment results of consecutively identified attack behaviors are aggregated into one or more independent attack event segments according to the continuity of time; For each attack event segment, obtain the timestamp corresponding to its start frame as the attack start time, and obtain the timestamp corresponding to its end frame as the attack end time. The difference between the attack end time and the attack start time is calculated as the duration of the individual in the attack event segment. If there are multiple attack event segments under the same identity, output the duration of each attack event segment separately, or output the sum of the durations of all attack event segments.

[0013] Furthermore, before outputting the behavior judgment result and its corresponding attack behavior duration, the process includes an optimization step for the behavior judgment result that is continuously judged as an attack behavior: removing discrete segments with a duration shorter than a first set threshold, and merging adjacent segments of the same type with a time interval less than a second set threshold; the first set threshold is less than the second set threshold.

[0014] Furthermore, after determining the duration of an individual's participation in the attack, an event-level quantitative evaluation step is also included: using trajectory numbers as units, the post-processed continuous attack frame sequences are aggregated into independent attack events, the start time, end time, and duration of each event are calculated, and based on real annotations, the whole-track time intersection-union ratio, the number of successfully matched events, the number of missed events, the number of false alarm events, and the mean absolute error of the start time, end time, and duration of the attack events under each identity are calculated, and finally, a summary JSON output containing the average indicators of all individuals is generated.

[0015] Furthermore, each individual pig in the surveillance video is assigned an identity tag, and the movement trajectory of each individual pig is generated based on the identity tag. Then, the spatiotemporal segment of each individual pig corresponding to the identity tag is constructed based on the movement trajectory. This includes: inputting the surveillance video frame by frame into a multi-target tracker; the multi-target tracker performs cross-frame association matching on the individual pigs in each frame image, generates a corresponding identity tag for each individual pig, and generates the movement trajectory of each individual pig based on the identity tag. Based on the identity identifier, the corresponding movement trajectory is determined. For each individual pig under the identity identifier, the image region of the corresponding individual pig is cropped from the historical buffer frame according to the movement trajectory and the detection box of the latest frame is used as the reference. The cropped regions of multiple consecutive frames are stacked along the time dimension to construct a spatiotemporal segment bound to the identity identifier.

[0016] Furthermore, during the tracking process, the multi-target tracker maintains a confidence baseline for each motion trajectory. The confidence baseline represents the detection confidence level of the pig individual corresponding to the trajectory under normal visibility conditions. When the detection confidence of the trajectory in the current frame decreases by more than a set range relative to the confidence baseline, the trajectory is marked as to be occluded, and the state information before occlusion is recorded. For detection results that are not successfully associated in the current frame, reconnect with the trajectory in the occlusion state based on the state information; if the reconnection is successful, the trajectory is restored and its original identity is maintained; if the reconnection fails, a new identity and a new motion trajectory are created for the detection result.

[0017] Furthermore, the multi-target tracker divides the detected pig individuals into high-score and low-score groups based on their confidence level. For individuals in the high-score group, re-identification appearance features are first extracted, and then a candidate pool is formed with the current active trajectory and the lost trajectory. The fusion matrix of the intersection-union distance and the feature embedding distance is calculated, and the first round of association matching is completed through the Hungarian algorithm. The remaining unmatched high-score trajectories are matched with low-score individuals in the second round of intersection-union matching. The confidence baseline is maintained as follows: after each successful association, it is selectively updated based on how close the current detection confidence is to the existing baseline level; the confidence baseline is only updated based on the current detection confidence when the difference between the current frame's detection confidence and the existing baseline level is less than a set value.

[0018] Compared with existing technologies, the technical solution provided by this invention has the following advantages: In multi-target tracking, this paper takes the occlusion period as the modeling object. Through the mechanism of occlusion evidence extraction and priority reconnection of occlusion trajectories, it significantly improves the trajectory continuity and identity consistency in dense occlusion and short-term disappearance scenarios. It verifies the effectiveness of suppressing identity switching errors in the occlusion recovery stage, laying a reliable input foundation for subsequent spatiotemporal modeling of region of interest alignment. In behavior recognition, this paper proposes to implicitly inject "pig-pig interaction" relationship reasoning at the region of interest level based on dual-timescale modeling. Through context memory and attention aggregation, interaction evidence is written back to individual representations, thereby more effectively distinguishing between aggressive and non-aggressive interactions with similar appearances. The addition of P2P interaction modeling is highly consistent with the discriminative nature of this task. In terms of individual-level quantization output, this paper organizes frame-level predictions into a set of event fragments based on trajectory numbers and performs set / event two-level evaluation. The closed-loop process can provide individual attack duration statistics with usable accuracy in real group-raising scenarios. In summary, this invention not only improves the performance of attack behavior identification, but also realizes the interpretable conversion from identification results to management indicators, providing practical technical support for intelligent monitoring and precise intervention of attack behavior in group-raised pig houses. Attached Figure Description

[0019] Figure 1 A flowchart illustrating a method for quantifying the duration of a pig's aggressive behavior, provided as an embodiment of the present invention; Figure 2 The overall process of FTO-SORT-OCTM (FTO-SORT-OCTM tracker, FTO-SORT is a lightweight multi-target tracking method for online long-term monitoring. The original FTO-SORT online association process introduces the Occlusion-Coupled Track Maintenance Module (OCTM) to form FTO-SORT-OCTM) provided for the embodiments of the present invention. Figure 3 A framework diagram for detecting P2P-SlowFast attack behavior provided in an embodiment of the present invention. Detailed Implementation

[0020] The following detailed description of a specific embodiment of the present invention is provided in conjunction with the accompanying drawings. However, it should be understood that the scope of protection of the present invention is not limited to the specific embodiment.

[0021] In the description of this invention, it should be understood that the terms "center," "longitudinal," "lateral," "length," "width," "thickness," "upper," "lower," "front," "rear," "left," "right," "vertical," "horizontal," "top," "bottom," "inner," "outer," "axial," "radial," and "circumferential" indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are only for the convenience of describing the technical solution of this invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this invention.

[0022] The present invention will be described below through several specific embodiments. To keep the following description of the embodiments clear and concise, detailed descriptions of known functions and components may be omitted. When any component of an embodiment of the present invention appears in more than one drawing, the component may be represented by the same reference numerals in each drawing.

[0023] Figure 1 This is a flowchart illustrating a method for quantifying the duration of aggression behavior in pigs, provided in an embodiment of the present invention. Figure 2 The overall flow of FTO-SORT-OCTM (FTO-SORT-OCTM tracker, FTO-SORT is a lightweight multi-target tracking method for long-term online monitoring. The original FTO-SORT online association process introduces the Occlusion-Coupled Track Maintenance Module (OCTM) to form FTO-SORT-OCTM) provided in the embodiments of the present invention. Figure 3 A framework diagram for detecting P2P-SlowFast attack behavior provided in an embodiment of the present invention.

[0024] like Figure 1 As shown, this embodiment provides a method for quantifying the duration of aggressive behavior in pigs. This method constructs a complete processing chain from video stream input to individual-level aggressive event output, aiming to solve the technical problems of difficulty in attributing individual aggressive behavior and inaccurate duration statistics in group-housed scenarios. The method mainly includes the following steps: The movement trajectory and identification of each pig in each frame of the surveillance video are obtained, and a spatiotemporal segment is constructed based on this movement trajectory.

[0025] Specifically, this step transforms unstructured video data into structured instance data. In a preferred implementation, the monitoring video is first inspected frame by frame using an object detector (e.g., YOLO series detectors) to obtain the bounding box and confidence score for each individual pig. The detection results are then input into a multi-object tracker, which maintains a unique trackid for each pig through cross-frame association and generates continuous motion trajectories. Based on this, for each motion trajectory, using the bounding box of the latest frame as a reference, the image region corresponding to the pig is cropped from historical buffer frames using Region of Interest Align (RoIAlign) technology. The cropped regions from consecutive T frames (e.g., 32 frames) are stacked along the time dimension to form a fixed-size four-dimensional tensor as a spatiotemporal segment. A spatiotemporal segment is a fixed-size feature tensor generated by RoIAlign operations on a sequence of T consecutive image regions extracted from the movement trajectory of a pig individual corresponding to its identity identifier. The value of T ranges from 16 to 64. The data format of this spatiotemporal segment is typically [time length, number of channels, height, width], and it is strictly bound to a specific identity identifier. It should be understood that although this embodiment lists RoIAlign as a specific means of spatial alignment, other implementations may also use ROI Pooling (Region of Interest Pooling) or other alignment algorithms that can preserve spatial structure information, as long as it can extract the temporal image sequence corresponding to a specific individual from the original image. Through this step, the system can decouple complex group monitoring videos into independent individual behavior samples with identity tags, laying a data foundation for subsequent refined identification.

[0026] The spatiotemporal segment is input into the P2P-SlowFast network to obtain the behavior judgment result of each pig individual corresponding to each identity in each frame; the behavior judgment result includes the identity and its corresponding attack behavior category.

[0027] In this step, the P2P-SlowFast network serves as the core recognition engine. Its internal processing flow specifically includes: extracting and fusing multi-scale spatiotemporal features of spatiotemporal segments through the SlowFast (a dual-path network architecture for video recognition) dual-path backbone network to form a shared feature representation; extracting and compressing a fixed number of context tags from the shared feature representation through the context construction module; using the subject tags extracted from the shared feature representation as queries through the P2P relationship reasoning module, and using the context tags as keys and values, retrieving interaction cues related to the current individual using a multi-head attention mechanism, and integrating the retrieved interaction cues into the subject tags through residual connections, outputting the enhanced subject tags; mapping the enhanced subject tags to the probability distribution of attack behavior categories and non-attack behavior categories through a classification head, and combining the time dimension information in the shared feature representation to output the behavior judgment result and its corresponding attack behavior duration.

[0028] Specifically, it's necessary to distinguish between two conceptual levels: spatiotemporal segments and shared feature representations. Spatiotemporal segments are the network's original input data, while shared feature representations are unified feature tensors formed after processing by the SlowFast dual-path backbone network. These tensors integrate spatial semantic information from the slow path and temporal motion information from the fast path, serving as the common data base for subsequent context construction and relational reasoning. The SlowFast dual-path backbone network is a dual-temporal-scale three-dimensional convolutional neural network, comprising two parallel branches: a slow path and a fast path. The slow path processes spatiotemporal segments at a lower sampling rate (e.g., 4 frames per second) and uses a larger number of channels (e.g., 64 channels) to extract the spatial semantic features and static appearance information of individual pigs. The fast path processes the same spatiotemporal segment at a higher sampling rate (e.g., 16 frames per second) and uses a smaller number of channels (e.g., 8 channels, typically 1 / 8 of the number of channels in the slow path) to capture temporal dynamic information such as the movement trajectory of individual pigs, limb movement speed, and relative movement changes between pigs. After two paths undergo independent 3D convolution operations, the features of the fast path are fused to the slow path through lateral connections, allowing the slow path to acquire motion temporal information while preserving spatial semantic information. In the SlowFast dual-path backbone network, both the slow and fast paths contain multiple 3D convolutional blocks, such as Stage 1 (each Stage consists of several stacked residual blocks) to Stage 4 of ResNet (a residual network). The feature maps output by each convolutional block have different spatiotemporal scales. The features output by shallow convolutional blocks (such as Stage 1 and Stage 2) have high spatial resolution and a short temporal receptive field, enabling them to capture local details of individual pigs, such as ear pose, tail wagging, and fine-grained features like skin texture. The features output by deep convolutional blocks (such as Stage 3 and Stage 4) have low spatial resolution and a long temporal receptive field, enabling them to capture the global morphology of individual pigs and behavioral patterns over long periods, such as complete attack behavior sequences like chasing, biting, and confrontation. By fusing spatiotemporal features at different scales (e.g., upsampling deep features and concatenating them with shallow features, or aggregating them layer by layer through a feature pyramid structure), the network can simultaneously utilize local details and global contextual information, thereby improving its ability to detect attacks. The specific generation method for the shared feature representation is as follows: First, the feature tensors output by the slow path and the fast path are aligned and concatenated in terms of channel dimension through lateral connections and feature projection layers. Then, a fusion convolutional layer (usually a 1×1×1 convolution) is used to perform cross-channel information interaction on the concatenated features, unifying the number of channels to a preset shared dimension. Finally, downsampling operations (including mean pooling in the temporal dimension and adaptive pooling in the spatial dimension) are used to adjust the feature tensors to a uniform temporal length and spatial size, resulting in the shared feature representation.This shared feature representation simultaneously encodes the spatial appearance information, movement temporal information, and spatial positional relationships of all pig individuals in the entire spatiotemporal segment. It can be shared by both the subject tag extraction and context tag extraction branches, avoiding redundant computation and improving model efficiency. The subject tag represents the local feature vector of the pig individual to be identified, while the context tag is a compact compressed representation of the global interaction information of the entire scene. The P2P relationship reasoning module establishes a dynamic connection between individuals and the scene, enabling the model to perceive interaction cues crucial for distinguishing between aggression and normal social interaction, such as the postures and relative movement trends of neighboring pigs, rather than relying solely on the individual's own appearance features. The final behavior judgment result output by the classification head is structured data, containing at least an identity identifier, behavior category label, aggression, non-aggression, and the timestamp corresponding to the judgment. It should be understood that although this embodiment describes a dual-path feature extraction method based on the SlowFast architecture, other backbone networks with spatiotemporal modeling capabilities can also be used in other embodiments, as long as they can generate shared feature representations containing rich spatiotemporal semantics for use by subsequent modules. By introducing a P2P relationship reasoning mechanism, this method can effectively improve the model's ability to detect attack behavior in scenarios with dense occlusion and similar appearance without significantly increasing computational overhead.

[0029] Based on the identification and behavior judgment results, the sequence of image frames continuously judged as attack behavior under each identification is counted, and the duration of the pig's participation in the attack behavior is determined according to the image frame sequence.

[0030] Specifically, because frame-by-frame predictions in deep learning models inevitably involve probability fluctuations and short-term false positives, directly using frame-level results as the final output often leads to event fragmentation. This step transforms discrete frame-level predictions into stable event-level metrics by temporally aggregating consecutive attack frames under the same identity. For example, when a certain identity is... When pigs are continuously identified as engaging in attack behavior between frames 100 and 130, the system aggregates these into a single attack event segment and calculates the start and end times and duration of the event based on the video frame rate. If multiple discontinuous attack event segments exist under the same identity, the duration of each segment can be output separately, or the total cumulative duration can be output according to business needs. This step is crucial in transforming the algorithm's identification results into quantifiable metrics that can be implemented by the livestock management team, ensuring the continuity and interpretability of the output results over time, thereby supporting accurate tracing and intervention decisions for problematic individuals.

[0031] This embodiment further details the internal implementation mechanism of the P2P-SlowFast network to construct a complete closed loop of interactive perception and recognition technology. For example... Figure 3As shown, the core of this network lies in the collaborative work of three sub-modules: context building, P2P relationship reasoning, and classification output, to achieve accurate identification of pig attack behavior.

[0032] As one implementation method, the context tokens are constructed by sequentially performing temporal mean pooling and spatial adaptive pooling on the shared feature representations to obtain a fixed number of context tokens. Specifically, although the shared feature representations contain rich spatiotemporal information, their data dimensionality is high and they contain a large amount of redundant details, making direct use for inter-individual interaction reasoning computationally expensive. To preserve scene-level interaction cues with lower overhead, this embodiment constructs a compact context bank, denoted as E, from the shared feature representation F. Its construction process can be formally represented as follows: , In the formula, E is the set of context tokens obtained from the construction; F s and F f These represent the characteristics of the Slow pathway and the Fast pathway, respectively; φ s and φ f To project the corresponding pathway features onto d c / 2 channel projection layer; Concat is the channel-dimensional concatenation operation; ψ is the projection layer that further projects the concatenated features; Downsample is the downsampling operation that sequentially performs temporal mean pooling and spatial adaptive pooling; Reshape is the dimension reshaping operation; B is the batch size; M is the fixed number of context tokens; d c Γ is the number of feature channels for each token; Γ is the downsampling factor.

[0033] Where, φ s and φ f Treated as a projection layer with 1×1×1 convolution, ReLU activation, and batch normalization, it projects the features of the Slow and Fast pathways onto d, respectively. c / 2 channels. φ f The projected features are then processed by time-adaptive average pooling to make them correlated with φ in the time dimension. sThe output is aligned to facilitate subsequent channel concatenation operations. ψ utilizes the same structure as the projection layer to fuse the concatenated features, enabling cross-channel interaction between Slow and Fast information. Downsample sequentially performs temporal mean pooling and spatial adaptive pooling to obtain M tokens (basic processing units). These tokens serve as scene-level group interaction memories, shared and retrieved by all individuals. This allows the model to perceive the overall dynamic distribution of the group within the current frame without explicitly modeling pairwise relationships between individuals, thus providing rich scene context for subsequent attention aggregation. This design eliminates the need for the model to explicitly calculate pairwise relationships between all individuals; instead, each individual retrieves the necessary interaction cues by searching this memory, significantly reducing computational resource consumption while maintaining the ability to perceive global context. It should be understood that although this embodiment uses a combination of temporal mean pooling and spatial adaptive pooling, in other implementations, max pooling, convolutional downsampling, or Transformer-based token merging methods can also be used to achieve feature compression and aggregation, as long as a fixed-length token sequence representing global scene information can be generated.

[0034] As one implementation method, the P2P relationship reasoning module is configured as follows: Subject tags and context tags are used as input; the subject tags and context tags are mapped to a unified feature space through linear projection layers to generate queries, keys, and values; the queries and keys are input into a multi-head attention layer, attention weights are calculated and normalized using Softmax (normalized exponential function) to obtain an attention distribution; the values ​​are weighted and aggregated according to the attention distribution to retrieve interaction cues related to the current individual; wherein, when calculating attention weights, the multi-head attention layer injects a geometric prior bias generated by a lightweight multilayer perceptron from the normalized bounding box of the current individual; the retrieved interaction cues are multiplied by an adjustable coefficient and then superimposed onto the original subject tags using residual connections to obtain intermediate features; the intermediate features are sequentially input into a layer normalization layer, a feedforward network layer, and a second layer normalization layer to obtain the enhanced subject tags.

[0035] The subject tag is a feature vector extracted from the shared feature representation for each tracked pig individual, used to characterize the individual's behavioral features in the current spatiotemporal segment. Specifically, for pigs with identification marks, a fixed-dimensional feature vector is obtained by cropping and pooling from the shared feature representation based on the detection box position in each frame of the spatiotemporal segment using the RoIAlign operation. This subject tag encodes the pig individual's appearance (e.g., body size, coat color), posture (e.g., standing, lying down, head up), and individual movement state (e.g., movement speed, movement direction), but does not include interaction information with other pigs. Each pig individual corresponds to an independent subject tag.

[0036] Context-based tags are a fixed set of M feature vectors extracted and compressed from shared feature representations through pooling operations. These vectors characterize the dynamic interactions of all pigs in the current scene. Unlike subject tags, which focus on "individuals," context-based tags focus on the "group": each context-based tag encodes information about a spatial region or interaction pattern within the scene. For example, some tags might encode the pattern of "two pigs approaching each other's heads," while others might encode the pattern of "one pig chasing another." All M tags together form a compact scene-level context bank, shared and retrieved by all subject tags. Context-based tags are not tied to specific individuals but rather capture the spatial layout, density, movement flow, and relative positional relationships between pigs within the entire pigpen group from a global perspective.

[0037] The enhanced subject label is a feature vector generated by integrating retrieved interaction cues into the original subject label. It represents the individual characteristics after incorporating group interaction information. Specifically, using the subject label as the query, interaction cues related to the current pig are retrieved from the context subject label set (such as whether other pigs are approaching, whether there is physical contact, whether it is in a crowded area, etc.). The retrieved cues are then superimposed onto the original subject label using residual connections to obtain the enhanced subject label. This enhanced subject label simultaneously contains the individual characteristics of the pig itself and the interaction characteristics with other pigs, enabling the subsequent classifier to comprehensively determine whether the pig is engaging in aggressive behavior based on two dimensions: "what the pig itself is doing" and "what influence other pigs around it have on the pig".

[0038] Specifically, the P2P relationship reasoning module is a key component for achieving "interaction awareness" in this application. In this module, the subject tag serves as the query, representing individual information such as "who I am" and "where I am"; the context tag serves as the key and value, representing global information about "what happened in the scene." Through a multi-head attention mechanism, the model can adaptively filter the most relevant interaction evidence (e.g., the attack posture of neighboring pigs, relative motion vectors, etc.) from global memory. The attention weight calculation and output feature fusion of this multi-head attention mechanism can be formally represented as follows: , Formula Description: Formula for multi-head attention weight calculation and output feature fusion. Where, Let i be the attention weight for query i to key j in the h-th header. This indicates that normalization is performed along the bond dimension. and Let be the query vector and key vector of the h-th header, respectively, where d is the vector dimension. For learnable geometric prior biases, z i W is the final output of multi-head attention. o To output the projection matrix, This indicates that the outputs from each head will be spliced ​​together. This is the value vector of the h-th head.

[0039] Where d=d c / H represents the channel dimension per head, H represents the number of attention heads, and q i =W q a i k j =W k c j v j =W v c j a i Let c represent the characteristics of the i-th individual. j Let represent the j-th label in B. This bias continuously and data-drivenally injects the spatial location and scale of individuals into each attention head, enabling the model to perceive spatial proximity relationships without manually setting distance thresholds. The attention weight of the h-th head is then calculated using Softmax. This guides attention to the tokens related to the spatial location of the current actor (the one performing the behavior). It's particularly important to note that this embodiment introduces a geometric prior bias in the attention weight calculation. This is because in densely populated pig farms, pigs often appear highly similar and frequently overlap. A pure attention mechanism relying solely on semantic features is prone to spatial ambiguity, potentially leading to incorrect attention to visually similar but spatially unrelated distant individuals. By using a lightweight multilayer perceptron (MLP) to transform the normalized bounding box coordinates of the current individual into a geometric bias and injecting it into the attention score, the model is provided with an explicit spatial constraint. This allows the model to perceive "spatial proximity" in a data-driven manner, thereby guiding attention to the local area where physical interaction actually occurs. It should be understood that while this embodiment uses MLP to process normalized bounding boxes as the geometric prior generation method, other embodiments can also use sinusoidal position encoding, relative position encoding, Gaussian distance masks, or other encoding methods that can characterize spatial relationships, as long as spatial position constraints are introduced into the attention calculation.

[0040] Furthermore, the aforementioned P2P relationship reasoning module also employs a residual connection structure with adjustable coefficients. Specifically, the retrieved interaction cues do not directly replace the original subject labels, but rather write back the subject features in a residual manner through learnable coefficients γ. This update process can be formally represented as: , In the formula, â i As an intermediate feature, a i The original subject features are represented by γ, which is a learnable scaling factor, and x is the original subject feature. i For the retrieved interaction information, LN represents the layer normalization operation, FFN represents the feedforward neural network module, and so on. i The enhanced main features.

[0041] Here, LN stands for Layer Normalization, and FFN is a two-layer feedforward neural network with a hidden layer expansion ratio of 2. The parameter γ is initialized to a small value (e.g., 0.01 or 0), allowing the model to rely more on its own features for judgment in the early stages of training, avoiding noise interference introduced due to the interaction module not yet converging. As training progresses, γ adaptively learns through backpropagation and gradually increases, enabling the model to gradually learn to utilize interaction cues to enhance its discriminative ability. This "progressive fusion" strategy effectively improves the model's convergence stability and robustness. Subsequent Layer Normalization (LN) and Feedforward Network (FFN) operations further perform nonlinear transformations and normalization on the fused features, ultimately outputting an enhanced subject label that includes both individual attributes and environmental interaction information.

[0042] As one implementation method, the enhanced subject markers are input into the classifier, and the output behavior judgment result is obtained. Specifically, the enhanced subject markers are input into a fully connected layer and mapped to the dimension of the number of categories; the probability distribution of aggressive behavior categories and non-aggressive behavior categories is calculated using the Softmax function; the category with the highest probability is taken as the behavior judgment result of the pig individual corresponding to the spatiotemporal segment. Specifically, the subject markers enhanced by P2P relationship reasoning already contain sufficient discriminative information. The classifier projects them onto a predefined behavior category space (such as "aggressive" and "non-aggressive" categories, or more granular categories such as "ear biting," "bumping," and "normal walking") through a fully connected layer, and uses the Softmax function to transform the logits (the final raw output) into a normalized probability distribution. The system selects the category with the highest probability as the final recognition result, and can also output this probability value as a confidence reference for subsequent event-level quantization modules to perform threshold filtering or weighted statistics. This standard classification head design ensures the interpretability and numerical stability of the output results, and facilitates integration with existing livestock management systems.

[0043] Building upon the high-precision frame-level behavior recognition achieved in the above embodiments, this embodiment further addresses the technical problem of transforming unstable frame-level predictions into reliable event-level statistical results. Since deep learning models inevitably experience probability fluctuations during frame-by-frame inference, and pig attack behavior itself is intermittent, directly performing statistics based on raw frame-level results often leads to event fragmentation or duration estimation errors. Therefore, this embodiment provides a conversion and optimization mechanism from frame sequences to event fragments.

[0044] As one implementation method, the duration of an individual's participation in an attack is determined based on the image frame sequence. Specifically, the image frame sequence is aggregated into one or more independent attack event segments based on the trajectory number and temporal continuity. For each attack event segment, the timestamp corresponding to its starting frame is obtained as the attack start time, and the timestamp corresponding to its ending frame is obtained as the attack end time. The difference between the attack end time and the attack start time is calculated as the duration of the individual's participation in that attack event segment. If multiple attack event segments exist under the same identity, the duration of each attack event segment is output separately, or the sum of the durations of all attack event segments is output. Specifically, this step establishes a standardized mapping rule from discrete prediction at the algorithm layer to continuous measurement at the business layer. Among them, "based on trajectory number" is the key to ensuring consistency in individual attribution. It ensures that even if multiple pigs interact in the same frame, the system can accurately distinguish and independently count the attack duration of each pig, avoiding the ambiguity of responsibility caused by group-level statistics. For example, when the identity is... When pigs are consecutively identified as exhibiting aggressive behavior between frames 100 and 130, and between frames 200 and 250, the system identifies these as two independent event segments and precisely calculates their durations as 1 second and 1.67 seconds respectively, based on the video frame rate (e.g., 30fps). In practical applications, farm managers may focus on the severity of a single conflict (in which case separate output mode) or the cumulative aggressive tendency of a pig within a day (in which case summation mode). This embodiment supports both output formats, meeting the needs of different refined management scenarios. It should be understood that although this embodiment uses timestamp difference as the duration calculation method, other implementations may also use methods such as multiplying the effective number of frames by the sampling interval, as long as the time span of the event can be accurately represented.

[0045] To further improve the accuracy and robustness of duration statistics, before determining the duration of an individual's aggressive behavior, an optimization process is included for the image frame sequence continuously identified as aggressive behavior: removing discrete segments with a duration shorter than a first set threshold, and merging adjacent segments of the same type with a time interval less than a second set threshold; the first set threshold ranges from 0.3 seconds to 0.8 seconds, and the second set threshold ranges from 0.5 seconds to 1.5 seconds. Specifically, this post-processing step is a necessary technical means to connect the algorithm output with management indicators. The setting of its parameter range is not arbitrary, but a critical balance point derived from the dual constraints of pig behavioral characteristics and model response characteristics.

[0046] The core function of the first threshold setting is to filter out noise segments caused by model false detections or normal short-term social behaviors (such as brief sniffing or touching). In specific implementations, this threshold can preferably be set to 0.3 seconds, 0.5 seconds, or 0.8 seconds. If the threshold is set too small (e.g., less than 0.3 seconds), the system will have difficulty effectively filtering out frequent non-aggressive short-term contacts between pigs, leading to a large number of normal interactions being misjudged as aggression events, significantly increasing the false alarm rate. Conversely, if the threshold is set too large (e.g., greater than 0.8 seconds), it may cause genuine short-term aggression behaviors (such as rapid ear biting followed by immediate separation) to be treated as noise and rejected, resulting in an increased false alarm rate. Therefore, a range of 0.3 seconds to 0.8 seconds retains short-term aggression signals with practical management significance while effectively suppressing high-frequency background noise.

[0047] The core function of the second threshold setting is to repair event fragmentation caused by brief occlusion, pose changes, or a momentary drop in model confidence. In practice, this threshold can be preferably set to 0.5 seconds, 1.0 seconds, or 1.5 seconds. If the threshold is set too small (e.g., less than 0.5 seconds), it cannot cover common occlusion recovery cycles, causing events that should belong to the same attack to be fragmented into multiple pieces, resulting in a shorter duration and higher frequency of statistically analyzed single attacks. Conversely, if the threshold is set too large (e.g., greater than 1.5 seconds), it is easy to incorrectly merge two temporally adjacent but semantically independent attack events, masking the true rhythm and burst characteristics of the attack behavior. Extensive experimental data verification shows that a merging window of 0.5 to 1.5 seconds can adapt to the occlusion duration distribution in most group farming scenarios, maintaining reasonable temporal resolution while ensuring event integrity. It should be understood that the above threshold range is an optimal range derived from the typical monitoring perspective and frame rate of large-scale pig farms. In specific high-resolution or ultra-high frame rate scenarios, those skilled in the art can make adaptive fine-tuning based on this, but its core "denoising-bridging" bidirectional adjustment mechanism remains unchanged.

[0048] As one implementation method, after determining the duration of an individual's participation in the attack, an event-level quantitative evaluation step is also included: Using trajectory numbers as units, the post-processed continuous attack frame sequences are aggregated into independent attack events. The start time, end time, and duration of each event are calculated. Based on real annotations, the whole-track time intersection-over-union ratio (tIoU), the number of successfully matched events, the number of missed events, the number of false alarms, and the mean absolute error of the start time, end time, and duration are calculated for each identity-based attack event. Finally, a summary JSON (lightweight data exchange format) output containing the average indicators of all individuals is generated. Specifically, this step constructs a closed-loop quantitative evaluation system, not only outputting the structured duration data required by the business but also providing multi-dimensional performance metrics to facilitate system iteration optimization and management decision verification. Among them, the whole-track time intersection-over-union ratio (tIoU) measures the degree of overlap between predicted and actual events on the time axis and is a core indicator for evaluating the accuracy of time-series positioning; the mean absolute error (MAE) directly reflects the magnitude of the duration estimation deviation. The final aggregated JSON output, serving as a standardized data carrier, can seamlessly integrate with the farm's digital management platform, directly transforming algorithm results into actionable management reports. For example, the system can automatically generate structured tables containing attack frequency per pig, average duration of each attack, cumulative duration, and confidence scores, assisting farmers in quickly identifying high-risk individuals and developing intervention strategies. This end-to-end transformation from underlying visual features to top-level management indicators demonstrates the completeness and practical value of the proposed technical solution in real-world applications.

[0049] Building upon the quantification of attack events from video streams to individual levels described in the above embodiments, this embodiment further addresses the frequent issues of dense occlusion and short-term disappearance in group-raised pigsties by providing an occlusion-coupled trajectory maintenance mechanism to improve the stability and identity consistency of multi-target tracking. For example... Figure 2 As shown, this mechanism operates independently of the behavior recognition algorithm, focusing on solving trajectory fragmentation and identity switching errors, and providing reliable trajectory input for subsequent spatiotemporal segment construction.

[0050] As one implementation method, the detection bounding boxes and detection confidence scores of each individual pig in each frame of the image are acquired. These boxes and confidence scores are then input into a multi-target tracker. During tracking, the multi-target tracker maintains a confidence reference value for each motion trajectory. When the detection confidence score of the current frame decreases beyond a set range relative to the confidence reference value, the trajectory is marked as awaiting occlusion, and the state information before occlusion is recorded. Before creating a new trajectory, the detection results that were not successfully associated in the current frame are preferentially reconnected with the trajectories in the awaiting occlusion state based on the state information. Specifically, this implementation method constitutes the core closed loop of Occlusion Coupled Trajectory Maintenance (OCTM). Unlike conventional SORT trackers, which only enter a "loss queue" and passively wait after the target is completely lost, this embodiment introduces an intermediate state of "awaiting occlusion state," modeling the occlusion process as a perceptible gradual event rather than an instantaneous binary state. When a significant drop in confidence of a track is detected, but not yet a complete mismatch, the system determines that the pig may be in a transitional phase, being squeezed by other individuals, turning around, or entering a visual dead zone. At this point, it immediately locks the center of the last reliable bounding box before occlusion, smooth appearance features, and the starting frame number as state anchors. This proactive perception mechanism allows the tracker to retain identity information as soon as visual evidence of the target weakens, rather than waiting until a complete match fails before attempting backtracking. More importantly, the reconnection operation is strictly performed before the initialization of new tracks. This means that when an unmatched detection result appears in the image, it is only allowed to be identified as a new individual after ruling out the possibility of reconnecting all occluded tracks. This priority strategy fundamentally suppresses identity splitting caused by brief occlusion, ensuring that the same pig maintains a unique trackid even after multiple occlusion re-enactments, thus guaranteeing the continuity of identity in spatiotemporal segment construction.

[0051] As one implementation method, the confidence reference value is maintained as follows: after each successful association, it is selectively updated based on the proximity of the current detection confidence to the existing reference level; the confidence reference value is only updated based on the current detection confidence when the difference between the current frame's detection confidence and the existing reference level is less than a set value. Specifically, this selective update strategy is the cornerstone of the stable operation of the OCTM mechanism. To detect potential occlusion in advance before the target completely mismatches, this embodiment maintains a stable confidence baseline for each trajectory, and its update rule can be formally expressed as: , in, This represents the detection confidence of trajectory i after successful association in frame t. This represents the stable confidence baseline inherited from the previous frame. This represents the updated baseline. The update rule adjusts the baseline only if the current confidence level remains close to the existing stable level; otherwise, it remains unchanged. This avoids short-term fluctuations caused by attitude changes, local overlap, or boundary blurring that could rapidly lower the reference value. Based on this, the occlusion indicator variable is defined as: , in, ∈{0,1} represents the occlusion status flag, and δ is the confidence drop threshold that controls the sensitivity of occlusion detection. When this condition is triggered, the system marks the trajectory as occluded and retains its anchor point information before occlusion, including the last reliable bounding box, smooth appearance features, and the occlusion start frame, for use in subsequent priority reconnection for occlusion. In this embodiment, δ is uniformly set to 0.3. In actual pig house monitoring scenarios, the sources of fluctuation in detection confidence are complex. In addition to actual occlusion, rapid turning of pigs, large limb swings, sudden changes in lighting, or local overlap can all cause the confidence output of the detector to drop instantaneously. If traditional methods such as exponential moving average are used to update the confidence baseline every frame, these short-term fluctuations caused by non-occlusion will quickly lower the reference value, resulting in baseline contamination. Once the baseline is abnormally lowered, when the pig subsequently encounters severe occlusion, the decrease in the current confidence relative to the lowered baseline may not trigger the preset threshold, resulting in missed detection. This embodiment constructs a stability filter by setting a difference threshold: the baseline is only allowed to be updated when the current confidence level is sufficiently close to the historical baseline (indicating stable detection quality or only a gradual change); while the baseline remains unchanged when the confidence level jumps drastically, thus preserving the true confidence level of the pig under normal visibility conditions. This design ensures that the occlusion discrimination threshold is always anchored to the pig's "health status," effectively distinguishing between posture interference and true occlusion, and improving the robustness of occlusion perception.

[0052] As one implementation method, the multi-target tracker categorizes detection boxes into high-score and low-score groups based on their confidence levels. High-score detection boxes first have their re-identified appearance features extracted, then form a candidate pool with the currently active and lost trajectories. A fusion matrix of the intersection-union distance (IoU) and feature embedding distance (IoU) is calculated, and the first round of association matching is completed using the Hungarian algorithm. The remaining unmatched high-score trajectories undergo a second round of IoU matching with the low-score detection boxes. Specifically, this hierarchical association strategy is a targeted adaptation to the heterogeneity of detection quality in pig farm scenarios. At the tracker's entry point, detection boxes with a confidence level below 0.25 are first filtered out, and then categorized into the two groups based on their confidence level to enter the subsequent association process. High-score detection boxes typically correspond to pig individuals with clear poses and minimal occlusion; their appearance features have high discriminative power. Therefore, using a dual metric of ReID (Person Re-identification) features and IoU for accurate matching maximizes the use of appearance information to distinguish similar neighboring individuals, ensuring the accuracy of the main tracking link. Low-scoring detection boxes often originate from severe occlusion, motion blur, or edge truncation. In these cases, the extracted appearance features are not only unreliable but may even introduce misleading noise. Forcing ReID matching on low-scoring boxes can easily lead to incorrect associations. Therefore, this embodiment uses only geometric position-based IoU matching as a remedial measure for low-scoring boxes. This avoids interference from inferior appearance features while utilizing the potentially valid positional information still contained within the low-scoring boxes to recover trajectories on the verge of being lost due to decreased detection quality. The cascaded design of the two-round matching significantly improves the recall capability for difficult samples while ensuring high-confidence target matching accuracy, enabling the tracker to maintain high trajectory integrity even in crowded interactive scenarios.

[0053] Furthermore, in the aforementioned reconnection process based on state information, a dynamic search radius mechanism is employed in this embodiment to accommodate potential displacements of the pigs during occlusion. Specifically, for a trajectory in an occluded state, the normalized center distance threshold between it and the unmatched detection result is not a fixed value, but rather adaptively increases with the duration of occlusion Δt. For the occluded trajectory i and the unmatched detection d, the normalized center distance is defined as: , Let i be the center of the last reliable bounding box before trajectory i is occluded. For testing The center, and Let represent the width and height of the bounding box, respectively. Here, the center distance of the anchor points before occlusion is used, rather than solely relying on IoU (Intersection over Union), because in high-density interaction scenarios, motion prediction tends to drift gradually after the trajectory enters occlusion. To allow the candidate region to adaptively expand with the duration of occlusion, the dynamic search radius R can be determined from the initial search radius. is determined together with the growth coefficient k, satisfying the relational expression , where Δt represents the duration since entering the occluded state. A detection d is regarded as a valid reconnection candidate only when dist(i,d)<R. Among all valid candidates, the one with the smallest normalized center distance is selected to resume the trajectory and retain its original identity number. The physical significance of this design lies in that pigs usually do not stay still the moment they are occluded, and the longer the occlusion time, the larger the spatial range they may move to. If a fixed small radius is always used for searching, pigs that reappear after long-term occlusion cannot be reconnected because their position offset exceeds the search window; if a large radius is always used for searching, it is easy to incorrectly match other adjacent pigs in a dense group. By decoupling the search radius from the occlusion duration and establishing a linear or nonlinear growth relationship, the system can maintain strict geometric constraints in the early stage of occlusion to prevent mismatching, and gradually relax the constraints in the later stage of occlusion to tolerate greater displacement uncertainty, thereby achieving a dynamic balance between accuracy and recall rate. It should be understood that although the present embodiment gives an example of linear growth, in other embodiments, stepwise growth, exponential decay or other functional forms that can characterize the accumulation of displacement uncertainty over time may also be adopted, as long as the adaptive adjustment of the search range with the occlusion duration can be realized.

[0054] The present embodiment provides a specific application example of the above method for quantifying the duration of aggressive behavior of pigs in the aggressive behavior monitoring scenario of large-scale pig farms, aiming to verify the implementation capability and business value of the technical solutions constructed in the foregoing embodiments in the actual production environment. In this application scenario, the system is connected to the surveillance cameras deployed inside the pigsty, and collects video streams in real time at a frame rate of 30fps as input. First, the YOLO12 detector is used to locate individual pigs in each frame of image, and output detection frames and confidence levels; then, the detection results are input into the FTO-SORT-OCTM tracker, and continuous motion trajectories across frames and unique identity identifiers (trackid) are generated through the occlusion-coupled trajectory maintenance mechanism, which effectively copes with the problems of frequent dense occlusion and viewing angle dead zones in the group rearing environment. On this basis, the system performs RoIAlign alignment sampling on the image region sequence corresponding to each trajectory to construct a spatio-temporal segment of fixed size, and inputs it into the P2P-SlowFast network for individual-level behavior recognition, and finally outputs structured aggressive behavior statistics results through the event-level quantization module.

[0055] Specifically, to adapt to the sparse participation and strong context-dependent characteristics of pig aggression behavior in long-term monitoring streams, this application adopted targeted adaptation strategies in the model training and data preparation stages. For the detection model training, approximately 5000 training images and 700 evaluation images were used, and the dataset was divided at the video segment level to avoid information leakage. During training, data augmentation techniques such as image rotation and random occlusion were applied. Automatic optimization was performed using the SGD optimizer with a batch size of 10, a total training duration of 200 epochs, an initial learning rate of 0.01 for the backbone network, and an SGD momentum parameter of 0.937. To avoid overfitting and improve training efficiency, a 30-epoch early stopping mechanism was implemented during training; the training process automatically terminated when the validation set accuracy did not show significant improvement within 30 consecutive epochs. For the behavior recognition model training, considering the long-tail characteristics of attack events in their temporal distribution and the cost constraints of annotation, an AVA-style keyframe annotation paradigm was adopted: keyframes were sampled at a frequency of 1Hz, and the action category was annotated for the individual's region of interest within each keyframe and aligned with the identity marker. To alleviate the problem of scarce positive samples, candidate segments for locating fights were first labeled using the Kinetics (behavioral dataset) style. These segments were then combined with the SlowFast model weights obtained through transfer learning and the tracking results to generate pseudo-labels. After manual review and cleaning, a training set containing 24,336 normal samples and 7,951 attack samples, and a validation set containing 2,942 normal samples and 1,008 attack samples were finally constructed. This data construction method allows the model to fully learn the group interaction patterns and contextual cues during attack behaviors, thereby accurately identifying real attack events from visually highly similar normal social behaviors (such as approaching and squeezing).

[0056] For tracking performance verification, this application selected seven representative test videos, each approximately one minute long. These videos all contained typical challenging factors such as occlusion and blind spots, and underwent frame-by-frame fine-grained annotation to generate realistic trajectory files. Experimental results show that the FTO-SORT-OCTM tracker significantly reduced the identity switching error rate compared to the baseline method, ensuring identity consistency in subsequent spatiotemporal segment construction. At the end-to-end output level, the system ultimately generates two core products: first, a structured event table for livestock managers, with each row containing the video number, pig identification, attack start time, attack end time, and duration, supporting traceability of historical attack files by individual; second, a summary JSON evaluation report for algorithm optimization personnel, containing metrics such as the overall time intersection-union ratio, the number of successfully matched events, the number of missed and false alarm events, and the mean absolute error of start time, end time, and duration. This application example demonstrates that the technical solution provided in this application can reliably convert video streams into individual-level quantitative indicators in real-world group-raising scenarios. It effectively solves the problems of difficulty in individual attribution and inaccurate duration statistics mentioned in the background art, providing practical technical support for pig welfare monitoring and precision feeding management. It should be understood that the specific detector models, frame rate values, sample sizes, and threshold parameters mentioned in the above application are merely illustrative examples. In actual deployment, they can be adaptively adjusted according to hardware computing power, pig farm size, and management needs. As long as the unified processing framework of detection-tracking-identification-quantification established in this application is followed, they should all be covered within the scope of protection of this application.

[0057] The above inventions are merely a few specific embodiments of the present invention. However, the embodiments of the present invention are not limited thereto, and any variations that can be conceived by those skilled in the art should fall within the protection scope of the present invention.

Claims

1. A method for quantifying the duration of aggressive behavior in pigs, characterized in that, Includes the following steps: Each individual pig in the surveillance video is identified and assigned an identity tag. The movement trajectory of each individual pig is generated based on the identity tag. Then, a spatiotemporal segment of the individual pig corresponding to each identity tag is constructed based on the movement trajectory. The spatiotemporal segment is input into a P2P-SlowFast network to obtain the behavior judgment result of each pig individual corresponding to each identity in each frame; the behavior judgment result includes the identity and its corresponding attack behavior category. The P2P-SlowFast network comprises a cascaded SlowFast dual-path backbone network, a context module, a P2P relationship reasoning module, and a classification head. The SlowFast dual-path backbone network extracts and fuses multi-scale spatiotemporal features of the spatiotemporal segments to form a shared feature representation. The context construction module extracts and compresses a fixed number of context tags from the shared feature representation. The subject tags extracted from the shared feature representation by the P2P relationship reasoning module are used as queries. Using the context tags as keys and values, a multi-head attention mechanism is employed to retrieve interaction cues related to the current individual. These cues are then integrated into the subject tags using residual connections, resulting in an enhanced subject tag output. The enhanced subject markers are mapped to probability distributions of attack behavior categories and non-attack behavior categories using a classification head, and combined with the time dimension information in the shared feature representation, the behavior judgment result and its corresponding attack behavior duration are output.

2. The method for quantifying the duration of aggressive behavior in pigs as described in claim 1, characterized in that, The contextual tags are constructed by performing temporal mean pooling and spatial adaptive pooling on the shared feature representations in sequence to obtain a fixed number of contextual tags.

3. The method for quantifying the duration of aggressive behavior in pigs as described in claim 1, characterized in that, The enhanced subject marker is input into the classifier, and the output behavior judgment result is as follows: The enhanced subject markers are input into a fully connected layer and mapped to the dimension of the number of categories. The probability distributions of attack behavior categories and non-attack behavior categories are calculated using the Softmax function; The category with the highest probability is taken as the behavioral judgment result of the pig individual corresponding to the spatiotemporal segment.

4. The method for quantifying the duration of aggressive behavior in pigs as described in claim 1, characterized in that, The P2P relationship reasoning module is configured as follows: The main tag and the context tag are used as input; The main tag and the context tag are respectively mapped to a unified feature space through a linear projection layer to generate queries, keys, and values; The query and key are input into the multi-head attention layer, the attention weights are calculated and normalized by Softmax to obtain the attention distribution, the values ​​are weighted and aggregated according to the attention distribution, and the interaction cues related to the current individual are retrieved; wherein, when the multi-head attention layer calculates the attention weights, it injects the geometric prior bias generated by the normalized bounding box of the current individual through a lightweight multilayer perceptron. The retrieved interaction cues are multiplied by an adjustable coefficient and then superimposed onto the original subject markers using a residual connection method to obtain intermediate features; The intermediate features are sequentially input into the layer normalization layer, the feedforward network layer, and the second layer normalization layer to obtain the enhanced subject label.

5. The method for quantifying the duration of aggressive behavior in pigs as described in claim 1, characterized in that, The output behavior judgment result and its corresponding attack behavior duration are as follows: Using trajectory numbers as the unit, the judgment results of consecutively identified attack behaviors are aggregated into one or more independent attack event segments according to the continuity of time; For each attack event segment, obtain the timestamp corresponding to its start frame as the attack start time, and obtain the timestamp corresponding to its end frame as the attack end time. The difference between the attack end time and the attack start time is calculated as the duration of the individual in the attack event segment. If there are multiple attack event segments under the same identity, output the duration of each attack event segment separately, or output the sum of the durations of all attack event segments.

6. The method for quantifying the duration of aggressive behavior in pigs as described in claim 5, characterized in that, Before outputting the behavior judgment result and its corresponding attack behavior duration, the method further includes a step of optimizing the behavior judgment result that is continuously judged as an attack behavior: removing discrete segments with a duration shorter than a first set threshold, and merging adjacent segments of the same type with a time interval less than a second set threshold; the first set threshold is less than the second set threshold.

7. The method for quantifying the duration of aggressive behavior in pigs as described in claim 5, characterized in that, After determining the duration of an individual's participation in the attack, the process also includes an event-level quantitative evaluation step: using trajectory numbers as units, the post-processed continuous attack frame sequences are aggregated into independent attack events. The start time, end time, and duration of each event are calculated. Based on real annotations, the whole-track time intersection-union ratio, the number of successfully matched events, the number of missed events, the number of false alarm events, and the mean absolute error of the start time, end time, and duration of the attack events under each identity are calculated. Finally, a summary JSON output containing the average indicators of all individuals is generated.

8. The method for quantifying the duration of aggressive behavior in pigs as described in claim 1, characterized in that, The process of assigning an identity tag to each individual pig in the surveillance video, generating a motion trajectory for each individual pig based on the identity tag, and then constructing a spatiotemporal segment for each individual pig corresponding to the identity tag based on the motion trajectory includes: inputting the surveillance video frame by frame into a multi-target tracker; the multi-target tracker performing cross-frame association matching on the individual pigs in each frame image to generate a corresponding identity tag for each individual pig, and generating a motion trajectory for each individual pig based on the identity tag; Based on the identity identifier, the corresponding movement trajectory is determined. For each individual pig under the identity identifier, based on the movement trajectory and using the detection box of the latest frame as a reference, the image region of the corresponding individual pig is cropped from the historical buffer frame. The cropped regions of multiple consecutive frames are stacked along the time dimension to construct a spatiotemporal segment bound to the identity identifier.

9. The method for quantifying the duration of aggressive behavior in pigs as described in claim 8, characterized in that, During the tracking process, the multi-target tracker maintains a confidence baseline for each motion trajectory. The confidence baseline represents the detection confidence level of the pig individual corresponding to the trajectory under normal visibility conditions. When the detection confidence of the trajectory in the current frame decreases by more than a set range relative to the confidence baseline, the trajectory is marked as to be occluded, and the state information before occlusion is recorded. For detection results that are not successfully associated in the current frame, they are reconnected with trajectories in a state of pending occlusion based on the state information; If the reconnection is successful, the trajectory will be restored and its original identity will be maintained; If reconnection fails, a new identity and a new motion trajectory will be created for the detection result.

10. The method for quantifying the duration of aggressive behavior in pigs as described in claim 8, characterized in that, The multi-target tracker divides detected pig individuals into high-score and low-score groups based on their confidence level. For individuals in the high-score group, re-identification appearance features are first extracted, and then a candidate pool is formed with the current active trajectory and the lost trajectory. The fusion matrix of the intersection-union distance and the feature embedding distance is calculated, and the first round of association matching is completed through the Hungarian algorithm. The remaining unmatched high-score trajectories are matched with low-score individuals in the second round of intersection-union matching. The confidence baseline is maintained as follows: after each successful association, it is selectively updated based on the proximity of the current detection confidence to the existing baseline level; the confidence baseline is only updated based on the current detection confidence when the difference between the current frame's detection confidence and the existing baseline level is less than a set value.