Introduction
![](/images/doc/6a493caf40956a57e8696ead64efa1a8.jpg)
Xiaobian hasn't updated these days! Because I don't have time to practice car test simulation ~ it's been a long time since I started learning to practice car today.
![](/images/doc/8be68efed4fb872135e48df47b7169ba.jpg)
Last night, I was still nervous and fell asleep slowly. I ran to the examination site early in the morning and waited for several hours. long long ago......
The moment he shouted to me, he walked in confidently:
![](/images/doc/ea4191d0297731a34cc04b7bcf262dac.jpg)
The result hung on the last subject, 2333333~ little tears streaming down his face.
![](/images/doc/2cad749f8a0f5b9ee112daad14ebf1cd.jpg)
Xiaobian has survived in despair. He hasn't updated it for a long time. Because SO is persistent in the examination and vehicles, what he learns and teaches today is also about the vehicle detection system based on opencv!!!
![](/images/doc/2eb5fa4999de76a196cf127c2abe3152.jpg)
text
Think about it. If you can integrate the vehicle detection system into the traffic light camera, you can easily track many useful things at the same time:
Many people learn Python and don't know where to start. Many people study python,After mastering the basic grammar, I don't know where to find cases. Many people who have been able to learn cases do not know how to learn more advanced knowledge. So for these three types of people, I will provide you with a good learning platform, free access to video tutorials, e-books, and the source code of the course! QQ Group:101677771 Welcome to join us, discuss and study together
- How many cars are there at the intersection during the day?
- When is the traffic jam?
- What kind of vehicles (heavy vehicles, cars, etc.) are passing through the intersection?
- Is there any way to optimize traffic and distribute it through different streets?
There are many examples that will not be listed one by one. Applications are endless~
![](/images/doc/f5b73d2cb00c2d60f15ad09cf603ed49.jpg)
First, install the environment:
Let's import the required libraries and modules - opencv installation:
pip install opencv-python
import os import re import cv2 # opencv library import numpy as np from os.path import isfile, join import matplotlib.pyplot as plt
Save frames in a folder in the working directory and import frames and save:
# get file names of the frames col_frames = os.listdir('frames/') # sort file names col_frames.sort(key=lambda f: int(re.sub('\D', '', f))) # empty list to store the frames col_images=[] for i in col_frames: # read the frames img = cv2.imread('frames/'+i) # append the frames to the list col_images.append(img)
Let's show two consecutive frames:
# plot 13th frame i = 13 for frame in [i, i+1]: plt.imshow(cv2.cvtColor(col_images[frame], cv2.COLOR_BGR2RGB)) plt.title("frame: "+str(frame)) plt.show()
![](/images/doc/d11f9fc3a42129ab3886f44bdf36045b.jpg)
Obtaining the difference between the pixel values of two consecutive frames will help us observe the moving target. So let's use this technique on the above two frames:
# convert the frames to grayscale grayA = cv2.cvtColor(col_images[i], cv2.COLOR_BGR2GRAY) grayB = cv2.cvtColor(col_images[i+1], cv2.COLOR_BGR2GRAY) # plot the image after frame differencing plt.imshow(cv2.absdiff(grayB, grayA), cmap = 'gray') plt.show()
![](/images/doc/a032300b132f33e354accdc301f97fbe.jpg)
Now we can clearly see the moving target in frames 13 and 14. Everything else that hasn't moved has been subtracted.
Image preprocessing - adds contours to all moving vehicles in all frames:
# specify video name pathOut = 'vehicle_detection_v3.mp4' # specify frames per second fps = 14.0
Next, read the last frame in the list:
frame_array = [] files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))] files.sort(key=lambda f: int(re.sub('\D', '', f))) for i in range(len(files)): filename=pathIn + files[i] #read frames img = cv2.imread(filename) height, width, layers = img.shape size = (width,height) #inserting the frames into an image array frame_array.append(img)
Finally, the following code is used to make the target detection video:
out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size) for i in range(len(frame_array)): # writing to a image array out.write(frame_array[i]) out.release()
All right! Have you learned?
![](/images/doc/069caf8ccd5e98e8585b1f1c9b6071f5.jpg)