Forked from bigsnarfdude/gist:510115144111ac81441cfb7009c3fdb0
Created
April 21, 2017 13:11
-
-
Save shadimsaleh/2947f5a63287e454fff4f4a0e4b7b04a to your computer and use it in GitHub Desktop.
[traffic] counting opencv python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://www.youtube.com/watch?v=Y3ac5rFMNZ0&t=318s | |
| # avconv -i rtsp://admin:admin@192.168.1.68/play1.sdp -c copy -map 0 -f segment -segment_time 300 -segment_format mp4 "capture-%03d-`date +%Y-%m-%d_%H:%M:%S`.mp4" | |
| import cv2 | |
| backsub = cv2.BackgroundSubtractorMOG() #background subtraction to isolate moving cars | |
| capture = cv2.VideoCapture("/home/ubuntu/Downloads/traffic_video.avi") | |
| i = 0 | |
| minArea=1 | |
| while True: | |
| ret, frame = capture.read() | |
| fgmask = backsub.apply(frame, None, 0.01) | |
| erode=cv2.erode(fgmask,None,iterations=3) #erosion to erase unwanted small contours | |
| moments=cv2.moments(erode,True) #moments method applied | |
| area=moments['m00'] | |
| if moments['m00'] >=minArea: | |
| x=int(moments['m10']/moments['m00']) | |
| y=int (moments['m01']/moments['m00']) | |
| if x>40 and x<55 and y>50 and y<65: #range of line coordinates for values on left lane | |
| i=i+1 | |
| print(i) | |
| elif x>102 and x<110 and y>105 and y<130: #range of line coordinatess for values on right lane | |
| i=i+1 | |
| print(i) | |
| #print(x,y) | |
| cv2.putText(frame,'COUNT: %r' %i, (10,30), cv2.FONT_HERSHEY_SIMPLEX, | |
| 1, (255, 0, 0), 2) | |
| cv2.imshow("Track", frame) | |
| cv2.imshow("background sub", fgmask) | |
| key = cv2.waitKey(100) | |
| if key == ord('q'): | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment