## 3 The Neural Network Dynamic Detection Model
### Combining Background Subtraction Algorithms with YOLOv5 Neural Network  
Given the complexity of the target railway slope sections and the significant differences between operational and non-operational periods, the task presents unique challenges. Normally operational railway slope areas tend to have a clean background, where any foreign object or stone becomes a hazard; however, construction sections along the main railway lines often feature complex backgrounds, such as scenes with numerous rocks or industrial materials, making it difficult to identify risk objects directly through neural network object detection technologies. It is challenging to achieve precise identification of hazardous rocks solely through accumulating data volume, annotating comprehensive datasets, and training mature deep learning models. Often, it necessitates extensive human intervention for accurate risk assessment across various scenes, which is not only time-consuming and labor-intensive but also results in models lacking in generalizability and interpretability, requiring retraining for new scenes. To achieve high recognition efficiency and precision, this study analyzes the characteristics of high-risk targets on railway slopes and finds that target foreign objects often exhibit movement traits. In real scenarios, as opposed to low-risk stationary objects, hazardous rocks display significant kinetic energy, moving down slopes toward the railway. Even in non-operational periods with clean backgrounds, the intrusion of foreign objects into slope areas is due to their kinetic energy. Therefore, this research incorporates Background-SubtractorMOG2 as an auxiliary detection algorithm, combining YOLOv5's identification capabilities with background subtraction to obtain dynamic monitoring results for comprehensively determining the location of falling rocks, as illustrated in Figure 3.  
In this study, the background subtraction object is initialized using the createBackgroundSubtractorMOG2 function from the OpenCV library. This function implements a background subtraction algorithm based on a Gaussian mixture model, capable of adapting to environmental changes and effectively isolating foreground targets in images. When processing video sequences or continuous image frames, the algorithm distinguishes between background and foreground by analyzing pixel changes over time, generating a foreground mask. This mask contains only the pixels considered as foreground, i.e., potential target objects, significantly suppressing background information. After the background subtraction step, the preprocessed image, with the foreground mask applied, is fed into a pre-trained YOLO model for object detection. The YOLO model conducts a one-time analysis of the image through its convolutional network architecture, predicting the category and location of each object in the image. Since the input image has been preprocessed with the background subtraction algorithm, the model can more focusedly recognize and locate target objects in the foreground, thus reducing the negative impact of background complexity on detection performance. To further enhance the accuracy of the detection results, the non-maximum suppression (NMS) algorithm is applied to the YOLO model's output to eliminate overlapping detection boxes and retain the most representative results. Moreover, by further analyzing the detected target locations within the foreground mask, false positives located in static backgrounds can be filtered out, retaining only the true positive detections in the foreground. This method also encompasses post-processing steps for the detection results, including resizing the detection boxes to fit the original image dimensions, processing specific category targets, and performing statistical analysis of the detection performance. Through a custom display function, the processed images and detection results can be displayed in real time, with the option to adjust the image display size as needed.