It's said that people earn 100000 a month in violation of regulations? I wrote an automatic detection system to check whether the vehicle is illegal

Introduction

 

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.

 

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:

 

The result hung on the last subject, 2333333~ little tears streaming down his face.

 

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!!!

 

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~

 

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()

 

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()

 

 

 

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?

Keywords: Python

Added by johnsiilver on Mon, 10 Jan 2022 10:04:03 +0200