# ====================================================
# ================== DEFINE CLASS ====================
# ====================================================
class Position(object):
def __init__(self, _x, _y, _w, _h):
self.x = _x
self.y = _y
self.w = _w
self.h = _h
def x(self):
return self.x
def y(self):
return self.y
def w(self):
return self.w
def h(self):
return self.h
class People(object):
def __init__(self, _x, _y, _w, _h, _roi, _hue):
# Position
self.x = _x
self.y = _y
self.w = _w
self.h = _h
self.roi = _roi
# Display of the contour while tracking
self.hue = _hue
self.color = hsv2rgb(self.hue%1, 1, 1)
# Motion Descriptors
self.center = [_x + _w/2, _y + _h/2]
self.isIn = checkPosition(boundaryPt1, boundaryPt2, self.center, inCriterion)
self.isInChangeFrameCount = toleranceCountIOStatus
self.speed = [0,0]
self.missingCount = 0
# ROI - Region of Interest
self.maxRoi = _roi
self.roi = _roi
def x(self):
return self.x
def y(self):
return self.y
def w(self):
return self.w
def h(self):
return self.h
def roi(self):
return self.roi
def color(self):
return self.color
def center(self):
return self.center
def maxRoi(self):
return self.maxRoi
def isIn(self):
return self.isIn
def speed(self):
return self.speed
def missingCount(self):
return self.missingCount
def isInChangeFrameCount(self):
return self.isInChangeFrameCount
def set(self, name, value):
if name == "x":
self.x = value
elif name == "y":
self.y = value
elif name == "w":
self.w = value
elif name == "h":
self.h = value
elif name == "center":
self.center = value
elif name == "roi":
self.roi = value
# Automatically update maxRoi as roi is updated
if self.roi.shape[0]*self.roi.shape[1] > self.maxRoi.shape[0]*self.maxRoi.shape[1]:
self.maxRoi = self.roi
elif name == "speed":
self.speed = value
elif name == "missingCount":
self.missingCount = value
elif name == "isIn":
self.isIn = value
elif name == "isInChangeFrameCount":
self.isInChangeFrameCount = value
else:
return
# ====================================================
# ===================== FUNCTION =====================
# ====================================================
def averageSize():
sum = 0
for i in humanSizeSample:
sum +=i
return sum/sampleSize
# Only care about top and bottom
def checkTouchVSide(x, y, w, h, maxW, maxH, tolerance):
if x <= 0:
return True
elif y - tolerance <= 0:
return True
elif x + w >= maxW:
return True
elif y + h + tolerance >= maxH:
return True
else:
return False
def hsv2rgb(h, s, v):
return tuple(i * 255 for i in colorsys.hsv_to_rgb(h, s, v))
def checkPosition(boundaryPt1, boundaryPt2, currPos, inCriterion):
m = (boundaryPt2[1] - boundaryPt1[1])/(boundaryPt2[0] - boundaryPt1[0])
c = boundaryPt2[1] - m*boundaryPt2[0]
if inCriterion == "<":
if currPos[0] * m + c < currPos[1]:
return True
else:
return False
elif inCriterion == ">":
if currPos[0] * m + c > currPos[1]:
return True
else:
return False
else:
return False
def nothing(x):
pass
# ====================================================
# ================== VIDEO SOURCE ====================
# ====================================================
srcTest = 'peopleCounter.avi'
srcWebcam = 0
srcMain = '' # live source here
cap = cv2.VideoCapture(srcTest) # Open video file
# ====================================================
# ================== PRE-CONFIG ======================
# ====================================================
minArea = 500 # default min area to be considered person
maxArea = 4000 # default max area to be considered person
noFrameToCollectSample = 100
toleranceRange = 50 # use for error calculation
toleranceCount = 10 # maximum number of frame an object need to present in order to be accepted
toleranceCountIOStatus = 3 # minimum number of frame between In/Out Status change -> prevent playing with the system
startHue = 0 # In HSV this is RED
hueIncrementValue = 0.1 # increment color every time to differentiate between different people