You are on page 1of 83

Camera based traffic light control system on raspberry-pi 2016

Addis Ababa University


Addis Ababa Institute of Technology
School of Electrical and Computer Engineering

A CAMERA BASED TRAFFIC LIGHT CONTROL SYSTEM


USING RASPBERRY PI

A thesis submitted to the School of Electrical and Computer Engineering of


Addis Ababa University in Partial fulfillment of the Degree of Bachelor of Science in
Electrical and Computer Engineering

By:

No Name ID.No STREAM

1 Asfaw Amdissa ENR\4430\04 Control

2 Eyob Dagne ENR\6663\04 Control

3 Nigus Tasew ENR\1711\04 Control

Advisor:
Dr. Dereje S.

Addis Ababa
Ethiopia, June, 2016

i|Page
Camera based traffic light control system on raspberry-pi 2016

Addis Ababa University


Addis Ababa Institute of Technology
School of Electrical and Computer Engineering

A CAMERA BASED TRAFFIC LIGHT CONTROL SYSTEM


USING RASPBERRY PI
A thesis submitted to the School of Electrical and Computer Engineering of
Addis Ababa University in Partial fulfillment of the Degree of Bachelor of Science in
Electrical and Computer Engineering

By:

No Name ID. No STREAM

1 Asfaw Amdissa ENR\4430\04 Control

2 Eyob Dagne ENR\6663\04 Control

3 Nigus Tasew ENR\1711\04 Control

Approval by:

Dr. Dereje S. Signature

Advisor

Examiner 1 Signature

ii | P a g e
Camera based traffic light control system on raspberry-pi 2016

Certification

The undersigned certify that he has read the thesis entitled: camera based traffic light
control system using raspberry pi and hereby recommend for acceptance by the Addis
Ababa University in partial fulfillment of the requirements for the degree of Bachelors of
Science.

____________________________________

Dr. Dereje S. (Advisor)


_______________________________________ Date

iii | P a g e
Camera based traffic light control system on raspberry-pi 2016

Declaration and Copy Right

We, declare that this thesis paper is our own original work and that it has not been presented
and will not be presented by us to any other University for similar or any other degree award. This
paper is copyright material protected. It may not be reproduced by any means, in full or in part,
except for short extracts in fair dealing, for research or private study, critical scholarly review or
discourse with an acknowledgement, without written permission of the school of Electrical and
Computer Engineering, on behalf of both the author and Addis Ababa University.

Signature

1. Asfaw Amdissa Gemechu +251913759070


2. Eyob Dagne Taye +251920031136
3. Nigus Tasew Tsegay +251921889462

iv | P a g e
Camera based traffic light control system on raspberry-pi 2016

Acknowledgment

First and foremost, we would like to thank the God Almighty who blessed and supported us
in every step of our journey and entitled us for this opportunity.

Our families, friends and are second only to God, in creating good working environment by
sharing our other responsibilities.

We would like also thank the staff members of School of Electrical and Computer
Engineering for their cooperation and assistance during our study.

This project would not be successfully completed without the supervision and critical
leading of our advisor Dr. Dereje Shiferaw.

v|Page
Camera based traffic light control system on raspberry-pi 2016

Abstract

The availability and the growing number of automobiles in Ethiopia, especially in Addis
Ababa brought some problems. Some of the problems are happening because of the operating
technique of conventional traffic lights. They work by allocating equal and fixed time for every
road that is regardless of their respective load. Mostly as a result of this, we see traffic conjunctions
around several road junction areas of the city. Hence, we can say the very approaches that are made
to solve the problem, sometimes become cause of the problem.

We have learned that the Addis Ababa City Road Administration has tried to solve the
problem, using the statistical way of time allocation at some areas of the city, but the problem is yet
far from removed. This all implies that another way of solution should be proposed.

This project is designed to develop a system which performs execution based on density of
vehicles (Vehicle Count). After calculating the number of vehicles we will came to know in which
lane the density is high and based on which signals will be allotted for a particular lane.

Raspberry pi is used as a microcontroller for image processing and also provides the
signaling time based on the traffic density.

Hence, in this project a density based Traffic Light Controlling System simulation and
prototype is developed to improve the conventional traffic lights of our city.

Key words: raspberry-pi, image processing, traffic congestion, lane, traffic signal

vi | P a g e
Camera based traffic light control system on raspberry-pi 2016

Table of Contents
Declaration and Copy Right .......................................................................................................................... iv
Acknowledgment .............................................................................................................................................. v
Abstract ........................................................................................................................................................... vi
List of acronyms ............................................................................................................................................. xii
Chapter One ..................................................................................................................................................... 1
1. Introduction.............................................................................................................................................. 1
1.1. Overview of the project ................................................................................................................... 1
1.2. Problem Statement .......................................................................................................................... 2
1.3. Objective of the project ................................................................................................................... 5
1.3.1. General Objective ...................................................................................................................... 5
1.3.2. Specific Objectives ..................................................................................................................... 5
1.4. Scope of the project.......................................................................................................................... 6
1.5. Document Organization .................................................................................................................. 7
1.6. Limitation ......................................................................................................................................... 7
Chapter Two..................................................................................................................................................... 8
2. Literature Review .................................................................................................................................... 8
2.1. Traffic light system .......................................................................................................................... 8
2.1.1. Definition of Traffic Conjunction ............................................................................................... 9
2.1.2. Source of Traffic Conjunction .................................................................................................... 9
2.1.3. Solutions of traffic conjunction................................................................................................ 10
2.2. Image processing ............................................................................................................................ 13
2.2.1. Types image processing ........................................................................................................... 15
Chapter Three ................................................................................................................................................ 16
3. Detail about System Components of the Project ................................................................................. 16
3.1. Hardware Components ................................................................................................................. 16
3.1.1. RASPBERY – PI .......................................................................................................................... 16
3.1.2. USB Web Camera ..................................................................................................................... 24
3.1.3. Stepper Motor ......................................................................................................................... 25

vii | P a g e
Camera based traffic light control system on raspberry-pi 2016

3.1.4. Stepper Motor Driver............................................................................................................... 29


3.1.5. Light Emitting Diodes (LED) for Traffic signaling ...................................................................... 31
3.2. Programing Tools (software) ........................................................................................................ 32
3.2.1. Raspbian OS ............................................................................................................................. 32
3.2.2. Python ...................................................................................................................................... 32
3.2.3. Open CV ................................................................................................................................... 33
3.2.4. VNC server ............................................................................................................................... 35
3.2.5. SSH client ................................................................................................................................. 35
Chapter Four .................................................................................................................................................. 36
4. DESIGN AND METHODOLOGY ...................................................................................................... 36
4.1. Controlling a stepper motor with raspberry pi ................................................................................ 37
4.1.1. Stepper motor driver and Indexer (controller) ........................................................................ 37
4.1.2. Raspberry pi as stepper motor controller:............................................................................... 38
4.1.3. Setting the connection: ............................................................................................................ 39
4.2. Capturing real time image using webcam and Open CV ................................................................ 40
4.2.1. Installing Open CV on Raspberry pi ............................................................................................... 40
4.2.2. Open CV installation steps ............................................................................................................. 40
4.2.3. Capturing real time image ............................................................................................................. 44
4.3. Image processing using Raspberry pi .......................................................................................... 45
4.3.2. Image cropping .............................................................................................................................. 46
4.3.3. Image Enhancement ...................................................................................................................... 46
4.3.4. Thresholding .................................................................................................................................. 46
4.3.5. Mean square error ......................................................................................................................... 46
4.3.6. Car Detection ................................................................................................................................. 47
4.4. Changing the duration of traffic light .......................................................................................... 47
Chapter Five ................................................................................................................................................... 50
5. Hardware implementation .................................................................................................................... 50
5.1. Development Tools.............................................................................................................................. 50
5.2. Working environment ........................................................................................................................ 50
5.3. Prototype......................................................................................................................................... 51

viii | P a g e
Camera based traffic light control system on raspberry-pi 2016

5.4. Load Detecting ............................................................................................................................... 52


Chapter Six ..................................................................................................................................................... 53
6. Results, Discussions, Conclusion and Recommendation .................................................................... 53
6.2. Discussion ............................................................................................................................................ 55
6.3. Conclusions .......................................................................................................................................... 56
6.4. Recommendations ............................................................................................................................... 57
References ....................................................................................................................................................... 59
Appendix ......................................................................................................................................................... 61

ix | P a g e
Camera based traffic light control system on raspberry-pi 2016

List of Tables

Table 1: Pin configuration of a Raspberry Pi ............................................................................................ 23


Table 2: Feature and specification of INTEX IT-309WC web camera..................................................... 24
Table 3: Pulse Sequence of a Bipolar Stepper Motor ................................................................................. 29
Table 4 : Algorithm for controlling the green signaling time .................................................................... 48
Table 5: Time allocation algorithm for prototype ...................................................................................... 52

x|Page
Camera based traffic light control system on raspberry-pi 2016

List of Figures

Figure 1 Traffic signal lights ........................................................................................................................... 1


