Aiming at the problems of complex background, serious occlusion and variable scale of X-ray security inspection images in dangerous goods detection, the YOLOv7 algorithm is improved, which improves the detection accuracy and makes the network more lightweight. Firstly, the PS-ELAN module is built to replace the ELAN module in the original backbone network, which reduces the network computing amount and memory occupation, and improves the feature extraction capability of the network. Secondly, the parameter-free attention mechanism SimAM and deformable convolutional DCNv2 are fused into the downsampling stage of the neck network to improve the network’s ability to capture the key features of dangerous goods in X-ray images. Finally, the Dynamic Head module is introduced to enhance the scale perception, spatial perception and task perception of the detection head, and improve the detection performance of the network. Experimental results show that the mean average precision (mAP) of the improved algorithm on the self-made dataset and CLCXray dataset is improved by 4.7 percentage points and 1.2 percentage points, respectively, and the number of parameters and calculations are reduced by 16.2% and 23.1%, respectively. The improved algorithm makes detection capability lighter, which can play a good role in actual security checks. © 2024 Journal of Computer Engineering and Applications Beijing Co., Ltd.; Science Press. All rights reserved.