Giter Club home page Giter Club logo

vehicle-detection's Introduction

Vehicle Detection for Autonomous Driving

Objective

A demo of Vehicle Detection System: a monocular camera is used for detecting vehicles.

gif_demo1

(2) City Drive (Vehicle Detection only) (Click to see the full video)

gif_demo2


Code & Files

1. My project includes the following files


Others are the same as in the repository of Lane Departure Warning System:

  • calibration.py contains the script to calibrate camera and save the calibration results
  • lane.py contains the lane class
  • examples folder contains the sample images and videos

2. Dependencies & my environment

Anaconda is used for managing my dependencies.

  • You can use provided environment-gpu.yml to install the dependencies.
  • OpenCV3, Python3.5, tensorflow, CUDA8
  • OS: Ubuntu 16.04

3. How to run the code

(1) Download weights for YOLO

You can download the weight from here and save it to the weights folder.

(2) If you want to run the demo, you can simply run:

python main.py

4. Release History

  • 0.1.1

    • Fix two minor bugs and update the documents
    • Date 18 April 2017
  • 0.1.0

    • The first proper release
    • Date 31 March 2017

Two approaches: Linear SVM vs Neural Network

1. Linear SVM Approach

svm_pipeline.py contains the code for the svm pipeline.

Steps:

  • Perform a Histogram of Oriented Gradients (HOG) feature extraction on a labeled training set of images and train a classifier Linear SVM classifier
  • A color transform is applied to the image and append binned color features, as well as histograms of color, to HOG feature vector.
  • Normalize your features and randomize a selection for training and testing.
  • Implement a sliding-window technique and use SVM classifier to search for vehicles in images.
  • Run pipeline on a video stream and create a heat map of recurring detections frame by frame to reject outliers and follow detected vehicles.
  • Estimate a bounding box for detected vehicles.

1.1 Extract Histogram of Oriented Gradients (HOG) from training images

The code for this step is contained in the function named extract_features and codes from line 464 to 552 in svm_pipeline.py. If the SVM classifier exist, load it directly.

Otherwise, I started by reading in all the vehicle and non-vehicle images, around 8000 images in each category. These datasets are comprised of images taken from the GTI vehicle image database and KITTI vision benchmark suite. Here is an example of one of each of the vehicle and non-vehicle classes:

alt text

I then explored different color spaces and different skimage.hog() parameters (orientations, pixels_per_cell, and cells_per_block). I grabbed random images from each of the two classes and displayed them to get a feel for what the skimage.hog() output looks like.

Here is an example using the RGB color space and HOG parameters of orientations=9, pixels_per_cell=(8, 8) and cells_per_block=(2, 2):

alt text alt text

To optimize the HoG extraction, I extract the HoG feature for the entire image only once. Then the entire HoG image is saved for further processing. (see line 319 to 321 in svm_pipeline.py)

1.2 Final choices of HOG parameters, Spatial Features and Histogram of Color.

I tried various combinations of parameters and choose the final combination as follows (see line 16-27 in svm_pipeline.py):

  • YCrCb color space
  • orient = 9 # HOG orientations
  • pix_per_cell = 8 # HOG pixels per cell
  • cell_per_block = 2 # HOG cells per block, which can handel e.g. shadows
  • hog_channel = "ALL" # Can be 0, 1, 2, or "ALL"
  • spatial_size = (32, 32) # Spatial binning dimensions
  • hist_bins = 32 # Number of histogram bins
  • spatial_feat = True # Spatial features on or off
  • hist_feat = True # Histogram features on or off
  • hog_feat = True # HOG features on or off

All the features are normalized by line 511 to 513 in svm_pipeline.py, which is a critical step. Otherwise, classifier may have some bias toward to the features with higher weights.

1.3. How to train a classifier

I randomly select 20% of images for testing and others for training, and a linear SVM is used as classifier (see line 520 to 531 in svm_pipeline.py)

1.4 Sliding Window Search