Figure 2 Unnecessary waiting on junction..................................................................................................... 3
Figure 3 Conventional traffic light with 4 road junctions ........................................................................... 6
Figure 4 Source of Traffic Congestion ......................................................................................................... 10
Figure 5 Front side of raspberry-pi.............................................................................................................. 18
Figure 6 win32DiskImager In action............................................................................................................ 19
Figure 7 Bitvise SSH on windows pc. ........................................................................................................... 20
Figure 8 VNC viewer on window pc ............................................................................................................. 21
Figure 9 USB WI-Fi adapter......................................................................................................................... 22
Figure 10 an INTEX IT 309WC WEB CAMERA ...................................................................................... 25
Figure 11 Stepper motor and its associated control unit ............................................................................ 26
Figure 12 permanent magnet stepper motor ............................................................................................... 27
Figure 13 variable reluctance stepper motor .............................................................................................. 28
Figure 14 hybrid stepper motor.................................................................................................................... 29
Figure 15 a Typical L293D IC ...................................................................................................................... 30
Figure 16 System General Block diagram ................................................................................................... 36
Figure 17 L293D motor driver connection with bipolar stepper motor ................................................... 37
Figure 18 Overall circuit connection for controlling the stepper motor. .................................................. 40
Figure 19 expanding the file system in raspberry pi................................................................................... 41
Figure 20 the flow chart of image processing .............................................................................................. 45
Figure 21: general system flow chart the design system ............................................................................... 49
Figure 22: typical designed prototype of a 4 way road ............................................................................... 52
Figure 23 general result of the system......................................................................................................... 54

xi | P a g e
Camera based traffic light control system on raspberry-pi 2016

List of acronyms

CMOS- Complementary Metal-Oxide Semiconductor

EESID- Extended Service Set Identification

GPIO- General Purpose Input/output

GPS- Geographical Positioning System

GSM- Global System for Mobile communication

GUI- Graphical user Interface

HDMI- High Definition Multimedia Interface

IP- Internet Protocol

LAN- Local Area Network

LED- Light Emitting Diode

NumPy- Numerical Python

Open CV- Open Computer Vision

OS- Operating System

RAM-Random Access Memory

RFID - Radio Frequency Identification

RGB- Red-Green-Blue

SD- Secured Digital Card

SOC- System on Chip

SSH- Secured Shell

USB- Universal Serial Bus

VNC- Virtual Network Computing

WAN- Wide Area Network

xii | P a g e
Camera based traffic light control system on raspberry-pi 2016

Chapter One
1. Introduction

1.1. Overview of the project

Traffic lights, which may also be known as stoplights, traffic lamps, traffic signals, stop-
and-go lights are signaling devices positioned at road intersections, pedestrian crossings and other
locations to control competing flows of traffic. Traffic lights have been installed in most cities
around the world.

Figure 1 Traffic signal lights

