Traditional vision methods for solar cell defect detection have problems such as low accuracy and few types of detection, so this paper proposes an optimized YOLOv5 model for more accurate and comprehensive identification of defects in solar cells. The model firstly integrates five data enhancement methods, namely Mosaic, Mixup, hsv transform, scale transform and flip, to expand the existing data set to improve the feature training accuracy and enhance the robustness of the model; secondly, CA attention mechanism is introduced to improve the feature extraction ability of the model; to address the problems of different target defect classification and localization concerns, the detection head in the original model is replaced with a decoupling head, which significantly improve the detection accuracy of the model without affecting the convergence speed of the model. The results show that the optimized model achieves an mAP of 96.1% on the publicly available dichotomous ELPV dataset, and can identify and locate a variety of common defects in the PVEL-AD dataset, while the mAP can reach 87.4%, an improvement of 10.38% compared with the original YOLOv5 model, which enables the model to perform more accurately while ensuring the real-time requirement of solar cell surface defects detection task.