Abstract:Uniqueness-based identity authentication is crucial for the implementation of agricultural insurance in dairy farms. However, there is currently no accurate and reliable method for cow identification, leading to incidents of insurance fraud and difficulties in coverage. To address this issue, this paper proposes a cross-attention mechanism and an adaptive loss function, built upon the YOLOv7 model framework, to detect cows in the complex environments of dairy farms. The cross-attention mechanism extracts correlation information from different directions in the images, integrating both deep and shallow features to adapt to scale variations caused by poor lighting conditions and shooting angles in farm settings. To tackle the inconsistency in image quality across the dataset, the adaptive loss function adjusts the weights of easy and hard samples, enabling the model to focus more on challenging samples during training, thereby enhancing the robustness and generalization performance of the detection model. Experimental results indicate that the proposed cross-attention mechanism and adaptive loss function model achieved an accuracy rate of 94.63% in the task of dairy cow detection and recognition, which is an improvement of 11.42% compared to the original YOLOv7 model.