For this SVM-based approach, I use two scales of the search window (64x64 and 128x128, see line 41) and search only between [400, 656] in y axis (see line 32 in svm_pipeline.py). I choose 75% overlap for the search windows in each scale (see line 314 in svm_pipeline.py).

For every window, the SVM classifier is used to predict whether it contains a car nor not. If yes, save this window (see line 361 to 366 in svm_pipeline.py). In the end, a list of windows contains detected cars are obtianed.

alt text

1.5 Create a heat map of detected vehicles

After obtained a list of windows which may contain cars, a function named generate_heatmap (in line 565 in svm_pipeline.py) is used to generate a heatmap. Then a threshold is used to filter out the false positives.

heatmap heatmap

1.6 Image vs Video implementation

For image, we could directly use the result from the filtered heatmap to create a bounding box of the detected vehicle.

For video, we could further utilize neighbouring frames to filter out the false positives, as well as to smooth the position of bounding box.

  • Accumulate the heatmap for N previous frame.
  • Apply weights to N previous frames: smaller weights for older frames (line 398 to 399 in svm_pipeline.py).
  • I then apply threshold and use scipy.ndimage.measurements.label() to identify individual blobs in the heatmap.
  • I then assume each blob corresponded to a vehicle and constructe bounding boxes to cover the area of each blob detected.

Example of test image

alt text


2. Neural Network Approach (YOLO)

yolo_pipeline.py contains the code for the yolo pipeline.

YOLO is an object detection pipeline baesd on Neural Network. Contrast to prior work on object detection with classifiers to perform detection, YOLO frame object detection as a regression problem to spatially separated bounding boxes and associated class probabilities. A single neural network predicts bounding boxes and class probabilities directly from full images in one evaluation. Since the whole detection pipeline is a single network, it can be optimized end-to-end directly on detection performance.

alt text

Steps to use the YOLO for detection:

  • resize input image to 448x448
  • run a single convolutional network on the image
  • threshold the resulting detections by the model’s confidence

alt text

yolo_pipeline.py is modified and integrated based on this tensorflow implementation of YOLO. Since the "car" is known to YOLO, I use the precomputed weights directly and apply to the entire input frame.

Example of test image

alt text


Discussion

For the SVM based approach, the accuray is good, but the speed (2 fps) is an problem due to the fact of sliding window approach is time consuming! We could use image downsampling, multi-threads, or GPU processing to improve the speed. But, there are probably a lot engineering work need to be done to make it running real-time. Also, in this application, I limit the vertical searching range to control the number of searching windows, as well as avoid some false positives (e.g. cars on the tree).

For YOLO based approach, it achieves real-time and the accuracy are quite satisfactory. Only in some cases, it may failure to detect the small car thumbnail in distance. My intuition is that the original input image is in resolution of 1280x720, and it needs to be downscaled to 448x448, so the car in distance will be tiny and probably quite distorted in the downscaled image (448x448). In order to correctly identify the car in distance, we might need to either crop the image instead of directly downscaling it, or retrain the network.

vehicle-detection's People

Contributors

junshengfu 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

vehicle-detection's Issues

Save the Hog into XML

Hi, is it able to save the clf_pickle_all_v1.p into *.xml so that I can use it in c++ detectMultiScale() function?

Sorry for I use this way to ask you the question, thanks very much.

Size of input

EDIT: Another question: I get this error: Fatal Python error: PyImport_GetModuleDict: no module dictionary when I run the demo inside Oracle VM VirtualBox. How can I fix this?

Run SVM detector error

