forked from sarvang00/Traffic-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.py
62 lines (47 loc) · 2.06 KB
/
test2.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
import cv2
import numpy as np
def fun(x):
pass
canvas = np.ones((10, 10), np.uint8)
if __name__ == "__main__":
#Parameters for croping#
top, right, bottom, left = 5, 10, 280, 180
vid = cv2.VideoCapture('latestData.mp4')
refIm = cv2.imread('refFrame.jpg', 0)
while (vid.isOpened()):
ret, frame = vid.read()
(h, w) = frame.shape[:2]
vidClone = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if ret == True:
bg = refIm.copy().astype('float')
diff = cv2.absdiff(bg.astype('uint8'), gray)
thresholded = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
#cv2.imshow('Input', frame)
closing = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, canvas)
#cv2.imshow('closing', closing)
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, canvas)
#cv2.imshow('opening', opening)
#k=cv2.getTrackbarPos("iteration","window")
dilated=cv2.dilate(opening,None,iterations=17)
_,contour,_=cv2.findContours(dilated ,cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_NONE)
#cv2.drawContours(vidClone,contour,-1,(0,255,0),3)
#cv2.imshow("cloned",vidClone)
for i in range(len(contour)):
z=cv2.drawContours(vidClone,contour,i,(0,255,0))
M=cv2.moments(contour[i])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
area=cv2.contourArea(contour[i])
cv2.putText(vidClone,str(area),(cx-10,cy-10),cv2.FONT_HERSHEY_SIMPLEX,0.3, (0, 0, 255), 2)
cv2.imshow("cloned", vidClone)
'''max=0
for i in contour:
print(cv2.contourArea(i))'''
#cv2.imshow("dilated",dilated)
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
break
vid.release
cv2.destroyAllWindows()