We've all experienced that frustrating moment: waiting at a red light with no traffic from the other direction to justify the delay.
Using computer vision, I built a new class of traffic light that adjusts the lights according to the amount of traffic it detects from each street. Throughout this project, I have encountered numerous probelms. Through preserverance, I was able to ultimately resolve these issues and significantly increased my understanding of computer vision.
I built this system on a Raspberry Pi 4, which presented a large problem: the Raspberry Pi only supports one camera, while I needed four (one for each street side). As a solution, I decided to put the camera on a stepper motor, where the motor rotates 90 degrees to capture each street side.
The first step of this project was to select a traffic dataset. I searched all over Kaggle and Google Search, but I couldn't find anything that fit my needs. Building a dataset like I did with my traffic sign classification project was impractical because unlike traffic signs, vehicles vary in size, shape, color, and features, which increases the complexity of assembling a good dataset. I ultimately found COCO, a large 120,000 image dataset with images for 80 different classes - ranging from different types of vehicles, to people, to toothbrushes.
Next, I needed to choose an object detection algorithm. I was tempted to use a Convolutional Neural Network again for its high accuracy; but because training models is computationally expensive, I opted for an algorithm called YOLO (You Only Look Once) which is a much faster and computationally efficent algorithm, albeit slightly less accurate than a CNN. The next big problem came to training a model. Downloading the entire COCO dataset was over 30 GB, and the dataset would then need to be put in a special format to be passed to YOLO. I spent 2 months trying to figure out how to create this model. I tried numerous libraries, searched all over the internet, but none of the guides made any sense. I then found Ultralytics, an API with a pretrained YOLOv8 (newest/fastest version of YOLO) on the COCO dataset.
At first, I had all my code in one large file. This method made the program very hard to read and view, so I split the code into classes: a Detector class, which detects and classifies objects; a TrafficLight class, which controls the traffic light and contains the algorithm for controlling lights; and a Motor class, which controlls the stepper motor.
Next, I created a algorithm for controlling the traffic light, which was hard-coded logic. At the time of this project, I was much less experienced with AI. If I was working on this project today, I would use Reinforcement Learning to control traffic logic instead of hard-coding it.