The detection of pavement diseases is an important and basic link in the road maintenance process. Many methods based on deep learning have been applied. However, these methods are not accurate enough and cannot accurately identify defects in complex background with shadow occlusion and uneven lighting brightness. In order to overcome the shortcomings of previous detection methods, a complex background defect detection algorithm based on improved YOLOv7 is proposed. First, the K-means++ clustering algorithm is used for initial anchor box setting to obtain better anchor box parameters; then, the group spatial pyramid pooling module SPPCSPC_G is introduced to replace the original SPPCSPC module to improve the fusion speed of image features and thereby improve the detection accuracy; Finally, the GELU activation function is used as the activation function of the REPConv convolution module in the YOLOv7 model, which effectively reduces model overfitting and thereby improves model detection accuracy. The test results show that the average accuracy of the improved detection algorithm for disease detection increased from 65.4% to 72.3%, an increase of 6.9%, the amount of calculation and parameters decreased by 4% and 14.9% respectively, and the FPS reached 80, an increase of 17%, and no pavement defects are missed or wrongly detected. It is more suitable for real-time detection of defects in complex background. It can be seen that the improved YOLOv7 has better detection effect on complex background defects.