The most common traffic lights consist of a set of three lights: red, yellow (umber), and
green (the colors of the lights are shown in Figure 1. When illuminated, the red light indicates for
vehicles facing the light to stop; the yellow indicates caution, either because lights are about to
turn green or because lights are about to turn red; and the green light to proceed (if it is safe to do
so). There are some variations in the use and legislation of traffic lights, depending on the
customs of a country and the special need of a particular intersection.

Traffic problems nowadays are increasing because of the growing number of vehicles, and
the limited resources provided by current infrastructure in roads. As we know in Ethiopia the
population in cities and number of cars is increasing day to day. With increasing urban population
and hence the number of cars, need of controlling streets, highways and roads is vital. Traffic is the
major problem which Ethiopia, especially down towns in Addis Ababa, faces today.

1|Page
Camera based traffic light control system on raspberry-pi 2016

The increase in number of vehicles resulting to the need of a smart system that could
efficiently handle traffic congestion based on the density of traffic.

We proposed a system design for controlling the traffic light by image processing. A
camera will be installed on a stepper motor alongside the traffic light and will rotate in some pre
specified direction which could capture image sequence from the road. A step by step approach of
image acquisition, image processing and implementation of algorithm to change the traffic light
duration as per the density of vehicles on different roads at a traffic signal is followed. The number
of objects in a given image is counted and enough time is given according to their density.

The proposed control system solves the current traffic problem which frequently happens
due to a time based traffic control system.

1.2. Problem Statement

Although one of the benefits of traffic lights is to minimize the high traffic conjunction and
to facilitate the traffic flow, sometimes the very system that is built to avoid the traffic conjunction
becomes a reason in creating it. Poor traffic signal timing accounts for an estimated 10 percent of
all traffic delay.

The monitoring and control of city traffic is becoming a major problem in many countries.
Even in Addis Ababa with the ever increasing number of vehicles on the road, the Addis Ababa
City Roads Authority has to find new ways or measures of overcoming such a problem. The
measures taken are development of new roads and flyovers (underpass and overpass) in the
middle of the City; building (rebuilding) large roundabouts in major traffic interchanges, building
of the ring road, planning to build electric buses and city trains and restricting of large vehicles at
some areas of the city during peak hours. Though all these things are being planned or done, the
problem is still far from solved.

How many times have we been sitting at a stoplight for 2, 3 minutes, no cars coming from
any direction? At some selected areas of the city it’s observed that when there were no cars from
some directions, the traffic light system is operating by giving equal time for green light for all

2|Page
Camera based traffic light control system on raspberry-pi 2016

the directions that are with the highest possible load. It’s seen that before the roads with the highest
load completely free from their respective load, the nearly empty roads were getting their next
round green light time consistently, it seems illogical.

Figure 2 Unnecessary waiting on junction

Inconvenience will be caused by unnecessary waiting; people will lose time, miss
opportunities and get frustrated. Traffic congestion problems create a deep impact on companies’

3|Page
Camera based traffic light control system on raspberry-pi 2016

productivity and transportation of goods. In addition, the existing traffic control system may have
the following major problems:
 It wastes the precious time in commuter caused by traffic lights.
 There is an excess expenditure on government on traffic police.
 There is an unnecessary road construction for reducing a traffic jam, which can be
reduced by using a smart traffic light control.
 There is high fuel consumption which is caused by traffic jam on junctions.

So to solve all the above mentioned problems, we can implement the system that controls
the traffic based on the density of vehicles at any particular lane. With this system, we shall
consider the density of vehicles at each side at the junction and give path to the particular side
which has high density of vehicles and keep the remaining in stop position.

4|Page
Camera based traffic light control system on raspberry-pi 2016

1.3. Objective of the project

1.3.1. General Objective

The main objective of this project is to change the current time based traffic control system
by density based, which is by considering the density of the vehicles in each lane and giving
appropriate green signaling time.

1.3.2. Specific Objectives

The specific objectives of this project are:


 Interfacing a web camera with RASPBERRY-PI.
 Control a stepper motor using the RASPBERRY-PI.
 Image processing using RASPBERRY-PI and appropriate image processing software
for vehicle counting.
 Design a system that decides the timing of the traffic light based on the processed
image.
 Prototyping the overall system.

5|Page
Camera based traffic light control system on raspberry-pi 2016

1.4. Scope of the project

There are different types of road junctions in Addis Ababa. But given that a 4 way road
junction (at 4 way road junctions there are a total of four traffic lights, which controls traffic from
every particular direction) is common, we restrict our system to a 4-way junction (as shown in
Figure 3).Hence our traffic control system design based on density is implemented in this four way
road junction.

Figure 3 Conventional traffic light with 4 road junctions

6|Page
Camera based traffic light control system on raspberry-pi 2016

1.5. Document Organization

This thesis work is organized into six chapters and associated reference and appendix.

The first chapter is an introduction that includes overview of the project, statement of the
problem, objectives, and scope of the project and significance of the study as well as the overall
thesis outline.

Chapter two covers the review of the main related facts from references used in the study
and review of earlier studies. The various studies conducted in specific parts of the entire study area
are well described and used in the study. And in this chapter a literature about image processing is
reviewed.

Chapter three covers the detail hardware and software components used to implement the
project. In the hardware part, a description about raspberry pi, stepper motor and its driver which is
L293D, an Intex IT 309 WC is mentioned in detail.

Chapter four covers the methodology and system design of the project.in this chapter the
algorithms followed to implement the proposed project is described and covered in detail.

In Chapter five the hardware implementation of the project is presented. Chapter six
discusses the conclusion, discussions, and recommendations of the implementation of the project.

1.6. Limitation

 Difficulties on developing density detection algorithm for our specific case


 Weather and lighting condition during image acquisition
 Speed limitation on the Raspberry pi computing power
 Installing Open CV on the Raspberry pi

7|Page
Camera based traffic light control system on raspberry-pi 2016

Chapter Two

2. Literature Review

2.1. Traffic light system

A traffic light system is an electronic device that assigns right of way at an


intersection or crossing or street crossing by a means of displaying the standard red, yellow and
green light or color indications. So traffic congestion is a condition on road networks that occurs as
use magnifies, and is describe by slower speeds, longer trip times, and augmented conveyance
queuing. The most communal example is the physical use of roads by vehicles.

Traffic congestion is an extremely annoying feature of road transport. It consumes


substantial amounts of valuable time, creates difficulties for scheduling and on-time deliveries, and
thus reduces the potential advantages of road transport. Congestion typically occurs at times of high
travel demand or as a consequence of accidents or other non-recurring incidents that temporarily
reduce a road's capacity. It is associated with external costs in the sense that an additional driver on
a road forces everyone else using this same road at the same time to adapt to the higher traffic
volume by lowering driving speeds, so other drivers need more time to cover a given distance ;
(Schrage, 2006).

It is clear that increasing traffic congestion does impose costs upon travelers and affect
broader business operations; it has been difficult to develop and apply empirical measures of the
extent of the traffic congestion and its economic costs. Even though, proper measuring the extent or
level of congestion is an important step for understanding the performance of the existing road
network, for evaluation of proposed congestion mitigation measures and for evaluation of cost of
congestion; (Weisbrod et.al, 2003; Taddesse, 2011).

8|Page
Camera based traffic light control system on raspberry-pi 2016

2.1.1. Definition of Traffic Conjunction

The definition of congestion is imprecise and is made more difficult since people have
different perceptions and expectations of how the system should perform based on whether they are
in rural or urban areas, in peak or off peak, and as a result of the history of an area (Bertini, 2005).

There is no consistent definition of congestion in terms of a single measure or set of


measures that considers severity, duration and spatial extent. Measures related to travel time and
speed are the most flexible and useful for a wide range of analyses (R. Narayanan et. al.2003). For
instant The Federal Highway Administration (FHWA) defines traffic congestion as: ―the level at
which transportation system performance is no longer acceptable due to traffic interference.‖
According to Lomax et.al, 1997, traffic congestion definition depends on the understanding of the
road users. Based on this the definition is divided as congestion and unacceptable congestion.

2.1.2. Source of Traffic Conjunction


According to a research done by Cambridge Systematics, Inc., 2004 with Texas
Transportation Institute Traffic Congestion has several root causes that summarized the finding in
Figure 1.1:

9|Page
Camera based traffic light control system on raspberry-pi 2016

Figure 4 Source of Traffic Congestion

2.1.3. Solutions of traffic conjunction

There are lots of methods in our world to design and build modern traffic light controlling
systems as a solution for the occurring traffic conjunction. The following are some of them:

 Lei, Leong; Intelligent traffic lights using GSM. A traffic signal control
system employing Universal Coordinated Time (UTC) of GPS as time base
comprising: a core system for receiving data of GPS, and to include longitude
and latitude data and to convert information to local time as a common time basis
for the traffic signal control system;
 Khalid A. S al developed an intelligent RFID traffic control.
RFID stands for Radio Frequency Identification, a technology that uses tiny
computer

10 | P a g e
Camera based traffic light control system on raspberry-pi 2016

chips smaller than a grain of sand to track items at a distance. This technology can
be
applied to a multi vehicle, multi-lane and multi road junction area to provide an
efficient time management scheme. A dynamic time schedule was worked out for
the passage of each column.

The intelligent traffic control system consists of four main parts: the RFID tags, the Cisco
access point, the Cisco location server and Wide Area Network (WAN). The first simulates the
moving vehicles; the second detects the RFID tags, the third acts as RFID software and fourth
simulate the ubiquitous environment. The location server acts as the microcontroller of the traffic
signal. It will collect the location and tagging time of the data from reader. This information is sent
to the management system via Internet. The management system using intelligent algorithm will
then send suitable instructions to control the traffic;

 Mamdani simulated an isolated signalized intersection composed of two one-


way
streets with two lanes in each direction without turning traffic. The fuzzy
controller
reduced average vehicle delay compared to an actuated controller. Tan, Khalid
and Yusof describe a fuzzy logic controller for a single junction that imitates
human
intelligence. They used two sensors for each lane. The first sensor behind each
traffic
light counts the number of cars passing the traffic lights, and the second sensor
behind the first sensor counts the number of cars coming to the intersection at
distance from the lights. The fuzzy logic controller determines the time that the
traffic light should stay in a certain state, before switching to the next state;

 Another expert system uses a set of given rules to decide upon the next action.
In traffic light control, such an action can change some of the control

11 | P a g e
Camera based traffic light control system on raspberry-pi 2016

parameters. Findler and Stapp describe a network of roads connected by


traffic light-based expert systems. The expert systems can communicate to
allow for synchronization. Performance on the network depends on the rules
that are used. For each traffic light controller, the set of rules can be optimized
by analyzing how often each rule fires, and the success it has. The system
could even learn new rules. Findler and Stapp showed that their system could
improve performance, but they had to make some simplifying assumptions to
avoid too much computation.

 Tavladakis and Voulgaris describe a traffic light controller using a simple


predictor. Mesasurements taken during the current cycle are used to test
several possible settings for the next cycle, and the setting resulting in the least
amount of queued vehicles is executed. The system seems highly adaptive, and
maybe even too much so. Since it only uses data of one cycle, it could not
handle strong fluctuations in traffic flow well. In this case, the system would
adapt too quickly, resulting in poor performance.

 Liu et al introduce a way to overcome problems with fluctuations. Traffic


detectors at both sides of a junction and vehicle identification are used to
measure delay of vehicles at a junction. This is projected to an estimated
average delay time using a filter function to smooth out random fluctuations.
The control system tries to minimize not only the total delay, but the summed
deviations from the average delay as well. Since it is no longer beneficial to let
a vehicle wait for a long time, even if letting it pass would increase the total
waiting time, this introduces a kind of fairness. Data of about 15 minutes is
used to determine the optimal settings for the next cycle, and even using a
simple optimization algorithm, the system performs well compared to preset
and actuated controllers.

12 | P a g e
Camera based traffic light control system on raspberry-pi 2016

 Maliket et al. use magnetometer sensor for vehicle detection. The sensor
detects distortions of the Earths field caused by a large ferrous object like a
vehicle. Since the distortion depends on the ferrous material, its size and
orientation, a magnetic signature is induced corresponding to the vehicles
shape and configuration. For detecting the presence of a vehicle, measurements
of the (vertical) z-axis is a better choice as it is more localized and the signal
from vehicles on adjacent lanes can be neglected.

All these different types of intelligent traffic light systems give the impression of
performing far better than the conventional traffic light control system. But they are based on
modern, latest and expensive techniques, and these techniques need some support from other
domains as well. Hence, it seems we have to use some other technique to reach the goal of
implementing density based traffic control system. For this reason, we have selected to solve the
problem of the city using an image processing technology to determine the vehicles density with the
help of Raspberry pi.

2.2. Image processing

In imaging science, image processing is processing of images using mathematical operations by


using any form of signal processing for which the input is an image, a series of images, or a video,
such as a photograph or video frame; the output of image processing may be either an image or a
set of characteristics or parameters related to the image. Most image-processing techniques involve
treating the image as a two-dimensional signal and applying standard signal-processing techniques
to it. Images are also processed as three-dimensional signals where the third-dimension being time
or the z-axis.

Image processing usually refers to digital image processing, but optical and analog image
processing also are possible. This article is about general techniques that apply to all of them. The
acquisition of images (producing the input image in the first place) is referred to as imaging.

Closely related to image processing are computer graphics and computer vision. In computer
graphics, images are manually made from physical models of objects, environments, and lighting,

13 | P a g e
Camera based traffic light control system on raspberry-pi 2016

instead of being acquired (via imaging devices such as cameras) from natural scenes, as in most
animated movies. Computer vision, on the other hand, is often considered high-level image
processing out of which a machine/computer/software intends to decipher the physical contents of
an image or a sequence of images (e.g., videos or 3D full-body magnetic resonance scans).

In modern sciences and technologies, images also gain much broader scopes due to the ever
growing importance of scientific visualization (of often large-scale complex scientific/experimental
data). Examples include microarray data in genetic research, or real-time multi-asset portfolio
trading in finance.

Or Image processing is a method to convert an image into digital form and perform some
operations on it, in order to get an enhanced image or to extract some useful information from it. It
is a type of signal dispensation in which input is image, like video frame or photograph and output
may be image or characteristics associated with that image. Usually Image Processing system
includes treating images as two dimensional signals while applying already set signal processing
methods to them.

It is among rapidly growing technologies today, with its applications in various aspects of a
business. Image Processing forms core research area within engineering and computer science
disciplines too.

Image processing basically includes the following three steps.

 Importing the image with optical scanner or by digital photography.


 Analyzing and manipulating the image which includes data compression and image
enhancement and spotting patterns that are not to human eyes like satellite photographs.
 Output is the last stage in which result can be altered image or report that is based on
image analysis.

14 | P a g e
Camera based traffic light control system on raspberry-pi 2016

2.2.1. Types image processing

The two types of methods used for Image Processing are Analog and Digital Image Processing.
Analog or visual techniques of image processing can be used for the hard copies like printouts and
photographs. Image analysts use various fundamentals of interpretation while using these visual
techniques. The image processing is not just confined to area that has to be studied but on
knowledge of analyst. Association is another important tool in image processing through visual
techniques. So analysts apply a combination of personal knowledge and collateral data to image
processing.

Digital Processing techniques help in manipulation of the digital images by using computers.
As raw data from imaging sensors from satellite platform contains deficiencies. To get over such
flaws and to get originality of information, it has to undergo various phases of processing. The three
general phases that all types of data have to undergo while using digital technique are Pre-
processing, enhancement and display, information extraction.

15 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Chapter Three

3. Detail about System Components of the Project

3.1. Hardware Components


The following hardware components are used to implement our project

 Raspberry-pi
 Stepper motor
 Webcam
 Breadboard
 USB Wi-Fi adapter
 LEDs
 Resisters
 L293D(H-bridge) motor driver
 Conducting wires
 PCB Board

3.1.1. RASPBERY – PI

3.1.1.1. BASICS OF RASPBERRRY-PI Model B


The Raspberry Pi is a small, powerful and lightweight ARM based computer which can do
many of the things a desktop PC can do All Raspberry Pi models have the same heart and brain: a
system on a chip (SoC) named BCM2835 that you can find in many mobile phones. It’s cheap, it’s
powerful, and it does not consume a lot of power. These characteristics made it a perfect choice for
the Raspberry team. The BCM2835 contains an ARM1176JZ-F processor running at 700MHz,
256MB of RAM, and a GPU named Video Core IV.

16 | P a g e
Camera based traffic light control system on raspberry-pi 2016

On a Model B board, we can find


 two regular-sized USB ports,
 a micro- SD card port at the back of the board,
 Power supply port; a raspberry pi is powered by a 5V micro USB supply.
Exactly how much current (mA) the Raspberry Pi requires is dependent on what
peripherals are connected to it. Typically, the model B uses between 700-
1000mA depending on what peripherals are connected.
 Ethernet (LAN) port which is 10/100 baseT Ethernet,
 Ports for connecting both HDMI and composite video, it uses 50mA.
 Two rows of pins. Most of these pins are general-purpose input/output pins
(GPIOs), and we can use them to connect the Pi to other electronic devices. In
general there are 26 pins in height of 2.54mm expansion header. Among these
eight GPIO pins plus access to PC, SPI and UART as well as +3.3v, +5v and
GND supply lines. They can draw 50mA safely, distributed across all the pins;
an individual GPIO pin can only safely draw 16mA.

17 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 5 Front side of raspberry-pi

3.1.1.2. Installing an Operating system on RASPBERRY-PI

 Available operating system

Like every computer, the Raspberry Pi needs an operating system, and the preferred one for
the Pi is Linux. That’s partly because it’s free, but mainly it’s because it runs on the Pi’s ARM
processor while most other operating systems work only on the Intel architecture. Still, not every
Linux distribution will run on the Pi, because some do not support the Pi’s particular type of ARM
processor. The Raspbian is the one that supported by the pi and has an image called Debian wheezy
or Debian Jessie (this is the one used in our case).

 Prepare a Bootable SD Card to installing an operating system

Raspberry Pi doesn’t have a BIOS or internal persistent storage. It has only an SD card slot.
Preparing the SD card on a Windows box is the most convenient alternative, because of

18 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Win32DiskImager. This application has a nice user interface, and has a single purpose which is
writing images to SD cards.

Figure 6 win32DiskImager In action

After completing the above action the SD card will become bootable and consisting of
Raspbian, Jessie operating system.

3.1.1.3. Networking with the Pi

We use our raspberry pi by connecting it to our pc. This requires wired or wireless network
between the pi and our pc. This mechanism of using raspberry pi as a computer is known as
headless.

 Use Secure Shell (SSH) with the Pi

One of the best ways to communicate securely between two computers is Secure Shell
(SSH), a network protocol for secure data communication. Raspbain comes with everything needed
to use SSH, there is only a few things have to configured.
This method helps us to use mouse, keyboard, and other accessories from a single computer;
even if there may be a possible disadvantage of using SSH which is it works only in a text terminal.
This limitation on the other hand can be overcome by using virtual network computing (VNC), a
technology that transmits the whole screen and all mouse and keyboard events from one computer
to another.

19 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Basically to enable VNC, there must be a client and server. The server runs on the machine
we would like to control which is in our case is the raspberry pi, and the client runs on the
controlling machine which is a laptop. One thing we have to do is installing the VNC server on our
raspberry pi. This is can be done via apt-get install tightvncserver command in the terminal side.

pi@raspberrypi ~ $ sudo apt-get install tightvncserver


pi@raspberrypi ~ $ tightvncserver
You will require a password to access your desktops.
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
New 'X' desktop is raspberrypi :1
Creating default startup script /home/pi/.vnc/xstartup
Starting applications specified in /home/pi/.vnc/xstartup
Log file is /home/pi/.vnc/raspberrypi:1.log

Figure 7 Bitvise SSH on windows pc.

When tightvncserver is run for the first time, it asks to set a password. This password is
entered when the VNC is connected every time. To address a virtual screen, we need two things:
the IP address of the Pi and the port address of the screen.

20 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 8 VNC viewer on window pc

 Add Wi-Fi to the Pi

Getting the access of internet via raspberry pi was critical desired during project time. The
main areas we need the access of internet was during installing Open CV, which take almost 2 days,
and many other software on pi. Basically whenever we try to install software’s which are not
included in the Raspbian operating system needs an internet access.
Joining wired networks via Ethernet is not usually complex task: plug in the Ethernet cable
and we’re done. However we are using the Ethernet port to connect the pi with our laptop to setup
the headless connection so we can’t use the Ethernet cable as source of internet for the pi. So, we
forced to investigate a possible way of getting an access of internet via wireless mode. To access a
wireless network we need to do a bit more. First, we need a Wi-Fi adapter for the USB port,
because the Pi’s hardware does not support Wi-Fi by default. We plug Wi-Fi adapter into one of
the Pi’s USB ports and run the lsusb command to see if the Pi recognizes it properly:

21 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 9 USB WI-Fi adapter


pi@raspberrypi ~ $ lsus
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 050d: 0237 Belkin Components F5U237 USB 2.0 7-Port Hub
Bus 001 Device 005: ID 04e8:2018 Samsung Electronics Co., Ltd WIS09ABGN
LinkStick Wireless LAN Adapter
Bus 001 Device 006: ID 046d: c312 Logitech, Inc. Deluxe 250 Keyboard
Bus 001 Device 007: ID 046d: c05a Logitech, Inc. Optical Mouse M90

In this case, device 005 is a Wi-Fi adapter manufactured by Samsung.


In order to access the current status of pi’s wireless network interface, we can write the command:
pi@raspberrypi ~ $ iwconfig
To search for an available wireless network, we write the following command:
pi@raspberrypi ~ $ sudo iwlist scan | grep ESSID

This command will display the wireless networks which are within reach. The next step
after recognizing the available wireless networks is that connecting the Wi-Fi to pi using the
commands below:
Sudo nano /etc/wpa_supplicant/wpa_supplicant.conf.

The above command is used to open the wpa-supplicant configuration file in nano. In
upcoming window we give the name of the ESSID (Wi-Fi name) and the password of it.

Network= {
ssid="The_ESSID_from_earlier"
psk="A_wifi_password"(if any)

22 | P a g e
Camera based traffic light control system on raspberry-pi 2016
}

pi@raspberrypi ~ $ sudo ifup wlan0

3.1.1.4. Raspberry pi model B GPIO pins

Electronic devices such as LEDs, displays, electric motors and other similar input or
output devices are connected to the pi through GPIO pins. In model B there are 26 pins in to rows
containing 13 pins each. The top row contains the even-numbered pins and the other row contains
the odd-numbered pins. That is, the first pin in the lower row is pin 1, for simplification it is
depicted by labeling ―P1‖ on the pi below.

5V - Groun GPIO GPIO GPIO - GPIO GPIO - GPIO GPIO GPIO7


d
14 15 18 23 24 25 8

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
Pin 2 4 6 8 10 12 14 16 18 20 22 24 26
Pin 1 3 5 7 9 11 13 15 17 19 21 23 25
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

3V3 GPIO GPIO GPIO - GPIO GPIO GPIO - GPIO GPIO GPIO -
0 4
1 17 21 22 10 9 11

Table 1: Pin configuration of a Raspberry Pi

Using pin 1 and pin 2, we can supply voltage + 3.3v and +5v for external electronic
devices connected to pi, respectively. The Pi limits the output of pin 1 to 50mA, while pin 2 allows
for a current draw that depends on the USB input current. If you power the Pi with a 1A power
supply, for example, you can draw up to 300mA from pin 2, because the Pi Model B needs 700mA
for itself. Pin 6 is assigned to share a common ground for electrical device connected to it. Pin 4, 9,

23 | P a g e
Camera based traffic light control system on raspberry-pi 2016

14, 20, and 25 are reserved for future enhancements. Other remaining pins are General-Purpose
Input/output (GPIO).

3.1.2. USB Web Camera

A webcam is a video camera that feeds or streams its image in real time to or through a
computer network. Unlike an IP camera (which connects using Ethernet or Wi-Fi), a webcam is
generally connected by a USB cable, or similar cable, or built into computer hardware, such as
laptops.
 Characteristics

Webcams are known for their low manufacturing cost and their high flexibility. Despite
the low cost, the resolution offered is rather impressive. They have also become a source of security
and privacy issue, as some built in webcams can be remotely activated by spyware.
The most popular use of webcam is the establishment of video links, permitting computers
to act as video phones. Other popular uses include security, computer vision, and video
broadcasting and for recording social videos. The video streams provided by webcams can be used
for a number of purposes.
In our case the USB web camera is used to capture the real time images of the road. We
use an INTEX IT-309WC web camera for this purpose. Its features and specifications are given in
table below:
Resolution 300K pixels to 16 mega pixels

CMOS Sensor High Quality Sensors

Focus Distance 4cm ~ infinity

Lens view angle 54 degree

I\O Interface USB 2.0

Image Control Brightness, Contrast, Hue, Saturation, Gamma, White Balance

Image Flip Both horizontal and vertical

Table 2: Feature and specification of INTEX IT-309WC web camera

24 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 10 an INTEX IT 309WC WEB CAMERA

The camera is installed on a stepper motor so that it has a capability of rotating in all four
directions to capture the image on each lane.

3.1.3. Stepper Motor

3.1.3.1. Basics on stepper Motor

A stepper motor is a special type of synchronous motor which is designed to rotate a


specific number of degrees for every electrical pulse received by its control unit. Typical steps are
7.5 or 15 degrees per pulse. These motors are used in many control systems, since the position of a
shaft or other piece of machinery (in our case an installed USB web camera) can be controlled
precisely with them.

25 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 11 Stepper motor and its associated control unit


Stepper motors work on the principle of electromagnetism. There is a soft iron or
magnetic rotor shaft surrounded by the electromagnetic stators. The rotor and stator have poles
which may be teethed or not depending upon the type of stepper. When the stators are energized the
rotor moves to align itself along with the stator (in case of a permanent magnet type stepper) or
moves to have a minimum gap with the stator (in case of a variable reluctance stepper). This way
the stators are energized in a sequence to rotate the stepper motor.
3.1.3.2. Types of stepper motor

There are basically three types of stepping motors; variable reluctance, permanent magnet
and hybrid. They differ in terms of construction based on the use of permanent magnets and/or iron
rotors with laminated steel stators.

 Permanent magnet stepper (can be subdivided into 'tin-can' and 'hybrid', tin-can
being a cheaper product, and hybrid with higher quality bearings, smaller step angle,
higher power density)
 Variable reluctance stepper
 Hybrid synchronous stepper

26 | P a g e
Camera based traffic light control system on raspberry-pi 2016

I. Permanent magnet Stepper motor:

The permanent magnet motor, also referred to as a "canstack" motor, has, as the name implies,
a permanent magnet rotor. It is a relatively low speed, low torque device with large step angles of
either 45 or 90 degrees. Its simple construction and low cost make it an ideal choice for non-
industrial applications, such as a line printer print wheel positioner.

Unlike the other stepping motors, the PM motor rotor has no teeth and is designed to be
magnetized at a right angle. Applying current to each phase in sequence will cause the rotor to
rotate by adjusting to the changing magnetic fields. Although it operates at fairly low speed the PM
motor has a relatively high torque characteristic to its axis.

Figure 12 permanent magnet stepper motor

II. Variable reluctance stepper:

The variable reluctance motor does not use a permanent magnet. As a result, the motor rotor
can move without constraint or "detent" torque. This type of construction is good in non-industrial
applications that do not require a high degree of motor torque, such as the positioning of a micro
slide.

27 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 13 variable reluctance stepper motor

The variable reluctance motor in the above illustration has three "stator pole sets" (A, B, C,),
set 15 degrees apart. Current applied to pole A through the motor winding causes a magnetic
attraction that aligns the rotor (tooth) to pole A. Energizing stator pole B causes the rotor to rotate
15 degrees in alignment with pole B. This process will continue with pole C and back to A in a
clockwise direction. Reversing the procedure (C to A) would result in a counterclockwise rotation.

III. Hybrid synchronous stepper:

Hybrid motors combine the best characteristics of the variable reluctance and permanent
magnet motors. They are constructed with multi-toothed stator poles and a permanent magnet rotor.
Standard hybrid motors have 200 rotor teeth and rotate at 1.80 step angles. Other hybrid motors are
available in 0.9ºand 3.6º step angle configurations. Because they exhibit high static and dynamic
torque and run at very high step rates, hybrid motors are used in a wide variety of industrial
applications.

28 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 14 hybrid stepper motor

In case of our project a stepper motor is used to rotate a camera in 90 degrees.

Coil 1a Coil 2a Coil 1b Coil 2b


Step 1 High High Low Low
Step 2 Low High High Low
Step 3 Low Low High High
Step 4 High Low Low High

Table 3: Pulse Sequence of a Bipolar Stepper Motor

3.1.4. Stepper Motor Driver

A microprocessor cannot drive a motor directly, since it cannot supply enough current. Instead,
there must be some interface circuitry so that the motor power is supplied from another power
source and only the control signals derive from the microprocessor. This interface circuitry can be
implemented by a circuit known as the H-bridge. An H-bridge merely consists of 4 switches
connected in topology of an H, where the motor terminals form the crossbar of the H.

29 | P a g e
Camera based traffic light control system on raspberry-pi 2016

In an H-bridge, the switches are opened and closed in a manner so as to put a voltage of one
polarity across the motor for current to flow through it in one direction or a voltage of the opposite
polarity, causing current to flow through the motor in the opposite direction for reverse direction.

For in case of our project we use an L293D IC for driving of a bipolar stepper motor purpose.
The L293D contains two H-bridges for driving small DC motors. It can also be used to drive
stepper motors because stepper motors are, in fact, two (or more) coils being driven in a sequence,
backwards and forwards.

The L293D is quadruple high-current half-H driver. The L293D is designed to provide
bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V.

The L293D chip has 16 pins as shown in figure below

Figure 15 a Typical L293D IC

30 | P a g e
Camera based traffic light control system on raspberry-pi 2016

3.1.5. Light Emitting Diodes (LED) for Traffic signaling

A Light emitting diode (LED) is essentially a p-n junction diode. When carriers are injected
across a forward-biased junction, it emits incoherent light. Most of the commercial LEDs are
realized using a highly doped n and a p Junction.

Advantages of using LEDs

 LEDs produce more light per watt than incandescent bulbs; this is useful in battery
powered or energy-saving devices.
 LEDs can emit light of an intended color without the use of color filters that traditional
lighting methods require. This is more efficient and can lower initial costs.
 The solid package of the LED can be designed to focus its light. In case of other light
sources such as incandescent often require an external reflector to collect light and direct
it in a usable manner.
 LEDs are ideal for use in applications that are subject to frequent on-off cycling.
 LEDs, being solid state components, are difficult to damage with external shock. So they
can have a relatively long useful life.
 LEDs light up very quickly. A typical red indicator LED will achieve full brightness in
microseconds
 LEDs can be very small and are easily populated onto printed circuit boards.

In our project LEDs are used to represent the three well known traffic signals (RED, Amber
and Green).

31 | P a g e
Camera based traffic light control system on raspberry-pi 2016

3.2. Programing Tools (software)

The following are the programming tool that we have used

 Raspbian OS
 Open CV
 Python
 SSH client
 VNC viewer

3.2.1. Raspbian OS

Raspbain is open source software from Unix-like family with ARM platform. It is also a free
operating system based on Debian optimized for the raspberry pi hardware. An operating system is
a set of basic programs and utilities that make our raspberry pi run. However, Raspbian provides
more than a pure OS: it comes with over 35,000 packages precompiled software bundled in a nice
format for easy installation on your raspberry pi.

3.2.2. Python

Python is a great object-oriented, interpreted, and interactive programming language. It is often


compared (favorably of course) to Lisp, Tcl, Perl, Ruby, C#, Visual Basic, Visual Fox Pro, Scheme
or Java... and it's much more fun.

Python combines remarkable power with very clear syntax. It has modules, classes, exceptions,
very high level dynamic data types, and dynamic typing. There are interfaces to many system calls
and libraries, as well as to various windowing systems. New built-in modules are easily written in C
or C++ (or other languages, depending on the chosen implementation). Python is also usable as an
extension language for applications written in other languages that need easy-to-use scripting or
automation interfaces.

32 | P a g e
Camera based traffic light control system on raspberry-pi 2016

 Python (programming language)

Python is a widely used high-level, general-purpose, interpreted, dynamic programming


language. Its design philosophy emphasizes code readability, and its syntax allows programmers to
express concepts in fewer lines of code than would be possible in languages such as C++ or Java.
The language provides constructs intended to enable clear programs on both a small and large scale.

Python supports multiple programming paradigms, including object-oriented, imperative and


functional programming or procedural styles. It features a dynamic type system and automatic
memory management and has a large and comprehensive standard library.

3.2.3. Open CV

Open CV (Open Source Computer Vision) is a library of programming functions mainly aimed
at real-time computer vision, originally developed by Intel's research center in Nizhny Novgorod
(Russia), later supported by Willow Garage and now maintained by Itseez. The library is cross
platform and free for use under the open-source BSD license. Open CV is written in C++ and its
primary interface is in C++, but it still retains a less comprehensive though extensive older C
interface. There are bindings in Python, Java and MATLAB/OCTAVE. All of the new
developments and algorithms in Open CV are now developed in the C++ interface.

 History

Officially launched in 1999, the Open CV project was initially an Intel Research initiative to
advance CPU-intensive applications, part of a series of projects including real-time ray tracing and
3D display walls. The main contributors to the project included a number of optimization experts in
Intel Russia, as well as Intel’s Performance Library Team. In the early days of Open CV, the goals
of the project were described as:

Advance vision research by providing not only open but also optimized code for basic vision
infrastructure. No more reinventing the wheel.

33 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Disseminate vision knowledge by providing a common infrastructure that developers could


build on, so that code would be more readily readable and transferable.

Advance vision-based commercial applications by making portable, performance-optimized


code available for free—with a license that did not require to be open or free themselves.

Open CV's application areas include:

 2D and 3D feature toolkits


 Ego motion estimation
 Facial recognition system
 Gesture recognition
 Human–computer interaction (HCI)
 Mobile robotics
 Motion understanding
 Object identification
 Segmentation and recognition
 Stereopsis stereo vision: depth perception from 2 cameras
 Structure from motion (SFM)
 Motion tracking
 Augmented reality

To support some of the above areas, Open CV includes a statistical machine learning library
that contains:

 Boosting
 Decision tree learning
 Gradient boosting trees
 Expectation-maximization algorithm
 k-nearest neighbor algorithm

34 | P a g e
Camera based traffic light control system on raspberry-pi 2016

 Naive Bayes classifier


 Artificial neural networks
 Random forest
 Support vector machine (SVM)

3.2.4. VNC server

In computing, virtual network computing (VNC) is graphical desktop sharing system that uses
the remote frame buffer protocol (RFB) to remotely control another computer. It transmits the
keyboard and mouse events from one computer to another, relaying the graphical screen updates
back in the other direction, network.

VNC is a platform-independent. There are clients and servers for many GUI based operating
systems and for java. Multiple clients may connect to a VNC as the same time. Popular uses for this
technology includes remote technical support and accessing files on ones work computer from
one’s home computer, or vice versa. There are number of variants of VNC server which offer their
own particular functionality.

3.2.5. SSH client

SSH server is designed for all windows NT-series OS. Supported platforms include all desktop
and server versions of windows starting from windows XP and windows server 2003 to the most
recent including windows 8.X and windows 10 both in 32 and 64 bit versions of windows.

Bitvise SSH server supports the following SSH service:

 Secured remote access via console


 Secure remote access via GUI
 Secure file transfer using SFTP and SCP
 Secure, effortless Git integration
 Secure TCP/IP connection tunneling

35 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Chapter Four

4. DESIGN AND METHODOLOGY

Stepper Camera
Motor

RASPBERRY-PI Hardware
Motor
Software Based Image
Driver processing and Traffic
Density Estimation
using Raspberry Pi
Power Supply

LED control using


Raspberry pi

Traffic light signal

Figure 16 System General Block diagram

36 | P a g e
Camera based traffic light control system on raspberry-pi 2016

4.1. Controlling a stepper motor with raspberry pi

A stepper motor is chosen to hold a camera and rotate in a designed fashion. The stepper motor
rotates 90 degrees and then stops, so that the camera takes a picture of that specific lane and sends
the image for further processing. After that the stepper motor rotates to next lane which is 90
degrees from the current lane.

We have selected OKI KBL42LLB560B model stepper motor for our specific application
(rotating a 200g weighting camera).

This stepper motor has voltage rating of 6.4V and 7.5 stepping degree. So a total of 12 steps
makes the stepper motor rotates 90 degrees.

4.1.1. Stepper motor driver and Indexer (controller)

L293D IC is used as a stepper motor driver.

Figure 17 L293D motor driver connection with bipolar stepper motor

37 | P a g e
Camera based traffic light control system on raspberry-pi 2016

4.1.2. Raspberry pi as stepper motor controller:

A stepper motor is controlled by multiple electromagnetic coils able to work individually to


provide a precision rotation of the motor divided into steps. Stepper motor can be controlled with
and without feedback.

The Raspberry pi model B generally has 26 pins arranged in two rows containing 13 pins
each. The top row contains the even-numbered pins, and the other row contains the odd-numbered
pins of which 20 of the can be used as GPIO.

By configuring any four of the GPIO pins of the pi, to generate a square wave output, we can
control the stepper motor using through a driver. In order to do so a python script would be a good
coding environment to generate the square wave on the pi GPIOs’.

The code used is shown below.

import time

import sys

import RPi.GPIO as GPIO

GPIO.setmode (GPIO.BCM)

Controlpin= [17, 18, 21, 22]

for pin in controlpin:

GPIO.setup (pin, GPIO.OUT)

GPIO.output (pin, 0)

Seq= [[1, 0, 1, 0],

[0, 1, 1, 0],

38 | P a g e
Camera based traffic light control system on raspberry-pi 2016

[0, 1, 0, 1],

[1, 0, 0, 1]]

for steps in range (3):

for fullstep in range (4):

for pin in range (4):

GPIO.output (controlpin [pin], seq [fullstep][pin])

time.sleep (0.05)

GPIO.cleanup ()

In this python script the stepper motor is controlled to move 12 steps in effect rotating 90
degrees.

4.1.3. Setting the connection:

Using a breads board and jumper wires we have built the stepper controlling circuit as shown
below.

39 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 18 Overall circuit connection for controlling the stepper motor.

4.2. Capturing real time image using webcam and Open CV

4.2.1. Installing Open CV on Raspberry pi

Open CV is an open source image processing library created by Intel. Which is available for
C/C++/Python?

The installation process of Open CV and python on the pi generally take 8-12 hours.

4.2.2. Open CV installation steps

Step 1: Expand file system

The first thing you should do is expand your file system to include all available space on your
micro-SD card:

40 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 19 expanding the file system in raspberry pi

Once prompted, you should select the first option, “1. Expand File System”, hit Enter on your
keyboard, arrow down to the “<Finish>” button, and then reboot your Pi.

Step 2: Install dependencies

The next step is to update and upgrade any existing packages:

You then need to install some developer tools, including CMake, which helps us configure
the Open CV build process:

41 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Next, we need to install some image I/O packages that allow us to load various image file
formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:

Just as we need image I/O packages, we also need video I/O packages. These libraries allow
us to read various video file formats from disk as well as work directly with video streams:

The Open CV library comes with a sub-module named high GUI which is used to display
images to our screen and build basic GUIs. In order to compile the high GUI module, we need to
install the GTK development library:

Many operations inside of Open CV (namely matrix operations) can be optimized further by
installing a few extra dependencies

Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile Open
CV with Python bindings:

42 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Step 3: Download the Open CV source code

Now that we have our dependencies installed, let’s grab the archive of Open CV from the
official Open CV repository.

Step 4: Python 2.7 or Python 3

Before we can start compiling Open CV on our Raspberry Pi 3, we first need to install pip, a
Python package manager

Installing NumPy on your Raspberry Pi

Our only Python dependency is NumPy, a Python package used for numerical processing:

Step 5: Compile and Install Open CV

We are now ready to compile and install Open CV! We can setup our build using CMake:

Finally, we are now ready to compile Open CV:

43 | P a g e
Camera based traffic light control system on raspberry-pi 2016

From there, all you need to do is install Open CV 3 on your Raspberry Pi

Here we have finished the installation process and we can build different open CV project from
now on

4.2.3. Capturing real time image

we have web camera which is mounted on our stepper motor, so that it can rotates in all
four directions and connected with our raspberry pi(with a USB connection)in order to capture real
time image and send the image for further image processing.

Open CV and python gives the platform to capture the image using the webcam.

The python code shown below captures image and stores it in the home directory with a
name of real_time.jpg.

camera_port = 0

camera = cv2.VideoCapture(camera_port)

def get_image():

retval, im = camera.read()

return im

temp = get_image()

print("Taking image...")

camera_capture = get_image()

file = "/home/pi/real_time.jpg"

cv2.imwrite(file, camera_capture)

del(camera)

44 | P a g e
Camera based traffic light control system on raspberry-pi 2016

4.3. Image processing using Raspberry pi


Here is the block diagram of our proposed algorithm for the image processing section. The
block diagram gives an overview of how traffic will be controlled using image processing. The
algorithm implemented is discussed in detail in the next pages.

 Reference image  Captured Image

 RGB to gray  RGB to gray


conversion conversion

 Image Resizing  Image Resizing

 Image  Image
enhancement enhancement

 Object detection
 Object
 (mean square
detection
error)
 (mean square
error)

 Image Matching

 Timing Allocation

Figure 20 the flow chart of image processing

45 | P a g e
Camera based traffic light control system on raspberry-pi 2016

The acquired image is converted to grey scale image for further processing. The grey scale
image is then converted to a binary image that contains only two colors, black and white. This
image is known as the threshold image. The main purpose of threshold the image is a radical
reduction of information in order to simplify further processing. The threshold image is then
complemented for further image processing.

4.3.2. Image cropping

The desired portion of the image is retained and the rest is cropped. Only the lane at which
there is an incoming traffic at the junction is to be processed. Hence the image is cropped to select
that section of the lane.

4.3.3. Image Enhancement

In this process the images are adjusted in such a way that the results are more suitable for
further processing. In this, the obtained image is converted into a greyscale image.

4.3.4. Thresholding

Thresholding is transforming the greyscale image into black and white image (binary:
white=1, black=0). The main purpose of thresholding is a radical reduction of information in order
to simplify further processing. White color is assigned to all the pixels that have luminosity greater
than the threshold level and the others as black.

4.3.5. Mean square error

Let’s take a look at the Mean Squared error equation:

46 | P a g e
Camera based traffic light control system on raspberry-pi 2016

MSE is a mechanism to calculate the mean difference of two images, which takes two
images and returns the mean square error of the two images. It’s important to note that a value of 0
for MSE indicates perfect similarity. A value greater than one implies less similarity and will
continue to grow as the average difference between pixel intensities increases as well.

4.3.6. Car Detection

Object detection is a mechanism of detecting different objects from a given image. Simply
put, object detection in Open CV is one of machine learning. Haar like feature is a good and
recommended way of training your own object detection xml file.

4.4. Changing the duration of traffic light

The duration of the traffic signal is monitored as follows:


 The camera mounted on stepper motor rotates in clockwise direction and stops to take
pictures of each lane. The clicked pictures are sent to RSAPBERRY-PI board for image
processing.
 Then the camera rotates to the next lane and repeats the above step.
 The images are processed in RASPBERRY-PI board and the priority decided as per its
traffic density.
 The lane with the highest relative traffic density is given longer time duration to cross the
junction, so that the traffic in that lane can pass and reduce the congestion. The other lanes
have their green signals as per their decreasing priorities.
 This process repeats and the duration of green signal given to each lane keeps adjusting
itself on every rotation of the camera.

47 | P a g e
Camera based traffic light control system on raspberry-pi 2016

ALGORITHM FOR CONTROLLING THE GREEN SIGNALING TIME

Number of cars Time allotted for green signal(sec)

1-5 30

6-10 45

11-15 60

15-20 90

>20 >90

Table 4 : Algorithm for controlling the green signaling time


This algorithm may work differently with the situation in hand. So,

48 | P a g e
Camera based traffic light control system on raspberry-pi 2016

The general flow chart of the overall or the implemented system is given in figure below:

Start

Stepper motor on to
rotate a webcam

Camera on to acquire
image

Image processing

Vehicle density

Green signaling time


allocation

End

Figure 21: general system flow chart the design system

49 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Chapter Five

5. Hardware implementation

This chapter discusses the system development tools and hardware components applied for
implementing the camera based automatic Traffic light Controlling System using raspberry pi.
Moreover, the prototype, hardware components and testing of the system are also detailed in this
chapter.

5.1. Development Tools

To make the system close to reality, different tools are applied. The programming language,
the hardware components and the testing mechanisms are major parts of the development tools.

The development tools are:-

 Open CV and python for programming language


 RASPBERRY PI as a microcontroller
 H-bridge as a motor driver
 INTEX IT 309-WC as a web camera
 LEDs as a traffic light signal
 Bi- polar Stepper motor for stepping purpose

5.2. Working environment

The road selected for the prototype of this camera based Traffic light Controlling System
using raspberry pi is a 4 way junction road. Therefore it has 3 colored traffic lights which are
basically LEDs at each of the four directions.

Basically we can say that the system works based on the dynamic traffic light condition.
Whenever one of the roads is under the green time operation the system will stay in its previous

50 | P a g e
Camera based traffic light control system on raspberry-pi 2016

state and when the system is in yellow state the motor rotates and the camera captures the current
traffic congestion of the remaining lanes. Then the system will set the green signal according the
processed image which is sent from the raspberry pi. Afterward when the time of the current road
expires, the permission will be given for the next road according to the traffic congestion of that
specific lane.

Generally the system checks the traffic state of the four lanes and gives enough green time
signals to the congested lane. This process repeats in a continuous loop.

5.3. Prototype
The system categorizes three different standard lights (GREEN, RED, YELLOW) into 4
groups. Every group has a set of traffic lights (red, green, and yellow) for signaling purpose.

The system prototype is basically the real time system which consists

 Cars,
 Properly scaled road,
 12 LEDs representing the real time traffic signal,
 Microcontroller(RASPBERRY PI ),
 Web camera and stepper motor

Mean Square Error(m) Time allotted for green signal(sec)

0-0.5 10

0.5-1 20

1-1.5 30

1.5-2 40

2-2.5 50

2.5-3.5 60

>3.5 90

51 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Table 5: Time allocation algorithm for prototype

Figure 22: typical designed prototype of a 4 way road

5.4. Load Detecting


As it is stated earlier, we have 4 groups of light. The congestion or load is detected by
processing the real time image and specific time is allotted to each lane by the microcontroller
(RASPBERRY PI).the load detecting mechanism is smart and automatic which is different from the
conventional way of controlling traffic light.

52 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Chapter Six

6. Results, Discussions, Conclusion and Recommendation

6.1. Results

The following results have been succeeded in our project.

 Controlling stepper motor using raspberry pi in both direction.


 Interfacing camera with raspberry pi and image acquisition
 Controlling LEDs using raspberry pi
 Detecting the density of vehicles.
So generally the prototype result of our system combing the above results together is shown
below.

53 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Figure 23 general result of the system

54 | P a g e
Camera based traffic light control system on raspberry-pi 2016

6.2. Discussion

Though there are lots of traffic lights in Ethiopia, all of them are controlled conventional traffic
lights. Not a single of them are working in a different way than operating by the pre-decided fixed
time.

Traffic congestion is now a days an unavoidable phenomena due to the rapid growth of traffic
volume and reduction of road capacity due to different reasons. Due to this traffic congestion
measures should be prepared before it is out of control.

In short traffic congestion causes are listed below:-

 Traffic Incidents (Crash)


 Work Zones
 Damaged Traffic Signals
 Reduction in road capacity
 Vehicle breakdown
 On Street Parking

This approach has its own drawback. It’s not complex to see that the very technique that is
built to solve the problem sometimes becomes a source of another problem, traffic conjunction.
How many times have we been sitting at a stoplight for 2, 3 minutes, no cars coming from any other
direction?

Obviously, some studies regarding these lights might be taken place in different domains;
but still the statistical way of handling the problem is the only technique chosen for materialization
at Mesqel Square.

However many approaches should be tried in a City like Addis Ababa. And in view of the
fact that no technique is perfect by itself, using different types of techniques might offer us many
advantages. For instance, the strong side of one technique could give some benefit in suppressing

55 | P a g e
Camera based traffic light control system on raspberry-pi 2016

the shortcoming of the other. And one of these other techniques is controlling the traffic light
system dynamically i.e. taking the real time situation in consideration to allocate the signaling time.

So our project, camera based automatic traffic light control system is based on the real time
situation which considers allocating signaling time based on the real time traffic congestion to solve
the current problem of traffic light controlling system which is basically based on fixed time
allocation.

For this reason, it can be said that the new system has a better opportunity in recognizing the
good feature and in overcoming the limitation of the current system. Moreover, this is the basic
feature that makes it to be an improved system than the conventional traffic light controlling
system.

So, this automatic camera based Traffic Light Controlling System can be one of those
possible options in automating the traffic lights we find in the City.

6.3. Conclusions
The following conclusions can be drawn from the implemented system:

 Traffic volume distribution and congestion are similar with the theoretical principle.
Traffic volume has the morning and evening peak period for almost all approaches on
the four intersections or junctions. Similarly, traffic congestion during morning and
evening peak period are congested and during midday it is relatively uncongested.so our
system solves and is real to consider the situation in real time.

 Raspberry pi is a mini computer which is in our system used as microcontroller to


 Allocate specific time period of the traffic flag.
 Control the stepper motor

 Image processing is a method to convert an image into digital form and perform some
operations on it, in order to get an enhanced image or to extract some useful information

56 | P a g e
Camera based traffic light control system on raspberry-pi 2016

from it. Image processing has three phases i.e. Image acquisition (pre-processing),
Image processing (image enhancement and display) and information extraction.
 There are different image processing tools and environments like MATLAB and Open
CV. To implement our proposed system we choose Open CV due to

 Faster in competition
 Low system resources i.e. low RAM usage
 Cost free( free software)

The main purpose or objective of this project was to solve the current traffic load problem
and in terms of achieving the specific objective

 Interfacing a web camera with and controlling stepper motor using RASPBERRY-PI
was successful.
 Image processing using RASPBERRY-PI and appropriate image processing software or
tool for vehicle counting was also successful.
 Design a system that decides the timing of the traffic light based on the processed image
and Prototyping the overall system was successful.

6.4. Recommendations

Through the course of conducting this project, it was learned that camera based automatic
Traffic Light Controlling System performs better than the current conventional system. But there
are rooms for improvement to incorporate additional features related with different aspects of the
system. These are based on the shortcomings of this proposed system.

To further enhance the results of this study the following recommendations are formulated:

 Develop an accurate car detection xml file

57 | P a g e
Camera based traffic light control system on raspberry-pi 2016

 Further research and study should be conducted to extend all aspects of this research
like taking in to considerations in the case of night shift.
 To override the light management of the system in case of arrival of emergency
vehicles little modification of the system must be considered.

58 | P a g e
Camera based traffic light control system on raspberry-pi 2016

References

1. http://en.wikipedia.org/wiki/Traffic_light. Retrieved March 28, 2010


2. Marco Wiering. Intelligent Traffic Light Control. Retrieved March 28, 2010, from
http://www.ercim.eu/publication/Ercim_News/enw53/wiering.html
3. Intelligent traffic light control using image processing by RAHISHET, APARAJITA
SAHOO, APARNA INDORE, VAIBHAVDESHMUKH, PUSHPA U S. University Of
Rodrigues Institute of Technology, Vashi
4. International Journal of Advanced Electrical and Electronics Engineering, (IJAEEE)ISSN
(Print): 2278-8948, Volume-2, Issue-5, 2013
5. International Journal of Innovative Research in Computer and Communication
Engineering.Vol.2, Special Issue 5, October 2014
6. Priyanka Khanke, Prof. P. S. Kulkarni, "A Technique on Road Tranc Analysis using Image
Processing", Vol. 3 Issue 2, February 2014.
7. Raspberry pi, Web page: https://en.wikipedia.org/wiki/Raspberry_Pi
8. S.Lokesh, T.Prahlad Reddy, and an Adaptive Traffic Control System Using Raspberry PI
Vol 3(6): June, 2014.
9. International Journal of Engineering Sciences & Research Technology Lokesh, 3(6): June,
2014
10. Khalid A. S. Al-Khateeb, Jaiz A.Y. Johari and Wajdi F. Dynamic Traffic Light, Sequence
Algorithm Using RFID. Al-Khateeb, Department of Electrical and Computer Engineering,
Faculty of Engineering, International Islamic University Malaysia, Kuala Lumpur,
Malaysia.
11. Kok Khiang Tan, 1Marzuki Khalid and Rubiyah Yusof, INTELLIGENT TRAFFIC
LIGHTS
CONTROL BY FUZZY LOGIC, Artificial Intelligence Center Universiti Teknologi
Malaysia.

59 | P a g e
Camera based traffic light control system on raspberry-pi 2016

12. Lei, Leong C. (2003). Traffic signal control system employing universal co-ordinated time
(UTC) of GPS as time base. Retrieved March 28, 2010, from
http://www.computer.org/portal/web/csdl/doi/10.1109/ ICETET.2009.76
13. Levinson, D. (2003). The value of advanced traveler information systems for route choice.
Transportation Research Part C: Emerging Technologies, 11-1:75–87.
14. Findler, N. and Stapp, J. (1992). A distributed approach to optimized control of street traffic
signals. Journal of Transportation Engineering, 118-1:99–110.
15. Tavladakis, K. and Voulgaris, N. C. (1999). Development of an autonomous adaptive traffic
control system. In ESIT ’99 - The European Symposium on Intelligent Techniques.
16. Liu, H. L., Oh, J.-S., and Recker, W. (2002). Adaptive signal control system with on-line
performance measure. In 81st Annual Meeting of the Transportation Research Board.
17. Malik Tubaishat, Yi Shang and Hongchi Shi. Adaptive Traffic Light Control with Wireless
Sensor Networks. Department of Computer Science, University of Missouri – Columbia.

60 | P a g e
Camera based traffic light control system on raspberry-pi 2016

Appendix
import cv2

import time

import sys

import RPi.GPIO as GPIO

import numpy as np

import skimage.measure import structural_similarity as ssim

import matplotlib.pyplot as plt

GPIO.setmode(GPIO.BCM)

# Assign constants for the traffic light GPIO pins

red_led_A = 15

yellow_led_A = 7

green_led_A = 0

red_led_B = 24

yellow_led_B = 8

green_led_B = 1

red_led_C = 10

yellow_led_C = 23

green_led_C = 4

red_led_D = 9

yellow_led_D = 25

green_led_D = 14

# Configure the GPIO to BCM and set the pins to output mode

GPIO.setmode(GPIO.BCM)

GPIO.setup(red_led_A, GPIO.OUT)

61 | P a g e
Camera based traffic light control system on raspberry-pi 2016

GPIO.setup(yellow_led_A, GPIO.OUT)

GPIO.setup(green_led_A, GPIO.OUT)

GPIO.setup(red_led_B, GPIO.OUT)

GPIO.setup(yellow_led_B, GPIO.OUT)

GPIO.setup(green_led_B, GPIO.OUT)

GPIO.setup(red_led_C, GPIO.OUT)

GPIO.setup(yellow_led_C, GPIO.OUT)

GPIO.setup(green_led_C, GPIO.OUT)

GPIO.setup(red_led_D, GPIO.OUT)

GPIO.setup(yellow_led_D, GPIO.OUT)

GPIO.setup(green_led_D, GPIO.OUT)

# Define function to control 1st traffic light

def trafficState_A(red, yellow, green):

GPIO.output(red_led_A, red)

GPIO.output(yellow_led_A, yellow)

GPIO.output(green_led_A, green)

#Define function to control 2nd traffic light

def trafficState_B(red, yellow, green):

GPIO.output(red_led_B, red)

GPIO.output(yellow_led_B, yellow)

GPIO.output(green_led_B, green)

#Define function to control 3rd traffic light

def trafficState_C(red, yellow, green):

62 | P a g e
Camera based traffic light control system on raspberry-pi 2016

GPIO.output(red_led_C, red)

GPIO.output(yellow_led_C, yellow)

GPIO.output(green_led_C, green)

#Define function to control 4th traffic light

def trafficState_D(red, yellow, green):

GPIO.output(red_led_D, red)

GPIO.output(yellow_led_D, yellow)

GPIO.output(green_led_D, green)

# for the motor rotating 90 degrees

#{

def motor():

controlpin=[17,18,11,22]

for pin in controlpin:

GPIO.setup(pin,GPIO.OUT)

GPIO.output(pin,0)

seq=[[1,0,1,0],

[0,1,1,0],

[0,1,0,1],

[1,0,0,1]]

for step in range(3):

for fullstep in range(4):

for pin in range(4):

GPIO.output(controlpin[pin],seq[fullstep][pin])

time.sleep(0.05)

#GPIO.cleanup()

#}

# for image capturing

63 | P a g e
Camera based traffic light control system on raspberry-pi 2016

#{

def image_capture():

motor()

camera_port = 0

camera = cv2.VideoCapture(camera_port)

def get_image():

retval, im = camera.read()

return im

temp = get_image()

print("Taking image...")

camera_capture = get_image()

file = "/home/pi/realtime.jpg"

cv2.imwrite(file, camera_capture)

del(camera)

#}

# FOR IMAGE PROCESSING AND COMPARING

#{

def mse(imageA, imageB):

err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)

