Friday, 8 February 2019

Traffic Counting System Based on OpenCV


Traffic Counting System Based on OpenCV and Python:
Introduction:
Traffic counts, speed and vehicle classification are fundamental data for a variety of transportation projects ranging from transportation planning to modern intelligent transportation systems Traffic Monitoring and Information Systems related to vehicles cascade. In addition to vehicle counts, a much larger set of traffic parameters such as vehicle classifications, lane changes, parking areas etc., can be measured in such type of systems. In large metropolitan areas, there is a need for data about vehicle classes that use a particular highway or a street.

Hardware Used:
1. Raspberry Pi:
This is the latest version of raspberry pi. In this we have inbuilt Bluetooth and wi-fi, unlike previously we have to use Wi-Fi dongle in one of its usb port. There are total 40 pins in RPI3. Of the 40 pins, 26 are GPIO pins and the others are power or ground pins (plus two ID EEPROM pins.) There are 4 USB Port and 1 Ethernet slot, one HDMI port, 1 audio output port and 1 micro usb port and also many other things you can see the diagram on right side. And also we have one micro sd card slot wherein we have to installed the recommended Operating system on micro sd card. There are two ways to interact with your raspberry pi. Either you can interact directly through HDMI port by connecting HDMI to VGA cable, and keyboard and mouse or else you can interact from any system through SSH(Secure Shell). (For example in windows you can interact from putty ssh.) Figure is given below.



2) USB Cameras
USB Cameras are imaging cameras that use USB 2.0 or USB 3.0 technology to transfer image data. USB Cameras are designed to easily interface with dedicated computer systems by using the same USB technology that is found on most computers. The accessibility of USB technology in computer systems as well as the 480 Mb/s transfer rate of USB 2.0 makes USB Cameras ideal for many imaging applications. An increasing selection of USB 3.0 Cameras is also available with data transfer rates of up to 5 Gb/s.




Installation steps for Python OpenCv on Raspberry Pi:

1) sudo apt-get update

2) sudo apt-get upgrade

3) sudo apt-get install build-essential

4) sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

5)sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

6) sudo apt-get install python-opencv

7) sudo apt-get install python-matplotlib


Project Description:
In this project we are using one raspberry pi and one usb camera.This project is used for detecting and counting vehicals.This project runs on two different modes we need to give an option of activating camera for real-time and detecting vehicles for pre-recorded mode. The project interface of the system which provides
several functions as given below:

1) Activating camera in color mode and grayscale mode

2) Detect vehicles for real time

3) Detect vehicles from pre-recorded video stream (Videos are stored in standard .avi format)

Code of project:

import cv2
print(cv2.__version__)

cascade_src = 'cars.xml'
video_src = 'dataset/video2.avi'
#video_src = 'dataset/video2.avi'

cap = cv2.VideoCapture(video_src)
car_cascade = cv2.CascadeClassifier(cascade_src)

while True:
    ret, img = cap.read()
    if (type(img) == type(None)):
        break
   
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   
    cars = car_cascade.detectMultiScale(gray, 1.1, 1)

    for (x,y,w,h) in cars:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)     
   
    cv2.imshow('video', img)
    print "Found "+str(len(cars))+" car(s)"
    b=str(len(cars))
    a= float(b)
    if a>=5:
        print ("more traffic")
    else:
        print ("no traffic")   
    if cv2.waitKey(33) == 27:
        break

cv2.destroyAllWindows()

0 comments:

Post a Comment

if you have any doubt please let me know