Code Monkey home page Code Monkey logo

car-counting-and-speed-estimation-yolo-sort-python's Introduction

vehicle-counting-and-speed-estimation-yolo-sort-python

This project uses YOLOv3 for Vehicle detection and SORT(Simple Online and Realtime Tracker) for vehicle tracking

This project imlements the following tasks in the project:

  1. Vehicle counting
  2. Lane segmentation
  3. Lane change detection
  4. Speed estimation
  5. Dumps all these details into a CSV file

SCREENSHOT

link to the original video: https://youtu.be/PSf09R3D7Lo | updated gdrive link

link to the ouput video: gdrive link

Note that there are 4 locations in the video and so the code(4 IFs), you can delete 3 and edit the first one according to your need.

To run the project:

  1. Download the code or simply run:
$ git clone https://github.com/bamwani/car-counting-and-speed-estimation-yolo-sort-python 
  1. To install required dependencies, run:
$ cd car-counting-and-speed-estimation-yolo-sort-python/
$ pip3 install -r requirements.txt
  1. Download yolo weights file by:
$ bash download_weights
  1. Make sure you change the line of detection and lane segmentation according to your video and fine tune the threshold and confidence for YOLO model

  2. Run

$ Python3 main.py -input /path/to/video/file.avi -output /path/for/output/video/file.avi -yolo /path/to/YOLO/directory/

Speed Detection:

This is an interesting project, mainly due to camera shaking(maybe due to wind or whaterver the reason may be!). This camera shake results into frame flickering. Which means we can not use traditional pixel distance travelled to Km/h mapping because as each frame flickers, the centroid of the bounding box also flickers arbitrarily. Hence I tried this new method: SPEED BETWEEN TWO LINES

Speed Between Two Lines (SBTL)

This method makes some assumptions which are as follows:

  1. We know the speed limit of the road for which the video is recorded/streamed.
  2. Atleast one vehicle is driving at the speed limit.

