Code Monkey home page Code Monkey logo

ahmetozlu / vehicle_counting_tensorflow Goto Github PK

View Code? Open in Web Editor NEW
871.0 45.0 358.0 243.67 MB

:oncoming_automobile: "MORE THAN VEHICLE COUNTING!" This project provides prediction for speed, color and size of the vehicles with TensorFlow Object Counting API.

Home Page: https://www.youtube.com/watch?v=PrqnhHf6fhM

License: MIT License

Python 99.62% Dockerfile 0.38%
vehicle-detection vehicle-tracking vehicle-detection-and-tracking vehicle-counting color-recognition speed-prediction object-detection object-detection-label detection prediction

vehicle_counting_tensorflow's Introduction

VEHICLE DETECTION, TRACKING AND COUNTING

This sample project focuses on "Vechicle Detection, Tracking and Counting" using TensorFlow Object Counting API. Please contact if you need professional vehicle detection & tracking & counting project with the super high accuracy!


The TensorFlow Object Counting API is used as a base for object counting on this project, more info can be found on this repo.



The developing is on progress! This sample project will be updated soon, the more talented traffic analyzer app will be available in this repo!


General Capabilities of This Sample Project

This sample project has more than just counting vehicles, here are the additional capabilities of it:

  • Detection and classification of the vehicles (car, truck, bicycle, motorcycle, bus)
  • Recognition of approximate vehicle color
  • Detection of vehicle direction of travel
  • Prediction the speed of the vehicle
  • Prediction of approximate vehicle size
  • The images of detected vehicles are cropped from video frame and they are saved as new images under "detected_vehicles" folder path
  • The program gives a .csv file as an output (traffic_measurement.csv) which includes "Vehicle Type/Size", " Vehicle Color", " Vehicle Movement Direction", " Vehicle Speed (km/h)" rows, after the end of the process for the source video file.

ToDos:

  • More powerful detection models will be shared.
  • Sample codes will be developed to process different types of input videos (for different types of road traffics such as two way lane road).
  • Code cleanup will be performed.
  • UI will be developed.

The input video can be accessible by this link.

Theory

System Architecture

  • Vehicle detection and classification have been developed using TensorFlow Object Detection API, see for more info.
  • Vehicle speed prediction has been developed using OpenCV via image pixel manipulation and calculation, see for more info.
  • Vehicle color prediction has been developed using OpenCV via K-Nearest Neighbors Machine Learning Classification Algorithm is Trained Color Histogram Features, see for more info.

TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them.

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.

Tracker

Source video is read frame by frame with OpenCV. Each frames is processed by "SSD with Mobilenet" model is developed on TensorFlow. This is a loop that continue working till reaching end of the video. The main pipeline of the tracker is given at the above Figure.

Model

By default I use an "SSD with Mobilenet" model in this project. You can find more information about SSD in here. See the detection model zoo for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

The minimum vehicle detection threshold can be set in this line in terms of percentage. The default minimum vehicle detecion threshold is 0.5!

Project Demo

Demo video of the project is available on My YouTube Channel.

Installation

Docker setup with Nvidia GPU: Run the demo in the GPU without installing anything, just nvidia-docker. The command to set up this docker:

docker-compose up

Alternative for nvidia-docker, you can follow the installation steps are given below!

1.) Python and pip

Python is automatically installed on Ubuntu. Take a moment to confirm (by issuing a python -V command) that one of the following Python versions is already installed on your system:

  • Python 3.3+

The pip or pip3 package manager is usually installed on Ubuntu. Take a moment to confirm (by issuing a pip -V or pip3 -V command) that pip or pip3 is installed. We strongly recommend version 8.1 or higher of pip or pip3. If Version 8.1 or later is not installed, issue the following command, which will either install or upgrade to the latest pip version:

$ sudo apt-get install python3-pip python3-dev # for Python 3.n

2.) OpenCV

See required commands to install OpenCV on Ubuntu in here.

3.) TensorFlow

Install TensorFlow by invoking one of the following commands:

$ pip3 install tensorflow     # Python 3.n; CPU support (no GPU support)
$ pip3 install tensorflow-gpu # Python 3.n; GPU support

