Lithium batteries are widely used in new energy vehicles and electronic equipment. Aiming at the typical defects that are easy to occur in the production process of lithium batteries, this paper improves the performance and recognition accuracy of the algorithm by integrating void convolution and attention mechanism into the YOLOv5 basic framework. At the same time, whale algorithm is used to automatically optimize the algorithm parameters in the process of optimization. Finally, through simulation experiments. This method realizes the rapid and accurate identification of lithium battery defects in the rapid production process of automatic production line.