err /= float(imageA.shape[0] * imageA.shape[1])*10000

return err

def compare_images(imageA, imageB):

m = mse(imageA, imageB)

64 | P a g e
Camera based traffic light control system on raspberry-pi 2016

print m

while true: #General loop

image_capture()

time.sleep(3)

reference = cv2.imread("reference1.jpg")

realtime = cv2.imread("realtime.jpg")

# convert the images to grayscale

reference1 = cv2.cvtColor(reference, cv2.COLOR_BGR2GRAY)

realtime = cv2.cvtColor(realtime, cv2.COLOR_BGR2GRAY)

# compare the images

compare_images(reference1, realtime)

#}

# density decision algorhtim

#{

if(0<m<0.5):

time=5

else if (0.5<m<1):

time=10

else if (1<m<1.5):

time=15

else if (1.5<m<2):

time=20

else if (2<m<3):

time=25

else:

65 | P a g e
Camera based traffic light control system on raspberry-pi 2016

time=30

#}

#FOR LIGHT CONTROL

#{

# A's Light Green for time seconds other light Lights Red

trafficState_A(0,0,1)

trafficState_B(1,0,0)

trafficState_C(1,0,0)

trafficState_D(1,0,0)

time.sleep(time)

# A's Light Yellow for 3 seconds other Lights Red

