-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Soumik
committed
Dec 27, 2023
1 parent
defb9c9
commit fb0401f
Showing
2 changed files
with
48 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |