[Tutor] How to count vehicle? I'm trying but my code don't increasing..
Alan Gauld
alan.gauld at btinternet.com
Mon Sep 1 18:13:38 CEST 2014
On 01/09/14 16:13, Whees Northbee wrote:
> Hello, I'm trying to counting vehicle, but I can't increasing it, I use
> python 2.7.6 and opencv 2.4.9
I don;t understand what you mean.
There is no vehicle in your code?
The code is overly complex for me to read through and it uses a module
that I don't have so I'm not even attempting to run it... And it looks
like it has a syntax error too - an if block missing.
Can you provide more details about what is happening?
Also try inserting some print statements so that you can
see what your code is doing.
That way you might see the problem yourself.
> 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 code to increase counter
> if dy==0:
> if (cx<=320)and(cx>=70):
> counter=counter+1
>
> if cy==170:
>
> cv2.putText(frame, str(counter),(10,150),
> cv2.FONT_HERSHEY_SIMPLEX,2, (255, 0, 0), 1, True)
> #cv2.namedWindow('Output',cv2.cv.CV_WINDOW_NORMAL)
> #cv2.resizeWindow('Output',320,180)
> cv2.imshow('Output', frame)
> cv2.imshow('FGMASK', fgmask)
>
>
> key = cv2.waitKey(100)
> if key == ord('q'):
> break
> cap.release()
> cv2.destroyAllWindows()
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list