Current program is compatible with TensorFlow 1.5.0 version. Please uncomment these lines to run the program with TensorFlow 2.x: #1, #2, #3, #4.

4.) TensorFlow Object Detection API

See required commands to install TensorFlow Object Detection API on Ubuntu in here.

If you are still getting problem about installation after completed the installation of the packet that are given above, please check that link out to get detailed info about installation.


How to run the program?

After completing these 4 installation steps that are given at above, you can test the project by one of these commands. Program takes an input argument 'imshow' or 'imwrite':

  python3 vehicle_detection_main.py imshow
  python3 vehicle_detection_main.py imwrite
  • imshow : shows the processed frames as an video on screen.
  • imwrite : saves the processed frames as an output video in the project root folder.

Citation

If you use this code for your publications, please cite it as:

@ONLINE{vdtct,
    author = "Ahmet Özlü",
    title  = "Vehicle Detection, Tracking and Counting by TensorFlow",
    year   = "2018",
    url    = "https://github.com/ahmetozlu/vehicle_counting_tensorflow"
}

Author

Ahmet Özlü

License

This system is available under the MIT license. See the LICENSE file for more info.

vehicle_counting_tensorflow's People

Contributors

ahmetozlu avatar enric1994 avatar othella 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  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

vehicle_counting_tensorflow's Issues

Regarding the line drawing in the junction

Hello sir, I want to use this code in the purpose of counting cars, Pedestrians, trucks, buses, bikes moving in the junction, so that camera video, I want the separate the based on the direction and the total count of them, can you help me out.

Thanks in advance.

ROI Line

how to change the position of ROI Line, and now it is horizontal, what should I do.. to make it Vertical for the same video input?

Full Source video

Hey. The sample video output on the project demo page is long with a lot of cars. However, the one provided in the video download link is very short. Can you post the full video which is used for detection please? @ahmetozlu

Tensorflow version check throws error

Line 38 of vehicle_detection_main.py checks for tf.version < '1.4.0' and throws an error for tensorflow v1.10+

I guess because numerically 1.10.0 is < 1.4.0

Issue about counting the vehicle

Hi @ahmetozlu this code sometime detect(count) vehicle and sometime it doesn't count the vehicle
suggest some changes or any link which will help me and if you solve this issue please share the code
Thanks

line 93 ret, frame = cap.read() ERROR

Hello,

if i try to start your code with
python vehicle_detection_main.py i get this error code

python vehicle_detection_main.py File "vehicle_detection_main.py", line 93 ret, frame = cap.read() ^ TabError: inconsistent use of tabs and spaces in indentation

Heave you an idee why this happend ?

And can i use your code with my Webcam if i change the line
cap = cv2.VideoCapture('sub-1504619634606.mp4') to cap = cv2.VideoCapture(0) ?

#Original speed

Hi,
Can u provide me a link for published paper of Vehicle speed prediction for this project which you have done? and also provide me the original speed of the vehicles in the video for accuracy and comparison study with predicted speed?
I need to show false positive and false negative for my project for vehicle speed prediction. So please help me and kindly reply for this.

code explanation

if ((bottom)< 250):
scale = 1

elif ((bottom > 250) and (bottom < 320)):
	scale = 2
else:
	isInROI = False

How did u decide the numbers 250 and 320? I think they are based on line y coordinate. But how did it get to 250 and 320?
Thanks

color

How do I cancel the color prediction

NAN

sometimes the result is NAN. why so?

Not getting any output

I already tested the code. Although, I'm not getting any error, there is not any detected vehicle in the output. I tried object detection API only (not your code package) to see what the problem is; many vehicles were detected for the same input and same model (ssd-mobilenet). After some investigations, I found out that the variable 'is_vehicle_detected" in 'predict_speed' function is replaced by 1. I thought it should be increased one be one, not to be substituted by a constant. Also the variable ‘bottom-bottom_position_of_detected_vehicle’ is not initialized.
Moreover, in 'visualization_utils.py', the function '
draw_bounding_box_on_image' is supposed to return the output image, however, it updates the is_vehicle_detected variable as well as csv file.
I'm not sure on how to fix this. My python version 3.6. Did you get the demo output in

Commulative Counting of different types of vehicles

