From dec3207f1e4e1765b16d70466f2001d4d3ed4e14 Mon Sep 17 00:00:00 2001 From: dhananjaymenon <72432304+dhananjaymenon@users.noreply.github.com> Date: Sat, 5 Jun 2021 18:05:49 +0400 Subject: [PATCH] Create SpeedRadar2.py --- SpeedRadar2.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 SpeedRadar2.py diff --git a/SpeedRadar2.py b/SpeedRadar2.py new file mode 100644 index 0000000..1833f76 --- /dev/null +++ b/SpeedRadar2.py @@ -0,0 +1,102 @@ +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("Resources/traffic4.mp4") +f = 25 +w = int(1000/(f-1)) +print(w) + + +#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() + 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)