小言_互联网的博客

Python OpenCV识别行人入口进出人数统计

291人阅读  评论(0)

 程序示例精选

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. if area>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. if abs(cx-i.getX())<=w and abs(cy-i.getY())<=h:
  81. new=False
  82. i.updateCoords(cx,cy)
  83. if i.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. if w> 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. if i.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.运行结果如下: 

三、在线协助: 

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!


转载:https://blog.csdn.net/alicema1111/article/details/128407152
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场