Safety helmet wearing detection has become particularly important in work sites such as construction, steel and mining. However, the complex environment of the work sites, with numerous equipment, dense personnel, and insufficient lighting, poses a challenge to safety helmet detection. To solve these issues, a target detection algorithm based on improved YOLOv5s is proposed. The BiFormer attention mechanism is added to the neck layer of YOLOv5s to reduce the computational burden by utilizing the Bi-Level Routing Attention (BRA) mechanism, which implements dynamic querying through sparse matrices and focuses on the key information. By introducing the BiFormer attention mechanism, the model can capture key features in images, especially under low-light and high-reflection conditions, enhancing the recognition capability of safety helmet features. Additionally, the introduction of Wise-IoU optimizes the performance evaluation of the model for detecting targets of varying sizes and complexities through a weighted intersection-over-union approach. Experimental results show that the improved YOLOv5s enhances detection accuracy and speed, particularly excelling in the detection of small and overlapping targets. Compared to the YOLOv5s model, the improved model achieved an improvement of 4.4% in accuracy, 2.3% in recall, and 14FPS in detection speed by. Additionally, it can be seen from the experimental transformation curves that mAP@0.5 and mAP@0.5:0.95 have increased significantly. Tests in real-world scenarios validate the practicality and robustness of the algorithm.