Pig counting is an important task in pig sales and breeding supervision. Currently, manual counting is low-efficiency and high-cost and presents challenges in terms of statistical analysis. In response to the difficulties faced in pig part feature detection, the loss of tracking due to rapid movement, and the large counting deviation in pig video tracking and counting research, this paper proposes an improved pig counting algorithm (Mobile Pig Counting Algorithm with YOLOv5xpig and DeepSORTPig (MPC-YD)) based on YOLOv5 + DeepSORT model. The algorithm improves the detection rate of pig body parts by adding two different sizes of SPP networks and using SoftPool instead of MaxPool operations in YOLOv5x. In addition, the algorithm includes a pig reidentification network, a pig-tracking method based on spatial state correction, and a pig counting method based on frame number judgment on the DeepSORT algorithm to improve pig tracking accuracy. Experimental analysis shows that the MPC-YD algorithm achieves an average precision of 99.24% in pig object detection and an accuracy of 85.32% in multitarget pig tracking. In the aisle environment of the slaughterhouse, the MPC-YD algorithm achieves a correlation coefficient (R2) of 98.14% in pig counting from video, and it achieves stable pig counting in a breeding environment. The algorithm has a wide range of application prospects.
Sow farrowing is a critical stage in pig farming, and predicting its onset can improve sow health and piglet survival. This study proposes a method based on optical flow estimation and time-series forecasting to predict sow farrowing onset. The RAFT optical flow algorithm is applied to visible-light videos of sows in late pregnancy to extract activity levels and generate time-series data. The reliability of the activity extraction algorithm is validated through correlation and trend analysis, showing strong inter-sow correlations with an average Pearson coefficient of 0.819. An in-depth analysis of sow_16 reveals a significant increase in activity 24 h before farrowing. Using these data, the CLA-PTNet model, incorporating CNN, LSTM, and attention mechanisms, is developed for continuous farrowing onset prediction. Experimental results demonstrate high predictive accuracy, with average MAE, RMSE, and R2 values of 5.42 min, 5.97 min, and 0.99, respectively, across four test sows. The method effectively captures activity pattern changes before farrowing, enabling precise predictions. This study offers an innovative, non-invasive solution for predicting sow farrowing onset, with significant application potential in farming practices.