For the problem of low resolution of camera module lens surface defect image, small target and blurred defect details leading to low detection accuracy, a camera module lens surface defect detection algorithm YOLOv5s-Defect based on improved YOLOv5s is proposed. Firstly, to solve the problems arising from the anchor frame generated by the network through K-means clustering, the dynamic anchor frame structure DAFS is introduced in the input stage. Secondly, the SPP-D (Spatial Pyramid Pooling-Defect) improved from the SPP module is proposed. The SPP-D module is used to enhance the reuse rate of feature information in order to reduce the loss of feature information due to the maximum pooling of SPP modules. Then, the convolutional attention module is introduced to the network model of YOLOv5s, which is used to enhance the defective region features and suppress the background region features, thus improving the detection accuracy of small targets. Finally, the post-processing method of non-extreme value suppression is improved, and the improved method DIoU-NMS improves the detection accuracy of small targets in complex backgrounds. The experimental results show that the mean average precision mAP@0.5 of the YOLOv5s-Small-Target algorithm is 99.6%, 8.1% higher than that of the original YOLOv5s algorithm, the detection speed FPS is 80 f/s, and the model size is 18.7M. Compared with the traditional camera module lens surface defect detection methods, YOLOv5s-Small-Target can detect the type and location of lens surface defects more accurately and quickly, and has a smaller model volume, which is convenient for deployment in mobile terminals, meeting the demand for real-time and accuracy of camera module lens surface defect detection.