Raspberry Pi Traffic Light Using TensorFlow & Python

Ashwini Kumar Sinha

8740
Advertisement

Tensorflow TrafficLight Using Raspberry Pi

In metropolitan cities traffic jams are one of the major problems and for the common man and he who use to travel on their legs is one of the major problems. Many time it is seen the people are waiting at crossing  to get the traffic clear and the people have to wait and keep standing for an hours to cross the road it become more complicated when any child and old women have to wait for long time to cross the road so today we are going to Raspberry Pi TrafficLight Using TensorFlow & Python that checks how many people are waiting at zebra crossing and from how long they are waiting at zebra crossing and  give priority to people rather than vehicles accordingly.

How Our System Works ?

  • First a camera streams the live video at zebra crossing 
  • Then that video is cut in certain frames and a tensorflow with computer vision modules checks the number of people and time from which they are waiting to cross the 
  • If the system get that the number of people is greater than the 3(can be changed ) waiting at zebra crossing then it give priority to them. If the number of people at zebra crossing is below 3 but waiting for more than 60 seconds (can be changed)  then it give priority to them in crossing.

So let’s start our project with some of required components 

Bill of material 

Bill of material

Prerequisites 

Assuming that you have already Raspbian os installed and with Python3 environment on raspberry pi and also have access to its desktop.

sudo  apt-get update

sudo apt-get upgrade

sudo nano /etc/dphys-swapfile

Then change the line CONF_SWAPSIZE=100 to  CONF_SWAPSIZE=1024

sudo /etc/init.d/dphys-swapfile stop  

sudo /etc/init.d/dphys-swapfile start

sudo pip3 install opencv

sudo pip3 install numpy

wget https://bootstrap.pypa.io/get-pip.py

pip3 install dlib

pip3 install tensorflow

After the installation you can now proceed with the cloning of TF modules, examples, and files using the following command: 

git clone https://github.com/tensorflow/tensorflow.git  

After successfully cloning, go to the directory →  research folder → and paste the code attached with the article. Now open the python3 IDE.Now let’s understand and change the code. First we have import the required modules in code  and these modules are:

  • os
  • cv2
  • numpy
  • tensorflow
  • argparse
  • sys
  • gpiozero
  • time
Tensor Flow code
Fig 1

Next we will set the path for tensor flow detection modules and the we will set the name and path for labels here we have using the “trafficlight.pbtxt”. Next part of code  will check the camera video and cut it in various frames and after that we have code that will try to detect the objects in each frame and then map with the labels that is assigned in traffic.pbtxt . here in traffic.pbtxt have only two labels that is “person” and “bicycle”(  you can exclude the bicycle )

Object detection folder
Fig 2.
Object detection path
Fig 3

Now in next part of the code we check the label of object detected and here we have set a substring ie “person” we use this substring to count the number of people in image.

Getting Name on People detection
Fig 4.

Now we have created several if() conditional statement that checks weather number of people detected is greater than 0 . Next if () statement will check count number is greater than 3 if yes then it turns the redlight on to stop vehicles and let the people pass . if number of people waiting at crossing is less than 3 then it start counting  the time and if the time exceeds the 60 min then it give priority to people and let them pass.

counting people using Tensorflow
Fig 5.

Connection

Now connect the RPI camera to RPI Camera port using ribbon cable and then connect the components as in diagram(Refer fig 8).

Raspberry Pi connection indication Detection
Fig 6.Circuit diagram

Testing

TensorFlow running
Fig 7.

Now save the code and run the code in python3 IDE and wait for few minutes to load the TF modules and let the camera video window to appear and then bring the camera in front of many people if it detects the number of people detected is greater than 3 then it turn on red  LED for stop sign. If people detected is less than 3 than wait for 60 second if the same people is wait for more than 60 seconds then it will let them pass and stop the vehicles by turning the red light on.

Congrats, Raspberry Pi TrafficLight Using TensorFlow is ready!!

Download Code

Advertisement


SHARE YOUR THOUGHTS & COMMENTS

Please enter your comment!
Please enter your name here