--> We draw two lines on the frame perpendicular to the vehicle moving directon, then we find the minimim time taken by any car to cross these two lines(This car will be moving at the speed limit). Once we find he minimum time required for a car to pass these two lines, we use simple speed=distance/time formula to calculate the speed for rest of the cars.[ I know this wouldn't be perfect, but surely much more precise than the pixel mapping method]

I will try to keep making commits to improve the speed detection of vehicles. Any pull request for improvement is highly welcomed.

References:

YOLO

SORT Algorithm

Reference for combining YOLO and SORT

car-counting-and-speed-estimation-yolo-sort-python's People

Contributors

bamwani avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

car-counting-and-speed-estimation-yolo-sort-python's Issues

Hello, can you explain the steps in more detail?

(yolo) PS G:\ai\car-counting-and-speed-estimation-yolo-sort-python-master\code_and_output> python main.py --input 2.avi --output 3.mp4 --yolo ../yolo
[INFO] loading YOLO from disk...
Traceback (most recent call last):
File "main.py", line 130, in
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
cv2.error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\dnn\src\darknet\darknet_io.cpp:684: error: (-215:Assertion failed) ifile.is_open() in function 'cv::dnn::darknet::ReadDarknetFromWeightsFile'

(yolo) PS G:\ai\car-counting-and-speed-estimation-yolo-sort-python-master\code_and_output> main.py -input 2.avi -output 3.mp4 -yolo ../yolo
(yolo) PS G:\ai\car-counting-and-speed-estimation-yolo-sort-python-master\code_and_output>
[main 2020-01-10T09:16:10.606Z] update#setState idle
[main 2020-01-10T09:16:41.257Z] update#setState checking for updates
[main 2020-01-10T09:17:02.533Z] [Error: net::ERR_CONNECTION_CLOSED]
[main 2020-01-10T09:17:02.536Z] update#setState idle

TypeError: create_target_machine() got an unexpected keyword argument 'jitdebug'

Hi, When I run the main.py ,I met an error,
File "/home/shangguoqian/anaconda3/envs/car_counting/lib/python3.6/site-packages/numba/targets/codegen.py", line 608, in _init
tm = target.create_target_machine(**tm_options)
TypeError: create_target_machine() got an unexpected keyword argument 'jitdebug'

I check the version of numba ,it is 0.38.1, just what the readme said.
Could you tell me what should I do to solve this problem,thanks so much!

error while running main.py

Hi,

while running python3 main.py -input /path/to/video/file.avi -output /path/for/output/video/file.avi -yolo /path/to/YOLO/directory/

i got this error

sorting_error

programs ran for some seconds and end up with this error!!
please help me!

Thanks advance

Error while running the code?

Hey, I did everything as written in readme, I downloaded yolo weights, but I am unable to get result, instead I am getting code error. WHy is this?
p
This is the error that I get. I am giving it the same input viseo whose link in given in readme.

TypeError: tuple indices must be integers or slices, not tuple

I am getting this error while running the code. how Can I solve this issue.

before I ran into an issue 'invalid index to scalar variable' but I solved it by changing my OpenCV’s version to 4.2
is the current issue due to the maybe wrong version of some package ?

Error while executing code.

When I run the code I'm getting this error.
Traceback (most recent call last): File "F:/MyProjects/TensorFlow/car-counting-and-speed-estimation-yolo-sort-python/code_and_output/main.py", line 265, in <module> tracks = tracker.update(dets)

File "F:\MyProjects\TensorFlow\car-counting-and-speed-estimation-yolo-sort-python\code_and_output\sort.py", line 207, in update matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(dets,trks)

File "F:\MyProjects\TensorFlow\car-counting-and-speed-estimation-yolo-sort-python\code_and_output\sort.py", line 146, in associate_detections_to_trackers

if(d not in matched_indices[:,0]): TypeError: tuple indices must be integers or slices, not tuple

Any solutions.

The output video is unclear

the output video shows like the attached image. I can't figure out what is the problem or where can I change the height and width of the video
Screen Shot 2020-07-14 at 4 21 54 PM

can you upload the test video

Your code is so cool! but I tested it with my own video, and I couldn't see LANE, SPEED, and I couldn't generate final. CSV, please upload your test video,i am looking forward to it.

Numba error

Building wheel for numba (setup.py) ... error
ERROR: Command errored out with exit status 1:

How do I change speed limit?

I found the block of code where you specify 40 km/h speed limit but I don't understand how to change it suppose to 60 km/h

Original video file

Can you upload your original video file because I used the one from youtube and converted it to .avi and now there is weird error

[INFO] loading YOLO from disk... [INFO] 3668 total frames in video [INFO] single frame took 0.3317 seconds [INFO] estimated total time to finish: 1216.7966 Traceback (most recent call last): File "main.py", line 265, in <module> tracks = tracker.update(dets) File "/home//venv/speed/car-counting-and-speed-estimation-yolo-sort-python/code_and_output/sort.py", line 207, in update matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(dets,trks) File "/home/venv/speed/car-counting-and-speed-estimation-yolo-sort-python/code_and_output/sort.py", line 146, in associate_detections_to_trackers if(d not in matched_indices[:,0]): TypeError: tuple indices must be integers or slices, not tuple

P.S. what IFs should I remove (line numbers) to detect only on first location

Getting error while running code

I have run the code but they gave me many types of error so can you help me out and I am using the same video only that you are using and its urgent for me so can you please reply as much fast as you can

bug in the code while running the main.py

!python main.py --input "cars.mp4" --output "output" --yolo "yolo"

[INFO] loading YOLO from disk...
[INFO] 1190 total frames in video
[INFO] single frame took 0.2311 seconds
[INFO] estimated total time to finish: 274.9524
[0]

[ERROR:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap.cpp (597) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): output in function 'cv::icvExtractPattern'


Traceback (most recent call last):
  File "D:\st_engineering\objcount\car-counting-and-speed-estimation-yolo-sort-python\code_and_output\main.py", line 266, in <module>
    tracks = tracker.update(dets)
  File "D:\st_engineering\objcount\car-counting-and-speed-estimation-yolo-sort-python\code_and_output\sort.py", line 208, in update
    matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(dets,trks)
  File "D:\st_engineering\objcount\car-counting-and-speed-estimation-yolo-sort-python\code_and_output\sort.py", line 157, in associate_detections_to_trackers
    if(iou_matrix[m[0],m[1]]<iou_threshold):
IndexError: index 1 is out of bounds for axis 0 with size 1

matched_indices = linear_assignment(-iou_matrix)
matched_indices has below elements:

(array([0], dtype=int64), array([0], dtype=int64))

Please guide me how to mitigate this error

Not getting the same result

You can view the output which i am getting here: https://drive.google.com/file/d/1nL_y4MVMc1beTifFa1PqrshYh89pgeTq/view?usp=sharing

The lane coordinates are not matching
Moreover, the counter is not changing at all.

Only i change i made was in the sort algorithm according the abewley/sort#136 (comment)

def linear_assignment(cost_matrix):
  try:
     import lap
     _, x, y = lap.lapjv(cost_matrix, extend_cost=True)
     return np.array([[y[i], i] for i in x if i >= 0])
   except ImportError:
     from scipy.optimize import linear_sum_assignment
     x, y = linear_sum_assignment(cost_matrix)
     return np.array(list(zip(x, y)))

Please guide me on what mistake can be there in the code.

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.