Livestock counting and identification algorithm is a key technology for livestock management and development. However, traditional methods rely on manual or RFID tags, which are time-consuming, costly and prone to errors. This paper proposes a livestock counting and identification algorithm based on YOLOv5, which solves the problems of high workload and low accuracy of yaks identification for herdsmen. The system can capture the images or videos of livestock on the pasture, and input them into the YOLOv5 algorithm model. The algorithm network analyzes and processes the images through four parts: input layer, backbone network, neck network, and detection layer. Finally, it detects and distinguishes the desired targets. The system can solve the problems of inaccurate counting, difficult identification and possible loss of livestock in winter or at night. The system can also help herdsmen manage their livestock more conveniently and efficiently, improve their quality of life, and promote the economic development of the region.