Python?OpenCV識(shí)別行人入口進(jìn)出人數(shù)統(tǒng)計(jì)
前言
這篇博客針對(duì)《Python OpenCV識(shí)別行人入口進(jìn)出人數(shù)統(tǒng)計(jì)》編寫代碼,功能包括了入口行人識(shí)別,人數(shù)統(tǒng)計(jì)。代碼整潔,規(guī)則,易讀。應(yīng)用推薦首選。
一、所需工具軟件
1. Python3.6以上
2. Pycharm代碼編輯器
3. OpenCV, Numpy庫(kù)
二、使用步驟
1.引入庫(kù)
代碼如下(示例):
#導(dǎo)入需要的包 import numpy as np import cv2 import Person import time
2.識(shí)別特征圖像
代碼如下(示例):
video=cv2.VideoCapture("counting_test.avi")
#輸出視頻
fourcc = cv2.VideoWriter_fourcc(*'XVID')#輸出視頻制編碼
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
w = video.get(3)
h = video.get(4)
print("視頻的原寬度為:")
print(int(w))
print("視頻的原高度為:")
area = h*w
print(int(h))
areaTHreshold = area/500
print('Area Threshold', areaTHreshold)
#計(jì)算畫線的位置
line_up = int(1*(h/4))
line_down = int(2.7*(h/4))
up_limit = int(.5*(h/4))
down_limit = int(3.2*(h/4))
print ("Red line y:",str(line_down))
print ("Green line y:", str(line_up))
pt5 = [0, up_limit]
pt6 = [w, up_limit]
pts_L3 = np.array([pt5,pt6], np.int32)
pts_L3 = pts_L3.reshape((-1,1,2))
pt7 = [0, down_limit]
pt8 = [w, down_limit]
pts_L4 = np.array([pt7,pt8], np.int32)
pts_L4 = pts_L4.reshape((-1,1,2))
#背景剔除
# fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True)
fgbg = cv2.createBackgroundSubtractorKNN()
#用于后面形態(tài)學(xué)處理的核
kernel = np.ones((3,3),np.uint8)
kerne2 = np.ones((5,5),np.uint8)
kerne3 = np.ones((11,11),np.uint8)
while(video.isOpened()):
ret,frame=video.read()
if frame is None:
break
#應(yīng)用背景剔除
gray = cv2.GaussianBlur(frame, (31, 31), 0)
#cv2.imshow('GaussianBlur', frame)
#cv2.imshow('GaussianBlur', gray)
fgmask = fgbg.apply(gray)
fgmask2 = fgbg.apply(gray)
try:
#***************************************************************
#二值化
ret,imBin= cv2.threshold(fgmask,200,255,cv2.THRESH_BINARY)
ret,imBin2 = cv2.threshold(fgmask2,200,255,cv2.THRESH_BINARY)
#cv2.imshow('imBin', imBin2)
#開操作(腐蝕->膨脹)消除噪聲
mask = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kerne3)
mask2 = cv2.morphologyEx(imBin2, cv2.MORPH_OPEN, kerne3)
#閉操作(膨脹->腐蝕)將區(qū)域連接起來(lái)
mask = cv2.morphologyEx(mask , cv2.MORPH_CLOSE, kerne3)
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kerne3)
#cv2.imshow('closing_mask', mask2)
#*************************************************************
except:
print('EOF')
print ('IN:',cnt_in+count_in)
print ('OUT:',cnt_in+count_in)
break
#找到邊界
_mask2,contours0, hierarchy = cv2.findContours(mask2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours0:
rect = cv2.boundingRect(cnt)#矩形邊框
area=cv2.contourArea(cnt)#每個(gè)矩形框的面積
if area>areaTHreshold:
#************************************************
#moments里包含了許多有用的信息
M=cv2.moments(cnt)
cx=int(M['m10']/M['m00'])#計(jì)算重心
cy=int(M['m01']/M['m00'])
x, y, w, h = cv2.boundingRect(cnt)#x,y為矩形框左上方點(diǎn)的坐標(biāo),w為寬,h為高
new=True
if cy in range(up_limit,down_limit):
for i in persons:
if abs(cx-i.getX())<=w and abs(cy-i.getY())<=h:
new=False
i.updateCoords(cx,cy)
if i.going_UP(line_down,line_up)==True:
# cv2.circle(frame, (cx, cy), 5, line_up_color, -1)
# img = cv2.rectangle(frame, (x, y), (x + w, y + h), line_up_color, 2)
if w>80:
count_in=w/40
print("In:執(zhí)行了/60")
time.strftime("%c"))
elif i.going_DOWN(line_down,line_up)==True:
# cv2.circle(frame, (cx, cy), 5, (0, 0, 255), -1)
# img = cv2.rectangle(frame, (x, y), (x + w, y + h), line_down_color, 2)
time.strftime("%c"))
break
#狀態(tài)為1表明
if i.getState() == '1':
if i.getDir() == 'down' and i.getY() > down_limit:
i.setDone()
elif i.getDir() == 'up' and i.getY() < up_limit:
i.setDone()
if i.timedOut():
# 已經(jīng)記過數(shù)且超出邊界將其移出persons隊(duì)列
index = persons.index(i)
persons.pop(index)
del i # 清楚內(nèi)存中的第i個(gè)人
if new == True:
p = Person.MyPerson(pid, cx, cy, max_p_age)
persons.append(p)
pid += 1
print("進(jìn)入的總?cè)藬?shù)為:")
print(cnt_in)
print("出去的總?cè)藬?shù)為:")
print(cnt_out)
video.release();
cv2.destroyAllWindows()3.運(yùn)行結(jié)果如下:

到此這篇關(guān)于Python OpenCV識(shí)別行人入口進(jìn)出人數(shù)統(tǒng)計(jì)的文章就介紹到這了,更多相關(guān)OpenCV人數(shù)統(tǒng)計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- OpenCV基于背景減除實(shí)現(xiàn)行人計(jì)數(shù)
- 基于opencv的行人檢測(cè)(支持圖片視頻)
- Python+OpenCV內(nèi)置方法實(shí)現(xiàn)行人檢測(cè)
- Python+OpenCV進(jìn)行人臉面部表情識(shí)別
- python+opencv3.4.0 實(shí)現(xiàn)HOG+SVM行人檢測(cè)的示例代碼
- 結(jié)合OpenCV與TensorFlow進(jìn)行人臉識(shí)別的實(shí)現(xiàn)
- python使用opencv進(jìn)行人臉識(shí)別
- python中使用OpenCV進(jìn)行人臉檢測(cè)的例子
相關(guān)文章
Python機(jī)器學(xué)習(xí)之隨機(jī)梯度下降法的實(shí)現(xiàn)
如果當(dāng)我們數(shù)據(jù)量和樣本量非常大時(shí),每一項(xiàng)都要參與到梯度下降,那么它的計(jì)算量時(shí)非常大的,所以我們需要采用隨機(jī)梯度下降法。本文介紹了Python實(shí)現(xiàn)隨機(jī)梯度下降法的方法,希望對(duì)大家有所幫助2023-02-02
Python訪問純真IP數(shù)據(jù)庫(kù)腳本分享
這篇文章主要介紹了Python訪問純真IP數(shù)據(jù)庫(kù)腳本分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06
python 實(shí)現(xiàn)二維字典的鍵值合并等函數(shù)
今天小編就為大家分享一篇python 實(shí)現(xiàn)二維字典的鍵值合并等函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12

