-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeedRadar2.py
103 lines (78 loc) · 2.79 KB
/
SpeedRadar2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import cv2
from tracker2 import *
import numpy as np
end = 0
#Creater Tracker Object
tracker = EuclideanDistTracker()
#cap = cv2.VideoCapture("Resources/traffic3.mp4")
cap = cv2.VideoCapture(r"C:\Users\DELL\Downloads\SpeedRadar-OpenCV--main\SpeedRadar-OpenCV--main\traffic4.mp4")
f = 25
w = int(1000/(f-1))
#Object Detection
object_detector = cv2.createBackgroundSubtractorMOG2(history=None,varThreshold=None)
#100,5
#KERNALS
kernalOp = np.ones((3,3),np.uint8)
kernalOp2 = np.ones((5,5),np.uint8)
kernalCl = np.ones((11,11),np.uint8)
fgbg=cv2.createBackgroundSubtractorMOG2(detectShadows=True)
kernal_e = np.ones((5,5),np.uint8)
while True:
ret,frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, None, fx=0.5, fy=0.5)
height,width,_ = frame.shape
#print(height,width)
#540,960
#Extract ROI
roi = frame[50:540,200:960]
#MASKING METHOD 1
mask = object_detector.apply(roi)
_, mask = cv2.threshold(mask, 250, 255, cv2.THRESH_BINARY)
#DIFFERENT MASKING METHOD 2 -> This is used
fgmask = fgbg.apply(roi)
ret, imBin = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY)
mask1 = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kernalOp)
mask2 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, kernalCl)
e_img = cv2.erode(mask2, kernal_e)
contours,_ = cv2.findContours(e_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
detections = []
for cnt in contours:
area = cv2.contourArea(cnt)
#THRESHOLD
if area > 1000:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(roi,(x,y),(x+w,y+h),(0,255,0),3)
detections.append([x,y,w,h])
#Object Tracking
boxes_ids = tracker.update(detections)
for box_id in boxes_ids:
x,y,w,h,id = box_id
if(tracker.getsp(id)<tracker.limit()):
cv2.putText(roi,str(id)+" "+str(tracker.getsp(id)),(x,y-15), cv2.FONT_HERSHEY_PLAIN,1,(255,255,0),2)
cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 3)
else:
cv2.putText(roi,str(id)+ " "+str(tracker.getsp(id)),(x, y-15),cv2.FONT_HERSHEY_PLAIN, 1,(0, 0, 255),2)
cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 165, 255), 3)
s = tracker.getsp(id)
if (tracker.f[id] == 1 and s != 0):
tracker.capture(roi, x, y, h, w, s, id)
# DRAW LINES
cv2.line(roi, (0, 410), (960, 410), (0, 0, 255), 2)
cv2.line(roi, (0, 430), (960, 430), (0, 0, 255), 2)
cv2.line(roi, (0, 235), (960, 235), (0, 0, 255), 2)
cv2.line(roi, (0, 255), (960, 255), (0, 0, 255), 2)
#DISPLAY
#cv2.imshow("Mask",mask2)
#cv2.imshow("Erode", e_img)
cv2.imshow("ROI", roi)
key = cv2.waitKey(w-10)
if key==27:
tracker.end()
end=1
break
if(end!=1):
tracker.end()
cap.release()
cv2.destroyAllWindows()