[Tutor] How to count vehicle? I'm trying but my code don't increasing..

Whees Northbee ch.de2.2309 at gmail.com
Mon Sep 1 17:20:02 CEST 2014


Hello, I'm trying to counting vehicle, but I can't increasing it, I use
python 2.7.6 and opencv 2.4.9
Here the code:

import cv2

bgsMOG = cv2.BackgroundSubtractorMOG2(500,30,True)
cap         = cv2.VideoCapture("d:\MOV_5680.avi")
counter    =0
if cap:
    while True:
        ret, frame = cap.read()

        if ret:
            fgmask = bgsMOG.apply(frame, None, 0.01)
            cv2.line(frame,(20,170),(320,170),(175,175,0),1)
            contours, hierarchy = cv2.findContours(fgmask,
cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

            ax1=20     #coordinate of line where vehicle will be count if
intersect
            ay1=170
            ax2=320
            ay2=170

            try: hierarchy = hierarchy[0]
            except: hierarchy = []
            for contour, hier in zip(contours, hierarchy):
                (x,y,w,h) = cv2.boundingRect(contour)

                if w > 10 and h > 15:
                    cv2.rectangle(frame, (x,y), (x+w,y+h), (180, 0, 0), 1)

                    x1=w/2      #to find centroid
                    y1=h/2
                    cx=x+x1
                    cy=y+y1
                    centroid=(cx,cy)
                    cv2.circle(frame,(int(cx),int(cy)),1,(0,255,0),-1)


                    dy=cy-170   #my first try to increase counter, not work
                    if dy==0:
                        if (cx<=320)and(cx>=70):
                            counter=counter+1

                    if cy==170:   #my second try to increasing, not work too
                         counter=counter+1


            cv2.putText(frame, str(counter),(10,150),
cv2.FONT_HERSHEY_SIMPLEX,2, (255, 0, 0), 1, True)
            cv2.imshow('Output', frame)
            cv2.imshow('FGMASK', fgmask)


            key = cv2.waitKey(100)
            if key == ord('q'):
                break
cap.release()
cv2.destroyAllWindows()



don't have error but I can't increasing the counter, please help me..
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140901/1443d516/attachment-0001.html>


More information about the Tutor mailing list