Abstract:Manual teat-cup-attachment is labor-intensive and has low production efficiency, while automatic attachment involves higher costs, posing a certain economic burden for small and medium-sized dairy farms. To meet the needs of these farms and address challenges such as the complex milking environment and varying teat morphologies in rapid and accurate automatic teat-cup-attachment for milking machines, a novel binocular vision positioning method was proposed based on an improved YOLO v8n. This method enabled accurate detection and rapid localization of cow teats. The CSPDarknet backbone network of YOLO v8n was replaced with the lightweight FasterNet backbone network, a P2 feature layer was constructed in the neck network, and an EMA attention mechanism was added to the Detect and C2f sections, enhancing the model’s detection accuracy and speed for cow teats. The RANSAC algorithm was employed to optimize SURF feature points, reducing mismatches caused by the similarity between teats and udders. Based on the improved YOLO v8n model, binocular vision was used to acquire three-dimensional spatial information of cow teats. Ablation and comparative experiments conducted on a self-built dataset showed that the improved YOLO v8n model achieved a mAP@0.5 of 98.62%, a precision of 97.23%, and a recall of 96.69%, representing improvements of 2.17, 3.28, and 3.65 percentage points, respectively, compared with that of the original YOLO v8n model. The parameter count was reduced to approximately half, and the frame rate was increased by a factor of 2.31, significantly enhancing the model’s detection performance. Statistical analysis revealed that the average absolute deviation of teat positioning was 0.0117m, with a variance of 0.0001m2 and a standard deviation of 0.0119m, meeting the requirements for the next stage of automatic teat cup attachment in milking machines.