Coder Social home page Coder Social logo

many-facedgod / vehicle-detection-and-classification Goto Github PK

View Code? Open in Web Editor NEW
21.0 1.0 14.0 222.36 MB

Vehicle detection and classification on a video from an Indian Highway.

Python 72.88% MATLAB 2.58% C++ 24.53%
vehicle-detection image-processing haar-cascade

vehicle-detection-and-classification's Introduction

Vehicle Detection and Classification

A project by Tanmaya Dabral, Bhargav Kanuparthi and Sabrinath Harilal as a part of the Image Processing course (BITS F311) offered at BITS Pilani, Hyderabad Campus. The aim of the project was to detect and classify vehicles from a video of an Indian highway, the classification labels being "Heavy" and "Light". Both supervised and unsupervised approaches were tried.

Note: Since committing thousands of frames every time they are processed is a hassle, only zipped versions are committed.


Method used

The vehicle blobs were first obtained by subtracting the images from the median image followed by elementary cleaning operations. For the unsupervised approach, the blobs were clustered based on their area and their median width to median height ratio. For the supervised approach, a few high-precision low recall samples of both classes were obtained using blob statistics. Then, a Haar cascade classifier was trained for each class using these training examples.


Results

The unsupervised clustering performs reasonably well on both classes. However, the supervised approach gives far too many false negatives for the heavy class to be acceptable. The "Light" class however, is predicted with greater accuracy by the Haar cascade classifier.

Clustering examples:

Haar cascades examples (Two-Wheelers):

Haar cascades examples (Four-Wheelers)::

vehicle-detection-and-classification's People

Contributors

many-facedgod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vehicle-detection-and-classification's Issues

Vehicle classification

Bro i want to classify vehicles but unable to do so.this is my code plz help me in classification as light and heavy vehicles.plz help me bro....m

import cv2

import numpy as np

min_contour_width=40  #40

min_contour_height=40  #40

offset=10       #10

line_height=550 #550

matches =[]

vehicles=0

def get_centroid(x, y, w, h):

    x1 = int(w / 2)

    y1 = int(h / 2)

    cx = x + x1

    cy = y + y1

    return cx,cy

    #return (cx, cy)

       

#cap = cv2.VideoCapture(0)

cap = cv2.VideoCapture('video.mp4')

#cap = cv2.VideoCapture('Relaxing highway traffic.mp4')

cap.set(3,1920)

cap.set(4,1080)

if cap.isOpened():

    ret,frame1 = cap.read()

else:

    ret = False

ret,frame1 = cap.read()

ret,frame2 = cap.read()

   

while ret:

    d = cv2.absdiff(frame1,frame2)

    grey = cv2.cvtColor(d,cv2.COLOR_BGR2GRAY)

    #blur = cv2.GaussianBlur(grey,(5,5),0)

    blur = cv2.GaussianBlur(grey,(5,5),0)

    #ret , th = cv2.threshold(blur,20,255,cv2.THRESH_BINARY)

    ret , th = cv2.threshold(blur,20,255,cv2.THRESH_BINARY)

    dilated = cv2.dilate(th,np.ones((3,3)))

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))

        # Fill any small holes

    closing = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, kernel)

    contours,h = cv2.findContours(closing,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    for(i,c) in enumerate(contours):

        (x,y,w,h) = cv2.boundingRect(c)

        contour_valid = (w >= min_contour_width) and (

            h >= min_contour_height)

        if not contour_valid:

            continue

        cv2.rectangle(frame1,(x-10,y-10),(x+w+10,y+h+10),(255,0,0),2)

       

        cv2.line(frame1, (0, line_height), (1200, line_height), (0,255,0), 2)

        centroid = get_centroid(x, y, w, h)

        matches.append(centroid)

        cv2.circle(frame1,centroid, 5, (0,255,0), -1)

        cx,cy= get_centroid(x, y, w, h)

        for (x,y) in matches:

            if y<(line_height+offset) and y>(line_height-offset):

                vehicles=vehicles+1

                matches.remove((x,y))

                print(vehicles)

               

    cv2.putText(frame1, "Total vehicles Detected: " + str(vehicles), (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1,

                    (0, 170, 0), 2)

   

   

   

    #cv2.drawContours(frame1,contours,-1,(0,0,255),2)

    cv2.imshow("Original" , frame1)

    #cv2.imshow("Difference" , th)

    if cv2.waitKey(1) == 27:

        break

    frame1 = frame2

    ret , frame2 = cap.read()

#print(matches)    

cv2.destroyAllWindows()

cap.release()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.