Introduction:

Xiaobian these days have not updated sa! Because I don’t have time for the test simulation ~ from the beginning of learning to today’s training so difficult to go to the ancient sa.

Last night I was still nervous and slowly fell asleep, ran to the exam site early in the morning, waiting for hours, long long ago…

The moment he called me:

Results hung in the last subject, 2333333~ small tears streaming down her face.

Xiaobian has survived in despair thinking for a long time did not update, SO because of the exam, the vehicle has a persistent, SO learning and today to teach you is also about opencV based vehicle detection system!!

The body of the

Think about it, if you can integrate a vehicle detection system into a traffic light camera, you can easily keep track of many useful things at once:

  • How many cars are there at the intersection during the day?

  • What time is the traffic jam?

  • What kind of vehicles (heavy vehicles, cars, etc.) are going through the intersection?

  • Is there a way to optimize traffic and distribute it through different streets?

There are many more examples not to mention. Applications are endless

First environment 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
Copy the code

Save frames in working directory folder 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)
Copy the code

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()
Copy the code

Getting 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 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()
Copy the code

Now we can clearly see the moving target in frames 13 and 14. Everything else that didn’t move was subtracted.

Image preprocessing – added Outlines for all moving vehicles in all frames:

# specify video name
pathOut = 'vehicle_detection_v3.mp4'

# specify frames per second
fps = 14.0
Copy the code

Now 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)
Copy the code

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()
Copy the code

All right! Did you learn?

conclusion

Pray for the next section two exam must pass! Must have! Must pass!!

If you see this, you like this article, remember three lian oh ~ love you.

Source code base: #959755565# welcome everyone to learn to exchange any information can be private letter oh!!