Hi! I have integrated the object Counting API for the model I have trained on my own custom dataset. After integrating the vehicle counting/object counting api, the customs classes (bikes , cars , bus , rickshaws) are being detected but they are not being counted separately. On video, I am getting total count of all vehicles only. Can you please tell me how to get commulative counts of each vehicle type as they pass ROI ? I urgently need help in this regard. Thanks in advance.

Ambiguity in visualize_boxes_and_labels_on_image_array() function

As per the snippet from the documentation, visualize_boxes_and_labels_on_image_array function accepts arguments which are completely different from your own arguments. Also it returns the image as you can see below, then how are you supposed to collect the 'counter' and 'csv line' from that fucntion?

 Returns:

    uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes

I modified the code as per the arguments but now I am not able to find the count when the object crosses ROI. Please help.
MODIFIED SNIPPET


counter = len(boxes)
                print("Cureent running counter: ",counter)

                # Visualization of the results of a detection.

                input_frame = vis_util.visualize_boxes_and_labels_on_image_array(

                    input_frame,

                    np.squeeze(boxes),

                    np.squeeze(classes).astype(np.int32),

                    np.squeeze(scores),

                    category_index,

                    use_normalized_coordinates=True,

                    line_thickness=4)

In out count

How can i be able to get the vehicle in and vehicle out count?

How to Change Label

hi

I want to detect stop sign alone, which has the id: 13

So how to change that in util function?

Speed Calculation explanation

Thanks for your explanation, can you please explain how you are calculating the speed in this block ?
Because I can't understand some of the transformations like scale_real_length, scale_real_time_passed, speed = speed/6*40 and scale = 1
please elaborate a bit.

visualization_util Error

temp, counter, csv_line = vis_util.visualize_boxes_and_labels_on_image_array(cap.get(1),
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4)
This is some part of my code. But i am getting the following error.


KeyError Traceback (most recent call last)
in ()
38 category_index,
39 use_normalized_coordinates=True,
---> 40 line_thickness=4)
41 print("-*-GELEN COUNTER: " + str(counter))
42 temp_counter = temp_counter + counter

~/video-analytics/models/object_detection/utils/visualization_utils.py in visualize_boxes_and_labels_on_image_array(image, boxes, classes, scores, category_index, instance_masks, keypoints, use_normalized_coordinates, max_boxes_to_draw, min_score_thresh, agnostic_mode, line_thickness)
443 box = tuple(boxes[i].tolist())
444 if instance_masks is not None:
--> 445 box_to_instance_masks_map[box] = instance_masks[i]
446 if keypoints is not None:
447 box_to_keypoints_map[box].extend(keypoints[i])

KeyError: 0

Installation Steps for Windows

Hi ,
1.I need to run this source code Windows OS.can you please send me the procedure for run this source code to windows
2.Is it possible open Visual Studio 2015?

ModuleNotFoundError: No module named 'utils' (On Windows)

RESTART: C:\tensorflow\models-master\research\vehicle_counting_tensorflow\vehicle_detection_main.py
Traceback (most recent call last):
File "C:\tensorflow\models-master\research\vehicle_counting_tensorflow\vehicle_detection_main.py", line 27, in
from utils import visualization_utils as vis_util
File "C:\tensorflow\models-master\research\vehicle_counting_tensorflow\utils\visualization_utils.py", line 30, in
from image_utils import image_saver
ImportError: cannot import name 'image_saver'

=====

.

RESTART: C:\tensorflow\models-master\research\vehicle_counting_tensorflow\utils\speed_and_direction_prediction_module\speed_prediction.py
Traceback (most recent call last):
File "C:\tensorflow\models-master\research\vehicle_counting_tensorflow\utils\speed_and_direction_prediction_module\speed_prediction.py", line 1, in
from utils.image_utils import image_saver
ModuleNotFoundError: No module named 'utils'

=======
utils is folder inside but while complie it error like above.
How to fix this? Please help

Setting coordinates for new video

Hi. I want to run my own video. What to change and how to change in the code so as to run my own vehicle traffic video.
Thanks in advance!!

Customising code of roi line

I want to use this program with another traffic video but can't find the code where the coordinates of roi line is defined. I tried changin the variable in visualization file that was previously set to 200 but it didn't work. Can you please help me with this?

Question about some variables