trafficState_A(0,1,0)

trafficState_B(1,0,0)

trafficState_C(1,0,0)

trafficState_D(1,0,0)

#}

# for lane 2

image_capture()

time.sleep(3)

reference = cv2.imread("reference2.jpg")

realtime = cv2.imread("realtime.jpg")

# convert the images to grayscale

reference2 = cv2.cvtColor(reference, cv2.COLOR_BGR2GRAY)

66 | P a g e
Camera based traffic light control system on raspberry-pi 2016

realtime = cv2.cvtColor(realtime, cv2.COLOR_BGR2GRAY)

# compare the images

compare_images(reference2, realtime)

#}

# density decision algorhtim

#{

if(0<m<0.5):

time=5

else if (0.5<m<1):

time=10

else if (1<m<1.5):

time=15

else if (1.5<m<2):

time=20

else if (2<m<3):

time=25

else:

time=30

#}

#FOR LIGHT CONTROL

#{

# B's Light Red for 10 seconds other Lights Green

trafficState_A(1,0,0)

trafficState_B(0,0,1)

trafficState_C(1,0,0)

trafficState_D(1,0,0)

67 | P a g e
Camera based traffic light control system on raspberry-pi 2016

time.sleep(time)

# B's Light Yellow for 3 seconds other Lights Red

