Abstract:In modern pig farming, the detection and counting of piglets presents a significant challenge due to their small size, high mobility, tendency to be occluded, and habit of clustering. Manual counting is not only time-consuming and labor-intensive but also prone to errors. To address these issues, a novel piglet detection algorithm, GSD-YOLO, was proposed based on YOLO v8n, which was designed to provide fast and accurate detection. The algorithm tackled the inherent difficulty of detecting piglets by introducing flexible nonmaximum suppression (FNMS) for bounding box intersection handling and employing the Inner-MPDIoU loss function to optimize the bounding box regression mechanism. Additionally, a coordinate attention (CA) mechanism was integrated to enhance the feature representation of target areas, effectively resolving issues related to long-range dependencies. For efficient deployment on embedded devices, the model was further optimized for lightweight performance. Specifically, the backbone and neck components of the original model were replaced with the GhostNet module, which reduced both model parameters and feature redundancy in the channels. Furthermore, a lightweight detection head, Detect_DG, was introduced, which reduced the overall model size by 18.48% while simultaneously improving detection accuracy. Compared with YOLO v8n, GSD-YOLO achieved improvements of 1.0 and 0.6 percentage points in F1-score and average precision (AP), respectively, while reducing the model size by 61.28% and improving the frame rate by 12.5%. In comprehensive detection performance, GSD-YOLO outperformed four widely used models, including YOLO v5. Experimental results demonstrated that the proposed model can rapidly and accurately detect piglets under challenging conditions, such as occlusion, overlap, and varying lighting environments. Moreover, with a compact memory footprint of only 2.6 MB. The deployment of GSD-YOLO on edge computing devices such as the Jetson Orin NX and Android mobile devices provided effective technological support for piglet counting and detection in practical applications.