0% found this document useful (0 votes)
8 views16 pages

AIreport Dharmit

The project report presents a vehicle detection system developed using Python and OpenCV, focusing on vehicle counting and speed detection for traffic management. It discusses the implementation of computer vision techniques to analyze video feeds, providing insights into traffic patterns and aiding in infrastructure planning. The report also addresses challenges and limitations faced during the project, emphasizing the importance of accurate calibration and fixed camera positioning.

Uploaded by

Ruturaj Nakum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

AIreport Dharmit

The project report presents a vehicle detection system developed using Python and OpenCV, focusing on vehicle counting and speed detection for traffic management. It discusses the implementation of computer vision techniques to analyze video feeds, providing insights into traffic patterns and aiding in infrastructure planning. The report also addresses challenges and limitations faced during the project, emphasizing the importance of accurate calibration and fixed camera positioning.

Uploaded by

Ruturaj Nakum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Project Report

On
Vehicle Detection
Submitted to
Institute
Code:017
Vishwakarma Government Engineering College, Chandkheda

Under the Guidance of


Prof. Navin Ganeshan

In partial Fulfilment of the Requirement of the award of the degree


of Bachelor of Engineering
Offered By
Gujarat Technological University
Ahmedabad

Prepared by:
Patel Dharmit
210170111127
ELECTRONICS & COMMUNICATION (Semester - VII)
Month & Year:
October-2024
Vishwakarma Government
Engineering College

Vishwakarma Government
Engineering College,
Nr. Visat three roads, Sabarmati-Koba
Highway, Chandkheda, Ahmedabad,
Gujarat, India-382424

CERTIFICATE

This is to certify that the project report submitted along with


the project entitled Vehicle Detection has been carried out by
Patel Dharmit under my guidance in partial fulfillment for
the Project in Electronics and Communication, 7th Semester
of Gujarat Technological University, Ahmedabad during the
academic year 2024-25.

Prof. Navin Ganeshan Dr. Arun

Nandurbarkar Internal Guide HOD of the department


ACKNOWLEDGEMENT

I am deeply grateful to my project guide, Prof. Navin Ganesha, for his invaluable
guidance, patience, and support throughout this journey. I consider myself fortunate to
have worked with such considerate and encouraging professors. My heartfelt thanks
also go to our HOD, Dr. Arun Nandurbarkar, for providing me with this excellent
opportunity to work on the project titled “Vehicle Detection.” This project would not
have been possible without their unwavering support.

I would also like to express my sincere gratitude to my parents, friends, and family
members for their continuous encouragement and support during this project.
Additionally, I would like to extend my thanks to the college personnel for granting
me access to the valuable resources required for the successful completion of this
project.

In conclusion, I once again thank the faculty and members of Vishwakarma


Government Engineering College for their indispensable support in the completion of
this project.

Thank you
Abstract

Vehicle counting and speed detection are critical elements for traffic management, congestion control,
and road safety analysis. This research presents an advanced system for vehicle counting and speed
detection using computer vision and deep learning techniques. Additionally, speed estimation is
achieved by tracking the detected vehicles over time, using frame-by-frame analysis with a calibrated
camera setup. The model is trained on a diverse dataset to ensure reliability under different weather
conditions, lighting variations, and road types. The system provides valuable insights into traffic
patterns, enabling authorities to make informed decisions for traffic control, resource allocation, and
infrastructure planning. Experimental results validate the accuracy and robustness of the system,
showcasing its capability to effectively manage real-world traffic scenarios.
TABLE OF CONTENTS

Sr. Pg.
Content
No. No.
Introduction 1
1

Components 2
2

System Design 2
3

Implementation 3
4

Output 8
5

Challenges and Limitations 9


6

Conclusion 10
7

References 10
8
Introduction

Vehicle counting and speed detection are essential components of modern traffic management systems,
playing a vital role in monitoring traffic flow, reducing congestion, and enhancing road safety. This
project demonstrates how Python, using the OpenCV library, can be employed to count vehicles and
detect their speeds from real-time video feeds. The vehicle detection and tracking functionality is based
on computer vision techniques, including background subtraction and object detection, to identify and
count vehicles while calculating their speeds.

The ability to detect, count, and track vehicles is a critical aspect of various modern applications, such
as intelligent traffic systems, congestion control, and law enforcement. Vehicle counting and speed
detection have numerous real-world applications, ranging from automated traffic monitoring to smart
city development and road safety improvements.

This project demonstrates how to implement vehicle counting and speed detection using Python and the
OpenCV library, which is a powerful tool for image processing and computer vision. By leveraging
object detection and tracking algorithms, the program can efficiently detect vehicles, count them, and
estimate their speeds based on frame-by-frame analysis. The development of this project was carried
outin Visual Studio Code (VS Code), which provided a robust environment for managing the
code,debugging, and improving productivity throughout the development process.

1|Page
Components

Software: - Python: The programming language used to develop the eye detection logic. -
cv2: OpenCV library for image and video processing.
numpy: Used for numerical operations and handling
matrices. time.sleep: Used to control the speed of video
playback.

System Design

The vehicle counting and speed detection system aims to process video footage,
identify vehicles, count them, and determine their speed. The system is implemented
using Python with the OpenCV library and follows a modular architecture to handle the
different stages of video processing.

2|Page
Implementation

The following Python code uses the OpenCV library to Vehicle counting and speed
detection within a video.

import cv2

import numpy as np

from time import

sleep

# Parameters

largura_min = 80 # Minimum rectangle width