When I run main.py and set demo =1, It return errors below:
File "E:/PycharmProject/vehicle-detection/main.py", line 37, in
draw_img = pipeline_svm(image)
File "E:/PycharmProject/vehicle-detection/main.py", line 16, in pipeline_svm
output = vehicle_detection_svm(img_undist, img_lane_augmented, lane_info)
File "E:\PycharmProject\vehicle-detection\svm_pipeline.py", line 556, in vehicle_detection_svm
hog_feat)
File "E:\PycharmProject\vehicle-detection\svm_pipeline.py", line 355, in find_cars
test_features = X_scaler.transform(X)
File "D:\DIYPrograms\Anaconda3\lib\site-packages\sklearn\preprocessing_data.py", line 794, in transform
force_all_finite='allow-nan')
File "D:\DIYPrograms\Anaconda3\lib\site-packages\sklearn\base.py", line 436, in _validate_data
self._check_n_features(X, reset=reset)
File "D:\DIYPrograms\Anaconda3\lib\site-packages\sklearn\base.py", line 373, in check_n_features
"The reset parameter is False but there is no "
RuntimeError: The reset parameter is False but there is no n_features_in
attribute. Is this estimator fitted?

running error

there is a problem when run the code, please help me, thank you very much.

Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1139, in _do_call
return fn(*args)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1121, in _run_fn
status, run_metadata)
File "D:\Anaconda3\lib\contextlib.py", line 89, in exit
next(self.gen)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for weights/YOLO_small.ckpt
[[Node: save/RestoreV2_2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_2/tensor_names, save/RestoreV2_2/shape_and_slices)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/vehicle-detection-master/main.py", line 4, in
from yolo_pipeline import *
File "C:\Users\Administrator\PycharmProjects\vehicle-detection-master\yolo_pipeline.py", line 211, in
yolo = yolo_tf()
File "C:\Users\Administrator\PycharmProjects\vehicle-detection-master\yolo_pipeline.py", line 25, in init
self.build_networks()
File "C:\Users\Administrator\PycharmProjects\vehicle-detection-master\yolo_pipeline.py", line 66, in build_networks
self.saver.restore(self.sess, self.weights_file)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1548, in restore
{self.saver_def.filename_tensor_name: save_path})
File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
run_metadata_ptr)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for weights/YOLO_small.ckpt
[[Node: save/RestoreV2_2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_2/tensor_names, save/RestoreV2_2/shape_and_slices)]]

Caused by op 'save/RestoreV2_2', defined at:
File "C:/Users/Administrator/PycharmProjects/vehicle-detection-master/main.py", line 4, in
from yolo_pipeline import *
File "", line 961, in _find_and_load
File "", line 950, in _find_and_load_unlocked
File "", line 655, in _load_unlocked
File "", line 678, in exec_module
File "", line 205, in _call_with_frames_removed
File "C:\Users\Administrator\PycharmProjects\vehicle-detection-master\yolo_pipeline.py", line 211, in
yolo = yolo_tf()
File "C:\Users\Administrator\PycharmProjects\vehicle-detection-master\yolo_pipeline.py", line 25, in init
self.build_networks()
File "C:\Users\Administrator\PycharmProjects\vehicle-detection-master\yolo_pipeline.py", line 65, in build_networks
self.saver = tf.train.Saver()
File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1139, in init
self.build()
File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1170, in build
restore_sequentially=self._restore_sequentially)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 691, in build
restore_sequentially, reshape)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 407, in _AddRestoreOps
tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 247, in restore_op
[spec.tensor.dtype])[0])
File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 640, in restore_v2
dtypes=dtypes, name=name)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in init
self._traceback = _extract_stack()

NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for weights/YOLO_small.ckpt
[[Node: save/RestoreV2_2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_2/tensor_names, save/RestoreV2_2/shape_and_slices)]]

ImportError: DLL load failed

ImportError: DLL load failed: The specified module could not be found.
Failed to load the native TensorFlow runtime.

what should i do?

Fatal Python error: PyImport_GetModuleDict: no module dictionary!

Hi, When I run main.py, the shows:

`[MoviePy] >>>> Building video examples/project_svm.mp4
[MoviePy] Writing video examples/project_svm.mp4
98%|██████████████████████████████████████████████████████████████████████████████████████████████ | 50/51 [00:28<00:00, 1.44it/s]
[MoviePy] Done.
[MoviePy] >>>> Video ready: examples/project_svm.mp4

Fatal Python error: PyImport_GetModuleDict: no module dictionary!

Current thread 0x00007f6414c20700 (most recent call first):
File "/home/chm/.virtualenvs/cv/lib/python3.5/site-packages/moviepy/video/io/VideoFileClip.py", line 116 in del
Aborted (core dumped)
`
Could you please tell me how to solve it?