trafficState_A(1,0,0)

trafficState_B(0,1,0)

trafficState_C(1,0,0)

trafficState_D(1,0,0)

#}

# for lane 3

image_capture()

time.sleep(3)

reference = cv2.imread("reference3.jpg")

realtime = cv2.imread("realtime.jpg")

# convert the images to grayscale

reference3 = cv2.cvtColor(reference, cv2.COLOR_BGR2GRAY)

realtime = cv2.cvtColor(realtime, cv2.COLOR_BGR2GRAY)

# compare the images

compare_images(reference3, realtime)

#}

# density decision algorhtim

#{

if(0<m<0.5):

time=5

else if (0.5<m<1):

time=10

else if (1<m<1.5):

time=15

68 | P a g e
Camera based traffic light control system on raspberry-pi 2016

else if (1.5<m<2):

time=20

else if (2<m<3):

time=25

else:

time=30

#}

#FOR LIGHT CONTROL

#{

# C's Light Red for 10 seconds other Lights Green

trafficState_A(1,0,0)

trafficState_B(1,0,0)

trafficState_C(0,0,1)

trafficState_D(1,0,0)

time.sleep(time)

# C's Light Yellow for 3 seconds other Lights Red

trafficState_A(1,0,0)

trafficState_B(1,0,0)