altura_min = 80 # Minimum rectangle height

offset = 6 # Allowed error between pixels

pos_linha = 550 # Line position for counting

cars

delay = 60 # FPS of the video (adjust this to match the video's FPS)

scale_factor = 0.05 # Assumed scale factor (meters per pixel, adjust this based on the

video) fps = 30 # Assuming the video has 30 frames per second

detec = []

carros =

car_speeds = {} # To store the speed of each car

3|Page
def pega_centro(x, y, w, h):

"""Returns the center of the bounding

box.""" cx = x + int(w / 2)

4|Page
cy = y + int(h /

2) return cx, cy

# Open the video file

file_path = r"C:\Users\Dell\OneDrive\Desktop\carvideo.mp4"

cap = cv2.VideoCapture(file_path)

# Background subtractor

subtracao = cv2.bgsegm.createBackgroundSubtractorMOG()

# Main processing

loop while True:

ret, frame1 = cap.read()

if not ret:

break

tempo = float(1/delay)

sleep(tempo) # Control video speed

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

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

img_sub = subtracao.apply(blur)

dilat = cv2.dilate(img_sub, np.ones((5, 5), np.uint8))

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

dilatada = cv2.morphologyEx(dilat, cv2.MORPH_CLOSE, kernel)


5|Pag
dilatada = cv2.morphologyEx(dilatada, cv2.MORPH_CLOSE, kernel)

# Updated contour detection to handle two return values

contorno, _ = cv2.findContours(dilatada, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Draw counting line

cv2.line(frame1, (25, pos_linha), (1200, pos_linha), (255, 127, 0), 3)

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

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

validar_contorno = (w >= largura_min) and (h >= altura_min)

if not validar_contorno:

continue

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

centro = pega_centro(x, y, w, h)

detec.append(centro)

cv2.circle(frame1, centro, 4, (0, 0, 255), -1)

for (cx, cy) in detec:

# Check if car crossed the line

if cy < (pos_linha + offset) and cy > (pos_linha -

offset): carros += 1

# Calculate speed based on the movement between frames

if len(detec) > 1:
6|Pag
last_cx, last_cy = detec[-2]

distance_in_pixels = np.sqrt((cx - last_cx)**2 + (cy - last_cy)**2)

distance_in_meters = distance_in_pixels * scale_factor

time_seconds = 1 / fps # Time per frame

speed_mps = distance_in_meters / time_seconds # Speed in meters per second

speed_kph = speed_mps * 3.6 # Convert to kilometers per hour

# Assign the speed to the current car

car_speeds[carros] = speed_kph

print(f"Car {carros} detected, Speed: {speed_kph:.2f} km/h")

# Draw the car speed on the frame

cv2.putText(frame1, f"Car {carros} Speed: {car_speeds[carros]:.2f} km/h",

(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)

# Update line after detection

cv2.line(frame1, (25, pos_linha), (1200, pos_linha), (0, 127, 255), 3)

detec.remove((cx, cy))

# Display vehicle count on the frame

cv2.putText(frame1, f"VEHICLE COUNT: {carros}", (450, 70), cv2.FONT_HERSHEY_SIMPLEX,

2, (0, 0, 255), 5)

# Show the video frames

cv2.imshow("Video Original", frame1)


7|Pag
cv2.imshow("Detection", dilatada)

# Exit on 'Esc' key

if cv2.waitKey(1) == 27:

break

# Release the resources

cap.release()

cv2.destroyAllWindows()

8|Pag
Output

9|Pag
Challenges and Limitations

Some challenges encountered in this project include:

1. Variable Lighting Conditions:


Changes in lighting (daylight, shadows, headlights) can affect the accuracy of vehicle
detection and background subtraction, leading to missed detections or false positives.
2. Occlusion:
Vehicles may be partially hidden behind other objects (e.g., other vehicles, poles), making
it difficult for the system to detect them accurately.
3. Complex Backgrounds:
Busy or cluttered backgrounds can confuse the background subtraction algorithms, resulting in
false detections.
4. Movement Speed Variability:
Vehicles traveling at different speeds can lead to inconsistencies in speed
measurement, especially if the frame rate is low or if there are abrupt changes in
vehicle direction.

Some Limitations encountered in this project include:

1. Accuracy and Precision:


The accuracy of counting and speed detection can be compromised due to the reasons
mentioned above, leading to potential errors in the vehicle count and speed measurements.
2. Dependency on Calibration:
The scale factor and other parameters need to be accurately calibrated for each specific
setup, which can be a tedious process.
3. Fixed Positioning:
The system typically requires fixed camera positions for effective counting and speed
detection, making it less adaptable to changing environments.
4 . No Identification Capability:
The system can count vehicles and estimate speeds but cannot identify specific vehicles or
track them over long distances without additional features (like license plate recognition).

10 | P a
Conclusion

The vehicle counting and speed detection system represents a significant advancement in the application
of computer vision technologies for real-time traffic monitoring and analysis. By leveraging Python and
the OpenCV library, this project demonstrates the feasibility of accurately detecting vehicles, counting
them as they pass a predefined line, and estimating their speed based on movement across video frames.
the successful execution of this project not only highlights the practical applications of vehicle
detection and speed estimation but also sets the groundwork for further exploration and
development in the realm of intelligent transportation systems.

References
 OpenCV documentation (https://docs.opencv.org)-
 Python tutorials and resources (https://www.python.org)-
 OpenCV Python tutorials (https://opencv-python-tutroals.readthedocs.io)

11 | P a

You might also like