-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkm.py
65 lines (43 loc) · 1.59 KB
/
km.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2
backsub = cv2.bgsegm.createBackgroundSubtractorMOG() #background subtraction to isolate moving cars
capture = cv2.VideoCapture("short.mp4") #change to destination on your pc
w = 400
h = 400
i = 0
minArea=75000
x = 0
y = 0
cv2.namedWindow('TRACK', cv2.WINDOW_NORMAL) # Creating a resizable image window
flag = 0
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
erode=cv2.dilate(fgmask,None,iterations=5) #erosion to erase unwanted small contours
moments=cv2.moments(erode,True) #moments method applied
area=moments['m00']
print area
print flag
if flag == 7:
flag = 0
if flag == 0:
if moments['m00'] >=minArea:
x=int(moments['m10']/moments['m00'])
y=int (moments['m01']/moments['m00'])
i = i + 1
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 3)
cv2.imwrite('C:\\TraViol\\cars\\{}.png'.format(i), frame)
flag = flag + 1
'''
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)
# 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)
k = cv2.waitKey(30) & 0xff
if k == 27:
break