High-precision traffic sign detection plays an important role in enhancing road traffic safety, ensuring traffic smoothness, supporting the development of intelligent transport systems and promoting the standardization and normalization of traffic facilities. To overcome the limitations of traditional methods, this study proposes an enhanced YOLOv5 algorithm for complex road environments. First, the K-Means clustering algorithm is used to cluster and analyze the boundary boxes of traffic signs in the training dataset, obtaining anchor box sizes that are closer to the distribution of the dataset to improve detection accuracy. Then, a genetic algorithm was used to further optimize the initial anchor box, and a global search strategy was used to find the optimal anchor box configuration, further improving detection performance. In terms of model structure, a bidirectional feature pyramid network (Bi-FPN) is introduced, which effectively utilizes multi-scale feature information and enhances the adaptability of the model to traffic signs of different sizes through top-down and bottom-up feature fusion paths, as well as cross-scale connections. In addition, a global attention mechanism (GAM) was introduced to recalibrate the feature maps through channel attention and spatial attention dimensions, improving the robustness and detection accuracy of the model for complex environments. Finally, a loss function called Focal-EIoU was used to solve the problems of class imbalance and sample imbalance, which improved the stability and performance of the object detection model. Experiments on a Chinese traffic sign dataset demonstrate significant improvements, with an increase in mAP by 10.03%, precision by 4.7%, recall by 2.6% and F1-score by 3.48%, respectively. The results prove the validity of the proposed traffic sign recognition method, especially in complex road environments. This study provides new ideas and methods for the field of traffic sign detection, which has important theoretical significance and application value.