How to monitor the number of goals achieved through Python and OpenCV?

Today we will use the python + OpenCV realize the number of objects in a video monitor, to the effect of video surveillance, such as monitoring the tap the water column color of coal preparation plant, when more than half of the water column is black, then the filter will explain a failure occurs. Not only that, of course, we are looking at the video image processing techniques, you can also migrate to other parts of the project such as this is just an example of it. We know about the image recognition computer vision tasks are four main categories:


 

Category -Classification: solve the "What is?" Question, namely given a picture or a video goal judge which contains what category.

Positioning -Location: solve the "Where?" Question, that is, locate the position of the target.

Detection -Detection: solve the "What is Where??" Question, namely locate the position of the target and know what target Yes.

Segmentation -Segmentation: divided into instances (Instance-level) and the scene division (Scene-level), to solve the problem "of each pixel which belongs to the object or scene," the.

The positioning not only need to find the position of the object where the need to be able to count the number and state of the target object.

In addition to image classification, objective test to solve the problem of infrastructure problems are:

1. The objectives may often appear in any orientation image;

2. Objectives There are various different sizes;

3. There may be a variety of target somewhat different shape.

If a rectangular box to define the purpose, the rectangle has a different resolution. Since the purpose of clarity is different, so using Windows + rotate an image pattern plan classic problem-solving test problems of standardization purposes the cost of production is too low. In recent years, target detection algorithm has made great breakthrough. Popular algorithm can be divided into two categories, one is based on the algorithm of Region Proposal R-CNN (R-CNN, Fast R-CNN, Faster R-CNN, etc.), which are two-stage, the algorithm needs to generate target candidate frame, which is the target location, and then do the candidate frame classification and regression. The other is Yolo, SSD such one-stage algorithm, which uses only a direct convolution neural network CNN predicted category and location of different goals. The first method is the high accuracy of some, but slow, but the second type of algorithm is fast, but the accuracy is lower. Well, today our project and not too much to explain various algorithms, but our core theme, the number of target recognition.

So how will we achieve it

More than just words below begin to realize our projects.

First import the relevant library

import cv2
from PIL import Image
from PIL import ImageDraw,ImageFont
import numpy as np

Then we need to be part of water flowing out of the faucet is extracted, i.e., the need to pre-process images in such a way, to use as a background, as shown in FIG 3ji.jpg named:

And then find the difference image by making a portion of the water column method, first it is necessary to color the image to grayscale and then Gaussian blur ease of calculation, of course, this does not in fact also possible. 2.jpg picture which is tested,

code show as below:

'''3ji是背景图不可换,调试换另一个图片,3ji自己用画图找到水的位置清除掉水柱即可,所以说摄像头不能动'''
firstframe=cv2.imread("3ji.jpg")
firstframe= cv2.cvtColor(firstframe, cv2.COLOR_BGR2GRAY)
firstframe= cv2.GaussianBlur(firstframe, (21, 21), 0)
secondframe0=cv2.imread("2.jpg")
secondframe0= cv2.cvtColor(secondframe0, cv2.COLOR_BGR2GRAY)
secondframe= cv2.GaussianBlur(secondframe0, (21, 21), 0)
frameDelta = cv2.absdiff(firstframe, secondframe)
x,y=frameDelta.shape
print(x,y)

Then find through the water column boundary edge detection for easy viewing.

#frameDelta和canny一个是区域一个是轮廓
img = cv2.GaussianBlur(frameDelta,(3,3),0)
canny = cv2.Canny(img, 0, 100)

The total area of ​​the water column defined variables. Variable water area, ss array storing pixel values ​​of the position

area=0 #6687,总面积
qingarea=0
ss=[]

Then draw the outline, and worth recording the water column at the pixel position

#画轮廓,存储要识别的像素值位置,记录在ss数组中
for i in range(x):
       for j in range(y):
           if any(frameDelta[i,j]!=[0,0,0]):#白色的时候,占位
               ss.append([i,j])

Then add original contour map display, adding to the picture:

canny0=cv2.add(secondframe0,canny)

The size is determined then the pixel color value, debugging the threshold value by item 50

#判断水柱颜色,清水占多少像素
for t in ss:
   k,l=t
   area=area+1
   if canny0[k, l] > 50:
       print(canny0[k,l])
       qingarea+=1
接着统计黑色水柱占比率为多少
deta=(qingarea/area)*100
print(qingarea)
pred="清水占比为"+str(deta)+"%"
print(pred)

Finally, the output image results:

cv2.imwrite("pred.jpg",canny0)
canny0=cv2.imread("pred.jpg")
img_PIL = Image.fromarray(cv2.cvtColor(canny0, cv2.COLOR_BGR2RGB))
myfont = ImageFont.truetype(r'C:/Windows/Fonts/simfang.ttf', 40)
draw = ImageDraw.Draw(img_PIL)
draw.text((200, 10), pred, font=myfont, fill=(255,23,140))
img_OpenCV = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)
cv2.imshow("frame", img_OpenCV)
key = cv2.waitKey(0)

Presentation ultimately results as shown:

Water accounted for 96%, or more accurately

Accounting for 38% of water, accounting for 62% of the black water, is also substantially accurate.

Of course, this is just a matter of thinking, at least examples of projects so far has not been online for the number of objects to be monitored, though not so profound, but it is a good direction to explore. Need more than just intelligent mining, it can also be a smart idea aspect of agriculture or animal husbandry and other intelligent surveillance. Of course, we can also again revised and improved code base, it has been given a complete code above. With the mobile Internet, a slow sustained development platform for mobile phones and various friends, the widespread magnitude greatly enhanced photos, widespread within the scope expanding. Compared to writing, video, video and other widespread way, very widely disseminated photographs "dotting" visual effect, in line with the rhythm of poor people under an efficient method for the reader.

When both photos to give people fast data recording and sharing methods, photo universally widespread in the public attention, the problem is also appropriate follow. Written description, the user can provide wonderful willingness data via keyword search, and when the photo description, users need to adopt difficult to find the photo index may help data.

Changes in science and technology often go hand in hand and express their views addressed in the user pain points, need to improve the high-tech ideas, image recognition has become particularly important after the emergence of new technologies in this natural environment. This also shows the rising status of computer vision.

Guess you like

Origin www.cnblogs.com/7758520lzy/p/12152184.html