## 2 Methods  
In order to increase the reliability of the detection of railway signals and increase the capability for certification of the system for detection of signals, an integrated system for detection of rails and signals on the railway is proposed, which is based on a combination of CV algorithm (Canny and Hough method) and CNNs-YOLO v5 algorithms. The CV algorithm detects rail lines and thus the route on which the train is moving, and CNN detects the actual signal. The system should detect that the signal is relevant for the train which is true only if the signal is directly next to the track on the right side in the direction of motion or directly above the track. This is especially important in environments where multiple tracks and signals are visible from the frontal train profile, as shown in Figure 1. If the signal detection is not related to the track the train is moving on, the system can falsely detect the signal which is not relevant to itself and proceed to the section where its presence is not allowed, ultimately leading to collision with some other train. The signal and track detection are thus an integrated package which ensures that the AI system is \"aware\" of which route the signal belongs to.  
The system for detection of rails and signals on the railway is explained schematically by a flow chart, as shown in Figure 2. The proposed algorithm allows single images, series of images, and real-time video from an RGB camera. The input data for the proposed algorithm undergo a resize process, due to the easier processing that should follow and the unloading of computer resources. The images are at first processed by CV algorithm consisting of two parts--Canny edge detection and Hough transform. The input images are subject to transformation that will prepare them for further processing. First, the color changes from 3 channels to 1--the image from RGB becomes grayscale. Then, a Gaussian filter is applied, which performs averaging the pixel intensity. Only then is the Canny edge detection process applied, where lines are drawn on the image with a series of white pixels corresponding to rails. The \"If\" statement defines that if the gradient reaches the defined upper threshold, a line is drawn. In addition, if the gradient value is between the upper and lower thresholds and is directly related to a series of strong gradients, lines are drawn. After the applied ROI, Hough transformation follows, where, after fusing the original image and the image with identified rails, output data are obtained, i.e., processed frames with detected rails, which continue to flow through the process and enter step 2--CNN. The detected rail track is overlaid on the resized RGB image. The image processed in this way enters a previously trained system whose goal is signal detection. Darknet is a framework of the YOLO algorithm based on deep learning. The first method of selection is to check the confidence of the detection for each frame--the confidence value must be at least 0.3 in order for the system to continue the process. If the mentioned confidence is reached, the next condition for signal detection is that the system has already drawn the rails, and that the signal is located near it, more precisely, on its right side or above the track. If the signal is placed on the right side, it is considered relevant if there is no track further right or the signal is placed up to the next track on the right side. As the final output, if all the conditions are met, the system displays the processed data; more precisely, it detects the railway infrastructure in an image, video, or in real time.  
Figure 1: Railway infrastructure—multiple tracks and signals along the train path.  
Figure 2: Flow chart—System for detection of rails and signals on the railway.