Skip to content

Commit

Permalink
Added patch for 2nd video
Browse files Browse the repository at this point in the history
  • Loading branch information
Soumik committed Dec 27, 2023
1 parent defb9c9 commit fb0401f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
22 changes: 14 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# importing librarys
import cv2 as cv
import model1
from model1 import model1

# loading video

capture=cv.VideoCapture("example_video/test1.mp4")

video_id=2
capture=cv.VideoCapture(f"example_video/test{video_id}.mp4")
ret,frame=capture.read()

# loading first frame
Expand All @@ -17,6 +16,16 @@
cars_count=0
playing=True

#choosing parameter
if video_id==1:
y_up=180
y_down=310
car_size=25
elif video_id==2 :
y_up=100
y_down=310
car_size=80
model= model1(y_up,y_down,car_size)
#Press D to Quit the video

#video loop
Expand All @@ -30,12 +39,9 @@
frame=cv.resize(frame,(int(frame.shape[1]/2),int(frame.shape[0]/2)))

# returns no of vehicles
valid=model1.find_cars(prev_frame,frame)
valid=model.find_cars(prev_frame,frame)
cv.putText(prev_frame,f"Vehicle Detected:{len(valid)}",(55,15),cv.FONT_HERSHEY_COMPLEX,0.6,(0,180,0),2)
cv.imshow('Sample2',prev_frame)
# else :
# playing=False


capture.release()

62 changes: 34 additions & 28 deletions src/model1.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import cv2 as cv
import numpy as np

algorithm=cv.bgsegm.createBackgroundSubtractorMOG()
y_level_up=180
y_level_down=310

def find_cars(prev_frame,frame) :
prev_frame_gray=cv.cvtColor(prev_frame,cv.COLOR_BGR2GRAY);
frame_gray=cv.GaussianBlur(cv.cvtColor(frame,cv.COLOR_BGR2GRAY),(3,3),5);
img_sub=algorithm.apply(frame_gray)
dialated=cv.dilate(img_sub,np.ones((5,5)))
kernel=cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
dialated=cv.morphologyEx(dialated,cv.MORPH_CLOSE,kernel)
dialated=cv.morphologyEx(dialated,cv.MORPH_CLOSE,kernel)
contours,hierarchy=cv.findContours(dialated.copy(),cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
class model1():
"""docstring for model1"""
algorithm=cv.bgsegm.createBackgroundSubtractorMOG()

valid=[]
for i,cntr in enumerate(contours) :
x,y,w,h=cv.boundingRect(cntr)
#right lane cars
if y>=y_level_up and y<=y_level_down-h and x<=int(frame.shape[1])/2 and w>25 and h>25:
valid.append(cntr)
cv.rectangle(prev_frame,(x,y),(x+w,y+h),(0,255,0),thickness=1)
def __init__(self,y_level_up,y_level_down,car_size):
self.y_level_up = y_level_up
self.y_level_down=y_level_down
self.car_size=car_size

#left lane cars
if y>=y_level_up and y<=y_level_down-h and x>int(frame.shape[1])/2 and w>25 and h>25:
valid.append(cntr)
cv.rectangle(prev_frame,(x,y),(x+w,y+h),(0,0,255),thickness=1)
#upper line
cv.line(prev_frame,(0,y_level_up),(int(frame.shape[1]),y_level_up),(0,255,0))
#lower line
cv.line(prev_frame,(0,y_level_down),(int(frame.shape[1]),y_level_down),(0,255,0))
return valid
def find_cars(self,prev_frame,frame) :
prev_frame_gray=cv.cvtColor(prev_frame,cv.COLOR_BGR2GRAY);
frame_gray=cv.GaussianBlur(cv.cvtColor(frame,cv.COLOR_BGR2GRAY),(3,3),5);
img_sub=self.algorithm.apply(frame_gray)
dialated=cv.dilate(img_sub,np.ones((5,5)))
kernel=cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
dialated=cv.morphologyEx(dialated,cv.MORPH_CLOSE,kernel)
dialated=cv.morphologyEx(dialated,cv.MORPH_CLOSE,kernel)
contours,hierarchy=cv.findContours(dialated.copy(),cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)

valid=[]
for i,cntr in enumerate(contours) :
x,y,w,h=cv.boundingRect(cntr)
#right lane cars
if y>=self.y_level_up and y<=self.y_level_down-h and x<=int(frame.shape[1])/2 and w>self.car_size and h>self.car_size:
valid.append(cntr)
cv.rectangle(prev_frame,(x,y),(x+w,y+h),(0,255,0),thickness=1)

#left lane cars
if y>=self.y_level_up and y<=self.y_level_down-h and x>int(frame.shape[1])/2 and w>self.car_size and h>self.car_size:
valid.append(cntr)
cv.rectangle(prev_frame,(x,y),(x+w,y+h),(0,0,255),thickness=1)
#upper line
cv.line(prev_frame,(0,self.y_level_up),(int(frame.shape[1]),self.y_level_up),(0,255,0))
#lower line
cv.line(prev_frame,(0,self.y_level_down),(int(frame.shape[1]),self.y_level_down),(0,255,0))
return valid

0 comments on commit fb0401f

Please sign in to comment.