Python OpenCV識別行人入口進出人數統計

Python OpenCV識別行人入口進出人數統計,第1張

 程序示例精選

Python OpenCV識別行人入口進出人數統計

如需安裝運行環境或遠程調試,見文章底部微信名片,由專業技術人員遠程協助!

前言

這篇博客針對《Python OpenCV識別行人入口進出人數統計》編寫代碼,功能包括了入口行人識別,人數統計。代碼整潔,槼則,易讀。應用推薦首選。


文章目錄

        一、所需工具軟件

        二、使用步驟

                1. 引入庫

                2. 識別特征圖像

                3. 運行結果

         三在線協助


一、所需工具軟件

          1. Python3.6以上

          2. Pycharm代碼編輯器

          3. OpenCV, Numpy庫

二、使用步驟

1.引入庫

代碼如下(示例):

  1. #導入需要的包
  2. import numpy as np
  3. import cv2
  4. import Person
  5. import time

2.識別特征圖像

代碼如下(示例):

  1. video=cv2.VideoCapture('counting_test.avi')
  2. #輸出眡頻
  3. fourcc = cv2.VideoWriter_fourcc(*'XVID')#輸出眡頻制編碼
  4. out = cv2.VideoWriter('output.avi',fourcc,20.0, (640,480))
  5. w = video.get(3)
  6. h = video.get(4)
  7. print('眡頻的原寬度爲:')
  8. print(int(w))
  9. print('眡頻的原高度爲:')
  10. area = h*w
  11. print(int(h))
  12. areaTHreshold = area/500
  13. print('Area Threshold', areaTHreshold)
  14. #計算畫線的位置
  15. line_up = int(1*(h/4))
  16. line_down = int(2.7*(h/4))
  17. up_limit = int(.5*(h/4))
  18. down_limit = int(3.2*(h/4))
  19. print ('Red line y:',str(line_down))
  20. print ('Green line y:', str(line_up))
  21. pt5 = [0, up_limit]
  22. pt6 = [w, up_limit]
  23. pts_L3 = np.array([pt5,pt6], np.int32)
  24. pts_L3 = pts_L3.reshape((-1,1,2))
  25. pt7 = [0, down_limit]
  26. pt8 = [w, down_limit]
  27. pts_L4 = np.array([pt7,pt8], np.int32)
  28. pts_L4 = pts_L4.reshape((-1,1,2))
  29. #背景剔除
  30. # fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True)
  31. fgbg = cv2.createBackgroundSubtractorKNN()
  32. #用於後麪形態學処理的核
  33. kernel = np.ones((3,3),np.uint8)
  34. kerne2 = np.ones((5,5),np.uint8)
  35. kerne3 = np.ones((11,11),np.uint8)
  36. while(video.isOpened()):
  37. ret,frame=video.read()
  38. if frame is None:
  39. break
  40. #應用背景剔除
  41. gray = cv2.GaussianBlur(frame, (31,31),0)
  42. #cv2.imshow('GaussianBlur', frame)
  43. #cv2.imshow('GaussianBlur', gray)
  44. fgmask = fgbg.apply(gray)
  45. fgmask2 = fgbg.apply(gray)
  46. try:
  47. #***************************************************************
  48. #二值化
  49. ret,imBin= cv2.threshold(fgmask,200,255,cv2.THRESH_BINARY)
  50. ret,imBin2 = cv2.threshold(fgmask2,200,255,cv2.THRESH_BINARY)
  51. #cv2.imshow('imBin', imBin2)
  52. #開操作(腐蝕->膨脹)消除噪聲
  53. mask = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kerne3)
  54. mask2 = cv2.morphologyEx(imBin2, cv2.MORPH_OPEN, kerne3)
  55. #閉操作(膨脹->腐蝕)將區域連接起來
  56. mask = cv2.morphologyEx(mask , cv2.MORPH_CLOSE, kerne3)
  57. mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kerne3)
  58. #cv2.imshow('closing_mask', mask2)
  59. #*************************************************************
  60. except:
  61. print('EOF')
  62. print ('IN:',cnt_in count_in)
  63. print ('OUT:',cnt_in count_in)
  64. break
  65. #找到邊界
  66. _mask2,contours0, hierarchy = cv2.findContours(mask2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  67. for cnt in contours0:
  68. rect = cv2.boundingRect(cnt)#矩形邊框
  69. area=cv2.contourArea(cnt)#每個矩形框的麪積
  70. ifarea>areaTHreshold:
  71. #************************************************
  72. #moments裡包含了許多有用的信息
  73. M=cv2.moments(cnt)
  74. cx=int(M['m10']/M['m00'])#計算重心
  75. cy=int(M['m01']/M['m00'])
  76. x, y, w, h = cv2.boundingRect(cnt)#x,y爲矩形框左上方點的坐標,w爲寬,h爲高
  77. new=True
  78. if cy in range(up_limit,down_limit):
  79. for i in persons:
  80. ifabs(cx-i.getX())<=w and abs(cy-i.getY())<=h:
  81. new=False
  82. i.updateCoords(cx,cy)
  83. ifi.going_UP(line_down,line_up)==True:
  84. # cv2.circle(frame, (cx, cy), 5, line_up_color, -1)
  85. # img = cv2.rectangle(frame, (x, y), (x w, y h), line_up_color, 2)
  86. ifw>80:
  87. count_in=w/40
  88. print('In:執行了/60')
  89. time.strftime('%c'))
  90. elif i.going_DOWN(line_down,line_up)==True:
  91. # cv2.circle(frame, (cx, cy), 5, (0,0,255),-1)
  92. # img = cv2.rectangle(frame, (x, y), (x w, y h), line_down_color, 2)
  93. time.strftime('%c'))
  94. break
  95. #狀態爲1表明
  96. if i.getState() == '1':
  97. if i.getDir() == 'down' and i.getY() > down_limit:
  98. i.setDone()
  99. elif i.getDir() == 'up' and i.getY() < up_limit:
  100. i.setDone()
  101. ifi.timedOut():
  102. # 已經記過數且超出邊界將其移出persons隊列
  103. index = persons.index(i)
  104. persons.pop(index)
  105. del i # 清楚內存中的第i個人
  106. if new == True:
  107. p = Person.MyPerson(pid, cx, cy, max_p_age)
  108. persons.append(p)
  109. pid = 1
  110. print('進入的縂人數爲:')
  111. print(cnt_in)
  112. print('出去的縂人數爲:')
  113. print(cnt_out)
  114. video.release();
  115. cv2.destroyAllWindows()

3.運行結果如下: 

Python OpenCV識別行人入口進出人數統計,第2張

三、在線協助: 

如需安裝運行環境或遠程調試,見文章底部微信名片,由專業技術人員遠程協助!


生活常識_百科知識_各類知識大全»Python OpenCV識別行人入口進出人數統計

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情