Real-Time Car Detection using OpenCV Python With Source Code

Real-Time Car Detection using OpenCV and Python With Source Code

The Real-Time Car Detection OpenCV Python was developed using Python OpenCV, Vehicle detection is one of the widely used features by companies and organizations these days.

This technology uses computer vision to detect different types of vehicles in a video or real-time via a camera.

A Car Detection OpenCV Python finds its applications in traffic control, car tracking, creating parking sensors, and many more. In this, we will learn how to build a car tracking system in Python for both recorded and live cam streamed videos.

This Python OpenCV Project also includes a downloadable Python Project With Source Code for free, just find the downloadable source code below and click to start downloading.

By the way, if you are new to Python programming and don’t know what Python IDE is, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install the Latest Version of Python on Windows.

To start executing Real-Time Car Detection OpenCV Python With Source Code, make sure that you have installed Python 3.9 and PyCharm on your computer.

Real-Time Car or Vehicle Detection using OpenCV and Python With Source Code: Steps on how to run the project

Time needed: 5 minutes

These are the steps on how to run Real-Time Car Detection OpenCV Python With Source Code

  • Step 1: Download the given source code below.

    First, download the given source code below and unzip the source code.

    car detection download source code

  • Step 2: Import the project to your PyCharm IDE.

    Next, import the source code you’ve downloaded to your PyCharm IDE.

    car detection open project

  • Step 3: Run the project.

    Lastly, run the project with the command “py main.py”.

    car detection run project

Installed Libraries

import numpy as np
import cv2

Complete Source Code

import numpy as np
import cv2

cascade_src = 'cars.xml'
# video = 'data/Cars_On_Highway.mp4'
video = 'data/video1.avi'
# video = 'data/video2.avi'


def detectCars(filename):
  rectangles = []
  cascade = cv2.CascadeClassifier(cascade_src)

  vc = cv2.VideoCapture(filename)

  if vc.isOpened():
      rval , frame = vc.read()
  else:
      rval = False


  while rval:
    rval, frame = vc.read()
    frameHeight, frameWidth, fdepth = frame.shape

    # Resize
    frame = cv2.resize(frame, ( 600,  400 ))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # haar detection.
    cars = cascade.detectMultiScale(gray, 1.3, 3)


    for (x, y, w, h) in cars:
      cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)


    # show result
    cv2.imshow("Result",frame)

    if cv2.waitKey(33) == ord('q'):
      break

  vc.release()


detectCars(video)

Output:

Download the Source Code below

Summary

Car detection is one of the widely used features by companies and organizations these days. This technology uses computer vision to detect different types of vehicles in video or in real time via a camera. It finds its applications in traffic control, car tracking, creating parking sensors, and many more.

Related Articles

Inquiries

If you have any questions or suggestions about Real-Time Car Detection OpenCV Python With Source Code, please feel free to leave a comment below.

Leave a Comment