Skip to content

Bhuribhat/Vehicle-Detection-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Created by

6330440921 Bhuribhat Ratansasanguanvongs

6330076121 Chanawat Voravijitchaikul

6330585221 Akkharawat Burachokviwat

Medium Link: https://medium.com/@EarthAkkharawat/real-time-vehicle-detection-and-counting-using-the-background-subtractor-comparing-with-haar-ff05f9ab2b92

Download Car Images

!wget https://miro.medium.com/max/640/1*0YyilCiA-lKLNNTZ1QO6rw.webp -O car22.jpeg
!wget https://miro.medium.com/max/640/1*N_FUmoCqT98ZwWVLxdeZZg.webp -O car29.jpeg

Download Car Classifier Model

# cars.xml file from https://github.com/afzal442/Real-Time_Vehicle_Detection-as-Simple/blob/master/cars.xml
!wget https://raw.githubusercontent.com/afzal442/Real-Time_Vehicle_Detection-as-Simple/master/cars.xml -O cars.xml

Detect Car in Images

using cars.xml model

import cv2
import matplotlib.pyplot as plt

# Colors
GREEN = (0, 255, 0)

# Configuration Car Size
min_width  = 30
min_height = 30

# Create Car Classifier
CLF = cv2.CascadeClassifier('cars.xml')

def detect_car_model(image_bgr):
    result = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
    gray   = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
    blur   = cv2.GaussianBlur(gray, (3, 3), 5)

    # Pass frame to our car classifier
    cars = CLF.detectMultiScale(
        blur, 
        scaleFactor = 1.1,    # how much the image size is reduced at each image scale
        minNeighbors = 2,     # how many neighbors each candidate rectangle should have to retain it
        minSize = (min_width, min_height)
    )

    # Extract bounding boxes for any car identified
    for x, y, w, h in cars:
        cv2.rectangle(result, (x, y), (x + w, y + h), GREEN, 2)

    plt.imshow(result)
    plt.title(f"Vehicle Count: {len(cars)}")
    plt.show()

Test Function

image = cv2.imread('car29.jpeg')
detect_car_model(image)

image = cv2.imread('car22.jpeg')
detect_car_model(image)

Result

Image 1 Image 2
Car22.jpeg Car29.jpeg

Real-time Car Detection

usage

>> py main.py
  • detect using subtract background (more accuracy)
  • detect using cars.xml model (less accuracy)

Resources

About

2110431 Introduction to Digital Imaging Final Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages