Build AI Car and Pedestrian Tracking with Python for Beginners

Nidhi punj
3 min readAug 9, 2020

--

Let me tell you, It’s very simple.

Step 1: Get A lot of Car Images

Step 2: Make them All Black and White

Grayscale images make the algorithm faster. Color increases the complexity of the model or we can say gray-colored images are used to simplify the mathematics. For example, one can talk about brightness, contrast, edges, shape, contours, texture, perspective, shadows, and so on, without addressing color.

Step 3: Train the Algorithm to Detect Cars

Now the question arrives: How does the computer train the algorithm?

https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html

We just find matches.

We can match the above features to actually detect the backward bumper of the cars as shown below.

The black part of the rectangle will help with the back mirror(darker one) and the lighter part of the car would be learned by the lighter part of the edge feature.

The same exact idea with pedestrians.

The line feature will be used for pedestrian detection.

It’s all about matching the feature or shape. If something matches with the above feature, the model will detect it as a pedestrian.

Let’s start coding our detector.

Step 1: We first need to install the OpenCV library.
pip install opencv-python if this doesn’t work try
pip install opencv-python-headless
If you are still not able to install. Try googling, How to install opencv on your computer?

Step 2: Download Machine Learning Files (Haar Cascade xml files):
We already have pre-trained Car and human full body (pedestrian) classifiers available and we just need to download it.
Car pre-trained classifier: https://raw.githubusercontent.com/andrewssobral/vehicle_detection_haarcascades/master/cars.xml
Human Full Body pre-trained classifier: https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_fullbody.xml

Step 3: We need to write just 20 lines of code. You can understand it just by reading the code.

GitHub link of Code: https://github.com/nidhipunj7/AI-Car-and-Pedestrian-Tracking

Result:

Green Boxes represents pedestrian and Red boxes are for cars.

Referred Links:
https://www.youtube.com/watch?v=zg9X6ASj3Q0&t=5269s
https://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html?highlight=detectmultiscale

--

--