trafficState_C(0,1,0)

trafficState_D(1,0,0)

#}

# for lane 3

image_capture()

time.sleep(3)

reference = cv2.imread("reference4.jpg")

realtime = cv2.imread("realtime.jpg")

69 | P a g e
Camera based traffic light control system on raspberry-pi 2016

# convert the images to grayscale

reference4 = cv2.cvtColor(reference, cv2.COLOR_BGR2GRAY)

realtime = cv2.cvtColor(realtime, cv2.COLOR_BGR2GRAY)

# compare the images

compare_images(reference4, realtime)

#}

# density decision algorhtim

#{

if(0<m<0.5):

time=5

else if (0.5<m<1):

time=10

else if (1<m<1.5):

time=15

else if (1.5<m<2):

time=20

else if (2<m<3):

time=25

else:

time=30

#}

#FOR LIGHT CONTROL

#{

# D's Light Red for 10 seconds other Lights Green

trafficState_A(1,0,0)

trafficState_B(1,0,0)

trafficState_C(1,0,0)

70 | P a g e
Camera based traffic light control system on raspberry-pi 2016

trafficState_D(0,0,1)

time.sleep(time)

# B's Light Yellow for 3 seconds other Lights Red

trafficState_A(1,0,0)

trafficState_B(1,0,0)

trafficState_C(1,0,0)

trafficState_D(0,1,0)

#}

GPIO.cleanup()

71 | P a g e

You might also like