Ask Your Question
0

About tracking a centroid of contour and a people counter

asked 2018-06-29 05:24:27 -0600

AkshayNarla gravatar image

updated 2018-07-02 05:48:22 -0600

berak gravatar image

I am using python for opencv programming and i'm developing a people counter using it. I've drawn the contours and found its centroid. But i am not able to track the centroids so that i can use their positions in 2 different frames to see if the contour has entered or exited from the room. Please help me with this.

import datetime
import numpy as np
import cv2 as cv
#functions for counter
def EntranceCrossing(y, ent, exi):
    if (ent-y<10 and y<ent):
        return 1
    else:
        return 0
def ExitCrossing(y, ent, exi):
    if (y-exi<8 and y>exi):    #there is a possibility that during entry also this'll be calculated
        return 1
    else:
        return 0
#video capture
var=cv.VideoCapture('sample-02.mp4')
fgbg = cv.bgsegm.createBackgroundSubtractorMOG()
EntranceCounter= 0
ExitCounter= 0

while True:
#if grabbed enter loop else break    
    (grabbed, frame) = var.read()
    text = "No Video"
    if not grabbed:
        break
#adjusting frame size and blurring  
    frame = cv.resize(frame, ( 480,480 ))
    gray= cv.cvtColor(frame,cv.COLOR_BGR2GRAY, cv.CV_8UC1)

    resize = cv.GaussianBlur( gray,(21,21),0)
#background subtraction
    fgmask= fgbg.apply(resize)
    ret, th3 = cv.threshold(fgmask ,25,200,cv.THRESH_BINARY+cv.THRESH_OTSU)
#smoothing and filling holes
    blur = cv.blur(th3,(5,5))
    kernel = np.ones((5,5),np.uint8)
    dil= cv.dilate( blur,kernel,iterations = 1)

    ret, th3 = cv.threshold(dil,0,50,cv.THRESH_BINARY+cv.THRESH_OTSU)
#contours and tracking    
    im2, contours, hierarchy = cv.findContours(th3.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    cv.drawContours(im2, contours, -1, (200,50,50), 2)
    #grab all contours and draw rectangles and their centroids in original frame    
    for c in contours:
        if cv.contourArea(c) < 6000:
            continue
        (x,y,w,h)= cv.boundingRect(c)
        cv.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        Centroid = (int((x + x + w) /2), int((y + y + h) /2))
        YCentroid= (y+y+h)/2
        cv.circle(frame, Centroid, 1, (0, 0, 255), 4)
#counter functions
        if (ExitCrossing(YCentroid,350,350)):
            ExitCounter += 1
        if (EntranceCrossing(YCentroid,350,350)):
            EntranceCounter += 1
#display the output        
    cv.line(frame,(0,350),(480,350),(0,255,0),1)
    cv.putText(frame, "WentIn:"+format(str(EntranceCounter)),(10,332),cv.FONT_HERSHEY_SIMPLEX,.5,(0,255,0))
    cv.putText(frame, "WentOut:"+format(str(ExitCounter)),(10,365),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,255))
    cv.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
               (10, frame.shape[0] - 10), cv.FONT_HERSHEY_SIMPLEX, 0.35, (255, 0, 0), 1)
    cv.putText(frame, "Inside:"+format(str(EntranceCounter-ExitCounter)),(10,320),cv.FONT_HERSHEY_SIMPLEX,.5,(0,0,0))
    cv.imshow('PeopleCount', frame)


    if cv.waitKey(1) & 0xFF==ord('q'):
         break
var.release()
cv.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

show us, what you've tried, so far.

berak gravatar imageberak ( 2018-06-29 05:25:43 -0600 )edit

Help me with this so that i can track properly and count it right

AkshayNarla gravatar imageAkshayNarla ( 2018-07-02 05:33:54 -0600 )edit

Hey berak whats wrong with this??. Please let me know. I'm in a bit of hurry to finish this. So help me out someone

AkshayNarla gravatar imageAkshayNarla ( 2018-07-02 06:08:45 -0600 )edit

Hi Akshay, now entry and exit both will be incremented for a centroid by just crossing the line.I think you can use a centroid tracker, provide unique ID to them and store them in a list to keep track of detected centroids.This tutorial will be much helpful for you in centroid tracking

Anand Simmy gravatar imageAnand Simmy ( 2018-07-31 01:32:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-07-31 01:34:50 -0600

Hi Akshay, now entry and exit both will be incremented for a centroid by just crossing the line.I think you can use a centroid tracker, provide unique ID to them and store them in a list to keep track of detected centroids.This tutorial will be much helpful for you in centroid tracking

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-29 05:24:27 -0600

Seen: 3,458 times

Last updated: Jul 02 '18