# YoloSortC3D: A YOLOv8, DeepSort, and C3D-Based Method for Sheep Rumination Behavior Recognition
## 2 Materials and Methods
### Calculation of Rumination Time for Sheep  
This study achieves the precise calculation of sheep rumination time by integrating a sliding window approach with the frame difference method, enhancing the system's detection capabilities. Specifically, each window comprises 32 frames and slides forward with a defined step size, generating a series of overlapping frame sequences. This ensures that each behavior segment is detected multiple times, reducing the likelihood of missed detections.  
Figure 8: Comparison of model training accuracy and loss curve. (**a**) C3D model training accuracy line variation (**b**) R(2+1)D model training accuracy line variation (**c**) R3D model training accuracy line variation (**d**) C3D model training loss line variation (**e**) R(2+1)D model training loss line variation (**f**) R3D model training loss line variation.  
The frame difference method captures subtle motion changes in rumination by calculating pixel differences between consecutive frames, providing more accurate behavior detection and timing functionality. After collecting images using YOLOv8 and DeepSORT for continuous tracking, the system retains only the current frame if the interval between the current and previous frames exceeds three frames, clearing earlier frames to ensure real-time tracking accuracy. This practice guarantees that the input frame sequences remain continuous and accurate, preventing misjudgments caused by sequence interruptions.  
When the condition of 32 frames is met, the model assesses whether the sheep is engaging in rumination behavior. If rumination is detected, it is labeled as \"chew\"; if not, it is labeled as \"normal\". To maintain continuous and real-time behavior judgment, new image frames are continually collected, with the oldest frame being deleted to ensure that 32 frames are always available for assessment. Upon detecting rumination behavior, the current frame index is recorded as the start time. If a start time already exists, the difference between the current frame index and the start time is calculated and divided by the frame rate, as shown in Equation (1), to determine the duration of the current rumination episode.  
\[\mathrm{T_{r}}=\frac{\mathrm{F_{cur}-F_{start}}}{\mathrm{FPS}} \tag{1}\]  
In the equations, \(\mathrm{T_{r}}\) represents the duration of rumination, \(\mathrm{F_{cur}}\) refers to the frame number of the current frame, \(\mathrm{F_{start}}\) denotes the frame number of the starting frame, and FPS indicates the frame rate.  
For timing the intervals of normal behavior, when the sheep is detected in a non-rumination state, if a starting time for rumination has been previously recorded, the difference between the current frame index and the starting frame index is calculated and divided by the frame rate, as shown in Equation (2). This calculation yields the current interval time, after which the starting time is cleared to prepare for the next timing session, allowing subsequent interval times to be accumulated.  
\[\mathrm{T_{int}}=\frac{\mathrm{F_{cur}-F_{start}}}{\mathrm{FPS}} \tag{2}\]  
In the equation, \(\mathrm{T_{int}}\) represents the duration of the interval, \(\mathrm{F_{cur}}\) refers to the frame number of the current frame, \(\mathrm{F_{start}}\) denotes the frame number of the starting frame, and FPS indicates the frame rate.  
Ultimately, the rumination time and the interval time for each sheep are summed to obtain the total behavior time, as shown in Equation (3).  
\[\mathrm{T_{total}}=\mathrm{T_{r}}+\mathrm{T_{int}} \tag{3}\]