Sunday, December 22, 2024

Raspberry Pi Traffic Light Using TensorFlow & Python

ashwiniIn 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

- Advertisement -

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

- Advertisement -

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

Ashwini Sinha
Ashwini Sinha
A tech journalist at EFY, with hands-on expertise in electronics DIY. He has an extraordinary passion for AI, IoT, and electronics. Holder of two design records and two times winner of US-China Makers Award.

13 COMMENTS

  1. “After successfully cloning, go to the directory → research folder → and paste the code attached with the article”..

    Hellow :), what is mean by this? i cannot understand clearly.. Really hope you can help me.

    • You have to clone the TensorFlow GitHub respiratory using git clone followed by a link so that the pre-trained models and essential files copied to your raspberry pi automatically. After performing this process you can see a Tensorflow folder in raspberry pi. Now open that and then follow the steps as described in the article

    • I have attached Voice bonnet on Rpi so installed it with AIY Voice Raspbian os for pi. You can download the official latest OS for raspberry pi from the raspberry pi official website, you can find the os image in the download section

  2. Hello sir, can I get your email or any medium that I can contact you? almost 6 times I try to run the following code, but still got some errors.. really hope that you can help me.. Btw really nice project sir?.

  3. Hello sir, can I get your email or any medium to contact you sir? About 6 times I try to run the given code and follow all the step given, but still got some errors.. I really hope that you can help me sir.. Btw really nice project sir ?

SHARE YOUR THOUGHTS & COMMENTS

EFY Prime

Unique DIY Projects

Truly Innovative Electronics

Electronics News

Latest DIY Videos

Electronics Components

Electronics Jobs

Calculators For Electronics