What does "bottom" and "bottom_position_of_detected_vehicle" variable mean? I get so confused. Does bottom refer to the bottom coordinates of the current object then the bottom_position_of_detected_vehicle is the previous bottom position of the previous vehicle?

CUDA 10 support

Can you brief me on what I have to do, so make the script compatible with CUDA 10.

Implement multiple camera at once?

Hi every one,
This works fine for single camera, but how i can implement multiple cameras for vehicles detection? . i googled a lot but couldn't find the practical approach for it.

can any one can help me, please!!!

ROI count

How do we count number of vehicles when they pass ROI area? Guess in current code bottom coordinates ymax is checked with 200 - but in subsequent frames this will be same since video moves fast - how is duplicate count eliminated? Thanks

tracker

why tracking algorithm is not required?
I am to count the number of people entering a vehicle. How can I provide the ROI line so that people leaving or entering past that line will be counted?

thank you

error while decoding MB 4 6, bytestream -9

Hi,
Thank you so much for providing this repo.
I'm running your code in a virtual machine which I only have access to the terminal, so I decided to save each frame instead of displaying it. I get this error when running the code
error while decoding MB 4 6, bytestream -9
The code stops after processing first frame of video file. I'd be thankful if you kindly help on that.

Thanks,

Bahareh

Cannot import name label_map_util

File "D:/ObjectDetect_Project/vehicle_counting_tensorflow/vehicle_detection.py", line 19, in
from utils import label_map_util

ImportError: cannot import name 'label_map_util'

how can i solve this import error ?

image

Run this on a new trained model

I have a trained model of inception v3, retrained on the class of Vehicles. I want to use that instead of the by default setting here. How can i do that ?

Slow output on using different model

i tried it on faster_rcnn_resnet101_coco_2018_01_28 and faster_rcnn_inception_v2_coco_2018_01_28 but both of them results in slow output and not able to detect speed but the already present model in this project works fine.Please help me @ahmetozlu

Vehicle detection problem

Hi guys
we know that this model (ssd_mobilenet_v1_coco_2017_11_17) is general object detection problem which changes we perform to detect only vehicle like in this project
Thanks

Identation not consistent

I am having trouble running the code without fixing identation which is a mix of tab and space usages

is that more efficient to work on sequential images OR video?

Dear sir,
I'm having a question.
Which of the following is more efficient?and why?

Applying the detection-tracking part on sequential images OR a video?

If we apply the pipeline on series of images, the detected bounding box needs to be saved within running of pipeline on each frame, however when running on video, it seems that it performs better by utilizing the functions in moviepy library. What do you think?

Not able to count the vechile

I am trying to use my own video which is 30 frames per second and resolution is of 1920*1080. I tried to change the ROI value from 200 to different values but it didnt work.

Can you please guide me on the same??

I would also like to know where to update the frames per second value.

Looking forward to hearing from you.

IndexError: list index out of range

When I run the code then it gives me the error....

Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/mark/CarSpeedDetection/vehicle_detection_main.py', wdir='C:/Users/mark/CarSpeedDetection')

File "C:\Users\Administrator\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\Administrator\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/mark/CarSpeedDetection/vehicle_detection_main.py", line 124, in
object_detection_function()

File "C:/Users/mark/CarSpeedDetection/vehicle_detection_main.py", line 91, in object_detection_function
np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=4)

File "C:\Users\mark\CarSpeedDetection\utils\visualization_utils.py", line 304, in visualize_boxes_and_labels_on_image_array
use_normalized_coordinates=use_normalized_coordinates)

File "C:\Users\mark\CarSpeedDetection\utils\visualization_utils.py", line 74, in draw_bounding_box_on_image_array
display_str_list, use_normalized_coordinates)

File "C:\Users\mark\CarSpeedDetection\utils\visualization_utils.py", line 103, in draw_bounding_box_on_image
current_path + "/utils/color_recognition_module/" + "test.data")

File "./utils\color_recognition_module\knn_classifier.py", line 64, in main
neighbors = kNearestNeighbors(training_feature_vector, test_feature_vector[x], k)

File "./utils\color_recognition_module\knn_classifier.py", line 24, in kNearestNeighbors
neighbors.append(distances[x][0])

IndexError: list index out of range

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.