In intensive farming, contactless individual pig identification is crucial for precision feeding and health monitoring. However, real-world barn conditions—such as fluctuating illumination, severe occlusions, non-rigid poses, and high inter-individual similarity—pose significant challenges. Existing models struggle to balance high accuracy with lightweight deployment. To address this, we propose YOLO-ESO, an optimized detection framework based on YOLOv10n. YOLO-ESO introduces three core innovations: (1) integrating the C2f_ODConv module into the backbone to strengthen feature learning under complex poses via dynamic convolution; (2) redesigning the neck with a Semantics and Detail Infusion (SDI) module to improve multi-scale fusion while suppressing background noise; and (3) embedding an Efficient Multi-Scale Attention (EMA) mechanism before the detection head to capture fine-grained identity cues like texture and contours. Evaluated on a real-world pig dataset, YOLO-ESO achieves an mAP@0.5 of 96.6%, an mAP@0.5:0.95 of 71.1%, and an F1 of 92.0%. YOLO-ESO surpasses state-of-the-art detectors including YOLOv8, YOLOv11, and RT-DETR, while introducing only 8.7 GFLOPs and 3.48 million parameters. Overall, the proposed YOLO-ESO provides an accurate and lightweight solution for robust individual pig identification in complex farming environments, showing strong potential for practical deployment in precision livestock farming.