Introduction
SO is obsessed with the test and the vehicle, so the study and the teaching today are also about the opencv-based vehicle detection system! ! !
text
Think about it, if you could integrate a vehicle detection system in a traffic light camera, you could easily track a lot of useful things at the same time:
-
How many vehicles 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 a way to optimize traffic and distribute it through different streets?
There are many examples that are not listed one by one. Apps are endless~
First environment installation:
We first 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 the frame in a folder in the working directory and import the frame 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 display 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()
Getting the difference of the pixel values of two consecutive frames will help us to observe moving objects. So, let's use the technique on the two frames above:
# 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()
Now we can clearly see the moving target in frames 13 and 14. Everything else that didn't move is subtracted.
Image preprocessing - added 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 use the following code to make an object 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()
Okay! Did you learn it?
Summarize
I pray that I will pass the next two exams! Must pass! Must pass! !
If you see this, it means that you like this article, remember Sanlian ~ I love you.
In the end, the editor will share with you some python gift packages [Jiajun Yang: 605018913] to help you learn python better!