# YoloSortC3D: A YOLOv8, DeepSort, and C3D-Based Method for Sheep Rumination Behavior Recognition
## 2 Materials and Methods
### Accurate Tracking of Sheep Head  
Rumination behavior in sheep is primarily manifested through jaw movements, necessitating the precise tracking of the sheep's head prior to behavior recognition. The DeepSORT algorithm enhances tracking accuracy and robustness by integrating deep learning techniques, including deep feature extraction, Kalman filtering, and the Hungarian algorithm. This approach significantly improves target recognition and data association in complex scenarios involving variations in perspective, lighting, and occlusion.  
This experiment employed a combination of YOLOv8 and DeepSORT [21, 22, 23] to track the sheep's head in the video. The flowchart of the multitarget tracking algorithm is illustrated in Figure 4, which primarily encompasses the following steps.  
1. The input video is first converted into individual frames using a video processing library, followed by the application of the YOLOv8 object detection algorithm to analyze the frames. Depth features of the target objects are extracted, and similar detection boxes are eliminated using the non-maximum suppression (NMS) algorithm to obtain the final detection results.
2. The Kalman filter algorithm is employed to track the position and state of objects across consecutive video frames. The detection boxes provided by the object detector are compared with the predicted results from the Kalman filter model to achieve the most accurate predictions. During this process, both the detection boxes and the tracking model are continuously updated with the latest data, improving prediction accuracy.
3. Targets in the previous and current frames are matched to generate possible matching pairs, and the matching cost is calculated. This cost is determined using appearance information, Mahalanobis distance, or intersection over union (IoU). The Hungarian algorithm is then applied to find the optimal matching that minimizes the overall cost, ensuring the efficient tracking of moving objects.
4. Output the tracking results, update the parameters of the tracker, and restart object detection.  
Figure 5 illustrates the training performance of the DeepSORT re-identification module on the sheep head re-identification dataset. The horizontal axis represents the number of training epochs; the left vertical axis corresponds to the loss value, while the right vertical axis denotes the Top-1 error rate (top1 err). The blue lines and dots indicate the performance metrics on the training set; whereas, the red lines and dots correspond to the validation set. As training progresses, both the loss value and error rate decrease significantly and gradually stabilize, indicating that the model demonstrates high effectiveness in handling  
Figure 4: Multitarget sheep head algorithm tracking flowchart.  
ID switching issues caused by target disappearance or occlusion. Experimental results demonstrate that this method performs strongly in sheep head tracking tasks. In Video 3, the sheep labeled as ID 1 was accurately tracked during brief movements and head shaking actions, consistently maintaining the same ID. Figure 6 illustrates the head tracking performance across several frames during head turning movements.