YOLOv7 has the highest accuracy of any known real-time target detector, improving the state of the art in target detection methods. In order to achieve real-time and accurate detection of track foreign objects and reduce cumbersome labour costs, this paper constructs a dataset of track foreign objects, applies the yolov7 network model based on the pytorch framework to train the dataset, and detects the track foreign object test images by the training model with an accuracy of 0.816 and a recall of 0.667, mAP@0.5 for 0.657, showing from the experimental results that the dataset achieved better results in terms of recognition accuracy.