Exemple #1
0
def count(mask, frame):
    global areaTH, cars, pt1, pt2, upLimit, downLimit, lineUp, lineDown, counterDown, counterUp, pid, max_p_age

    contours, _ = cv2.findContours(
        mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > areaTH:
            M = cv2.moments(cnt)
            cx = int(M['m10']/M['m00'])
            cy = int(M['m01']/M['m00'])
            x, y, w, h = cv2.boundingRect(cnt)

            newCar = True
            if cy in range(upLimit, downLimit):
                for i in cars:
                    if abs(cx - i.getX()) <= w and abs(cy - i.getY() <= h):
                        newCar = False
                        i.updateCoords(cx, cy)
                        if i.going_UP(lineDown, lineUp) == True:
                            counterUp += 1
                            print(f'up: {str(counterUp)}')
                        elif i.going_DOWN(lineDown, lineUp) == True:
                            counterDown += 1
                            print(f'down: {str(counterDown)}')
                        break
                    if i.getState() == '1':
                        if i.getDir() == 'down' and i.getY() > downLimit:
                            i.setDone()
                        elif i.getDir() == 'up' and i.getY() < downLimit:
                            i.setDone()
                    if i.timedOut():
                        index = cars.index(i)
                        cars.pop(index)
                        del i
                if newCar == True:
                    p = Car.MyCar(pid, cx, cy, max_p_age)
                    cars.append(p)
                    pid += 1
            cv2.circle(frame, (cx, cy), 5, (0, 0, 255), -1)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    for i in cars:
        cv2.putText(frame, str(i.getId()), (i.getX(), i.getY()),
                    font, 0.3, i.getRGB(), 1, cv2.LINE_AA)
Exemple #2
0
         #   TRACKING    #
         #################            
         M = cv2.moments(cnt)
         cx = int(M['m10']/M['m00'])
         cy = int(M['m01']/M['m00'])
         x,y,w,h = cv2.boundingRect(cnt)
         
         new = True
         for i in cars:
             if abs(x-i.getX()) <= w and abs(y-i.getY()) <= h:
                 # the object is close to one that was already detected before
                 new = False
                 i.updateCoords(cx,cy)  #Update coordinates on the object and resets age
                 break
         if new == True:
             p = Car.MyCar(pid,cx,cy, max_p_age)
             cars.append(p)
             pid += 1     
         cv2.circle(frame,(cx,cy), 5, (0,0,255), -1)
         img = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)            
         cv2.drawContours(frame, cnt, -1, (0,255,0), 3)
 for i in cars:
     if len(i.getTracks()) >= 2:
         pts = np.array(i.getTracks(), np.int32)
         pts = pts.reshape((-1,1,2))
         frame = cv2.polylines(frame,[pts],False,i.getRGB())
     if i.getId() == 9:
         print (str(i.getX()), ',', str(i.getY()))
     cv2.putText(frame, str(i.getId()),(i.getX(),i.getY()),font,0.3,i.getRGB(),1,cv2.LINE_AA)
  
 
Exemple #3
0
                    #If state is '1', where '1' is already counted and '0' is default (not yet counted)
                    if i.getState() == '1':
                        #If direction is up/down and Y coordinate is past tracking limits, set done
                        if i.getDir() == 'down' and i.getY() > down_limit:
                            i.setDone()
                        elif i.getDir() == 'up' and i.getY() < up_limit:
                            i.setDone()
                    #If car has not been detected in max_c_age amount of frames...
                    if i.timedOut():
                        #Pop car from index, then delete the entry
                        index = cars.index(i)
                        cars.pop(index)
                        del i
                if new == True:
                    #Create new object from class Car for tracking purposes
                    c = Car.MyCar(cid, cx, cy, max_c_age)
                    #Append new object to array
                    cars.append(c)
                    #Increment car ID counter
                    cid += 1

            #Draw green contours around the object with a red dot identifying the middle
            cv2.circle(frame, (cx, cy), 5, (0, 0, 255), -1)
            img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.drawContours(frame, cont, -1, (0, 255, 0), 3)

            #This loop draws lines showing vectors of objects between frames
            for i in cars:
                if len(i.getTracks()) >= 2:
                    cts = np.array(i.getTracks(), np.int32)
                    cts = cts.reshape((-1, 1, 2))