该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
# Process Contours
for c in contours:
# Filter Contour By Size
if len(humanSizeSample) < 100:
if cv2.contourArea(c) < minArea or cv2.contourArea(c) > maxArea:
continue
else:
humanSizeSample.append(cv2.contourArea(c))
else:
if cv2.contourArea(c) < averageArea/2 or cv2.contourArea(c) > averageArea*3:
continue
(x, y, w, h) = cv2.boundingRect(c)
detectedContours.append(Position(x, y, w, h))
# Process Detected People
if len(detectedPeople) != 0:
for people in detectedPeople:
# Setup Meanshift/Camshift for Tracking
track_window = (people.x, people.y, people.w, people.h)
hsv_roi = pOpening[people.y:people.y + people.h, people.x:people.x + people.w]
mask = cv2.inRange(hsv_roi, np.array(128), np.array(256))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [100], [0, 256])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 1, 1) # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
dst = cv2.calcBackProject([opening], [0], roi_hist, [0, 256], 1)
ret, track_window = cv2.CamShift(dst, track_window, term_criteria)
# Process POST Tracking
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
img2 = cv2.polylines(frameForView, [pts], True, people.color, 2)
pos = sum(pts)/len(pts)
isFound = False
for dC in detectedContours:
if dC.x - toleranceRange < pos[0] < dC.x + dC.w + toleranceRange \
and dC.y - toleranceRange < pos[1] < dC.y + dC.h + toleranceRange:
people.set("x", dC.x)
people.set("y", dC.y)
people.set("w", dC.w)
people.set("h", dC.h)
people.set("speed", pos - people.center)
people.set("center", pos)
people.set("missingCount", 0)
detectedContours.remove(dC)
isFound = True
tR = getExteriorRect(pts)
people.set("roi", frame[tR[1]:tR[1]+tR[3], tR[0]:tR[0]+tR[2]])
# Process Continuous Tracking
prevInStatus = people.isIn
currInStatus = checkPosition(boundaryPt1, boundaryPt2, people.center, inCriterion)
people.isIn = currInStatus
# Check In/Out Status Change
if prevInStatus != currInStatus and people.isInChangeFrameCount >= toleranceCountIOStatus:
if not allowPassage:
passImage = people.roi
people.set("isInChangeFrameCount", 0)
if currInStatus:
peopleIn += 1
if not allowPassage:
peopleViolationIn += 1
else:
peopleOut += 1
if not allowPassage:
peopleViolationOut += 1
else:
people.set("isInChangeFrameCount", people.isInChangeFrameCount + 1)
# Process DIS-continuous Tracking
if not isFound:
if people.missingCount > toleranceCount:
detectedPeople.remove(people)
else:
if checkTouchVSide(people.x + people.speed[0], people.y + people.speed[1], people.w,
people.h, maxWidth, maxHeight, toleranceRange):
detectedPeople.remove(people)
else:
people.set("missingCount", people.missingCount+1)
people.set("x", people.x + people.speed[0])
people.set("y", people.y + people.speed[1])
people.set("center", people.center + people.speed)
# Check New People
for dC in detectedContours:
if checkTouchVSide(dC.x, dC.y, dC.w, dC.h, maxWidth, maxHeight, toleranceRange):
startHue += hueIncrementValue
detectedPeople.append(People(dC.x, dC.y, dC.w, dC.h, frame[dC.y:dC.y+dC.h, dC.x:dC.x+dC.w], startHue))
# RE-set
detectedContours = []
pFrame = frame
pNoBg = noBg
pOpening = opening
frameCounter += 1
# Output
try:
# Setup Stats
textNoOfPeople = "People: " + str(len(detectedPeople))
textNoIn = "In: " + str(peopleIn)
textNoOut = "Out: " + str(peopleOut)
textNoViolationIn = "In: " + str(peopleViolationIn)
textNoViolationOut = "Out: " + str(peopleViolationOut)
if allowPassage:
cv2.line(frameForView, (long(boundaryPt1[0]), long(boundaryPt1[1])),
(long(boundaryPt2[0]), long(boundaryPt2[1])), (0, 255, 0), 2)
else:
cv2.line(frameForView, (long(boundaryPt1[0]), long(boundaryPt1[1])),
(long(boundaryPt2[0]), long(boundaryPt2[1])), (0, 0, 255), 2)
# Draw Infos
cv2.putText(frameInfo, textNoOfPeople, (30, 40), cv2.FONT_HERSHEY_SIMPLEX
, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frameInfo, textNoIn, (30, 80), cv2.FONT_HERSHEY_SIMPLEX
, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frameInfo, textNoOut, (30, 120), cv2.FONT_HERSHEY_SIMPLEX
, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.line(frameInfo, (0, 160), (640, 160), (255, 255, 255), 1)
cv2.putText(frameInfo, "VIOLATION", (30, 200), cv2.FONT_HERSHEY_SIMPLEX
, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frameInfo, textNoViolationIn, (30, 240), cv2.FONT_HERSHEY_SIMPLEX
, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frameInfo, textNoViolationOut, (30, 280), cv2.FONT_HERSHEY_SIMPLEX
, 1, (255, 255, 255), 1, cv2.LINE_AA)
# Display
cv2.imshow('FrameForView', frameForView)
# cv2.imshow('Frame', frame)
if passImage != None:
cv2.imshow('Violators', passImage)
cv2.imshow('config', frameInfo)
except:
print('EOF')
break
# Abort and exit with 'Q' or ESC
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# else:
# cv2.imwrite(chr(k) + ".jpg", frame)
cap.release()
cv2.destroyAllWindows()