# Brake Light Detection Algorithm for Predictive Braking
## 3 Methods
### Algorithm  
The developed brake light detection algorithm builds upon a YOLOv3 [5] CNN model implemented in PyTorch [28], open source computer vision library OpenCV 3.4.0 [29] and a random forest classification algorithm [30; 31]. The used version of YOLOv3 has been trained on the COCO dataset [32] which contains nearly 100,000 samples of passenger vehicles. The detailed training parameters of the network can be found in the original source code [33]. A 608 pixel frame input, which is relatively high for CNN processing, was selected for YOLOv3 to achieve the highest possible detection rates. High input resolution ensures that vehicles can be detected from a longer distance while preserving details in the detected vehicle. Distances up to 150 m are considered here. After detecting the vehicle rears with YOLOv3, the bounding boxes are preprocessed, which means performing colorspace filtering. These preprocessed images are downscaled in resolution and fed to the random forest algorithm. Resolution reduction is necessary to prevent overfitting of the random forest algorithm. However, proper resolution reduction is only possible when the original image is clear. In this case, the clarity is ensured by the high resolution. Predicting the brake light status of the preceding vehicle from all three brake lights, including upper middle brake light, requires that the rear of the vehicle is properly detected. The developed algorithm assumes that all the vehicles have a center brake light as well as no faulty equipment or malfunctions, such as brake light operation failure or faulty lamps. The entire detection process is depicted in Figure 3. For clarity, the detection process can be divided to three stages: (i) object detection, (ii) image preprocessing, and (iii) random forest classification. In Stage (i), the image is processed through the YOLOv3 object detection for vehicle recognition. YOLOv3 object detection returns bounding boxes indicating the areas that have vehicles in the image. The bounding boxes extracted from the original image are then fed into the image preprocessing stage. For the colorspace filtering in the preprocessing stage, L*a*b colorspace was used. This colorspace was chosen after observing the LAB, HSV, and RGB values of the brake lights in the gathered dataset. LAB produced the most consistent and separable values for the brake lights. Figure 4 provides a pixel distribution of lit brake lights (marked as red triangles) in comparison to vehicle rear (marked as gray dots). By examining the plot representing an example of the dataset, it can be seen that L*a*b colorspace pixels provide a compact volume in lit brake light pixels. The same distribution was seen in the dataset pixel value scatter and distribution. In the L*a*b colorspace, pixels are represented by three variables: \(L\), \(a\), and \(b\). \(L\) is the value for lightness, \(a\) the green-to-red spectrum, and \(b\) the blue-to-yellow spectrum. The L*a*b colorspace was defined by the International Commission on Illumination (CIE) in 1976 and it is officially called the CIELAB colorspace. Figure 2: Hardware installations on the test vehicle. The windshield camera installation on the (**left**) and computational power with accessories on the (**right**). Stage (ii) receives bounding box images from Stage (i) and filters away pixel values that do not belong to a pre-specified threshold range. The threshold ranges are defined for all three variables (\(L\), \(a\), \(b\)) as shown in Table 4. These threshold ranges were acquired by capturing initial image samples of brake lights and analyzing the color values found in the brake light area at different ranges with different vehicles with the research vehicle camera. Pixels that do not have all their (\(L\), \(a\), \(b\)) values in the specified ranges have their values changed to 0. These preprocessed images are used as input for the random forest algorithm in Stage (iii).