run main.py error

Hello, after I have prepared all the dependencies, I run main.py and report an error, segmentation fault (core dumped), what is the reason?

Exception has occurred: cv2.error

Hello, I am trying your code on a dash cam video that I found on youtube and I get this error:

OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\imgproc\src\resize.cpp:4044: error: (-215) ssize.width > 0 && ssize.height > 0 in function cv::resize
File "C:\MT_Denemeler2\01\vehicle-detection-master\visualizations.py", line 38, in draw_thumbnails
vehicle_thumb = cv2.resize(thumbnail, dsize=(thumb_w, thumb_h))
File "C:\MT_Denemeler2\01\vehicle-detection-master\yolo_pipeline.py", line 190, in draw_results
draw_thumbnails(img_cp, img, window_list)
File "C:\Users\mbk\Desktop\MT_Denemeler2\01\vehicle-detection-master\yolo_pipeline.py", line 219, in vehicle_detection_yolo
yolo_result = draw_results(image, image_lane, yolo, fps, lane_info)
File "C:\Users\mbk\Desktop\MT_Denemeler2\01\vehicle-detection-master\main.py", line 10, in pipeline_yolo
output = vehicle_detection_yolo(img_undist, img_lane_augmented, lane_info)
File "C:\users\mbk\desktop\mt_denemeler2\01\vehicle-detection-master&lt;decorator-gen-129>", line 2, in get_frame

RUN ERROR ?

ModuleNotFoundError: No module named 'moviepy'

ModuleNotFoundError: No module named 'cv2'
Please tell me how to solve it?

Problem with other videos

When I use a different video I get an error in np.polyfit that the input is empty and when I see the values of leftx and lefty in lane.py, they are empty arrays. Is there any fix for this.

Did you train yolo on your own?

I wonder if you trained the yolo on KITTI on your own or did you applied the weights from the yolo repo you mentioned directly to KITTI?

I didn't find your training function, and thats why I wonder.

Using yolo_pipeline with camera

Hello JunshengFu,

This is a great project that is done by you, I manage to run this but I have few concerns on this. I want to run this project through by camera live stream and want to show the result on the same window. Do you have any idea how to achieve this? One more thing, is there any compulsion to use it with moviepy.editor, is this possible to use yolo_pipeline with cv2 like cv2.VideoCapture because they're quite standard and powerful apis it seem.

Thanks a lot
Jitender

Could you share the source video of "City Drive"?

A colleague of my mentor asked me to do some modifications on your work and present a demo for him to show off or whatever.Could you please share your source video of "City Drive"?That would help me a lot.

Module not found moviepy.

Installed moviepy using pip in virtualenv and also ffmpeg using brew. Still it throws the same error that module not found.

Anyone ? Solved this issue.

sklearn problem

hello,when i run your demo, i met this problem : ModuleNotFoundError:No module named 'sklearn.preprocessing.data',can you help me to handle this?

OSError: [WinError 193] %1 is not a valid Win32 application

For demo == 2 and demo == 3 it is showing this error.

Loading complete! Traceback (most recent call last): File "main.py", line 47, in <module> clip1 = VideoFileClip('examples/1.mp4').subclip(30,32) File "C:\Users\Hrishikesh\Anaconda3\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 91, in __init__ fps_source=fps_source) File "C:\Users\Hrishikesh\Anaconda3\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 33, in __init__ fps_source) File "C:\Users\Hrishikesh\Anaconda3\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 256, in ffmpeg_parse_infos proc = sp.Popen(cmd, **popen_params) File "C:\Users\Hrishikesh\Anaconda3\lib\subprocess.py", line 676, in __init__ restore_signals, start_new_session) File "C:\Users\Hrishikesh\Anaconda3\lib\subprocess.py", line 957, in _execute_child startupinfo) OSError: [WinError 193] %1 is not a valid Win32 application

How to solve this?

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.