Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. import math
  5. import cv2
  6. import numpy as np
  7.  
  8. width = 0
  9. height = 0
  10. EntranceCounter = 0
  11. ExitCounter = 0
  12. MinCountourArea = 3000
  13. BinarizationThreshold = 70
  14. OffsetRefLines = 150
  15.  
  16.  
  17. def CheckEntranceLineCrossing(y, CoorYEntranceLine, CoorYExitLine):
  18. AbsDistance = abs(y - CoorYEntranceLine)
  19. if AbsDistance <= 2 and y < CoorYExitLine:
  20. return 1
  21. else:
  22. return 0
  23.  
  24.  
  25. def CheckExitLineCrossing(y, CoorYEntranceLine, CoorYExitLine):
  26. AbsDistance = abs(y - CoorYExitLine)
  27.  
  28. if AbsDistance <= 2 and y > CoorYEntranceLine:
  29. return 1
  30. else:
  31. return 0
  32.  
  33.  
  34. camera = cv2.VideoCapture(0)
  35.  
  36. camera.set(3, 640)
  37. camera.set(4, 480)
  38.  
  39. ReferenceFrame = None
  40.  
  41. for i in range(0, 20):
  42. (grabbed, Frame) = camera.read()
  43.  
  44. while True:
  45. (grabbed, Frame) = camera.read()
  46. height = np.size(Frame, 0)
  47. width = np.size(Frame, 1)
  48.  
  49. if not grabbed:
  50. break
  51.  
  52. GrayFrame = cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)
  53. GrayFrame = cv2.GaussianBlur(GrayFrame, (21, 21), 0)
  54.  
  55. if ReferenceFrame is None:
  56. ReferenceFrame = GrayFrame
  57. continue
  58.  
  59. FrameDelta = cv2.absdiff(ReferenceFrame, GrayFrame)
  60. FrameThresh = cv2.threshold(FrameDelta, BinarizationThreshold, 255,
  61. cv2.THRESH_BINARY)[1]
  62.  
  63. FrameThresh = cv2.dilate(FrameThresh, None, iterations=2)
  64. (_, cnts, _) = cv2.findContours(FrameThresh.copy(),
  65. cv2.RETR_EXTERNAL,
  66. cv2.CHAIN_APPROX_SIMPLE)
  67.  
  68. QttyOfContours = 0
  69.  
  70. CoorYEntranceLine = height / 2 - OffsetRefLines
  71. CoorYExitLine = height / 2 + OffsetRefLines
  72. cv2.line(Frame, (0, CoorYEntranceLine), (width, CoorYEntranceLine),
  73. (255, 0, 0), 2)
  74. cv2.line(Frame, (0, CoorYExitLine), (width, CoorYExitLine), (0, 0,
  75. 255), 2)
  76.  
  77. for c in cnts:
  78. if cv2.contourArea(c) < MinCountourArea:
  79. continue
  80.  
  81. QttyOfContours = QttyOfContours + 1
  82.  
  83. (x, y, w, h) = cv2.boundingRect(c)
  84. cv2.rectangle(Frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  85.  
  86. CoordXCentroid = (x + x + w) / 2
  87. CoordYCentroid = (y + y + h) / 2
  88. ObjectCentroid = (CoordXCentroid, CoordYCentroid)
  89. cv2.circle(Frame, ObjectCentroid, 1, (0, 0, 0), 5)
  90.  
  91. if CheckEntranceLineCrossing(CoordYCentroid, CoorYEntranceLine,
  92. CoorYExitLine):
  93. EntranceCounter += 1
  94.  
  95. if CheckExitLineCrossing(CoordYCentroid, CoorYEntranceLine,
  96. CoorYExitLine):
  97. ExitCounter += 1
  98.  
  99. print 'Total countours found: ' + str(QttyOfContours)
  100.  
  101. cv2.putText(
  102. Frame,
  103. 'Entrances: {}'.format(str(EntranceCounter)),
  104. (10, 50),
  105. cv2.FONT_HERSHEY_SIMPLEX,
  106. 0.5,
  107. (250, 0, 1),
  108. 2,
  109. )
  110. cv2.putText(
  111. Frame,
  112. 'Exits: {}'.format(str(ExitCounter)),
  113. (10, 70),
  114. cv2.FONT_HERSHEY_SIMPLEX,
  115. 0.5,
  116. (0, 0, 255),
  117. 2,
  118. )
  119. cv2.imshow('Original Frame', Frame)
  120. cv2.waitKey(1)
  121.  
  122. camera.release()
  123. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement