Skip to content

Commit 725b20c

Browse files
committed
Car Counting with python
0 parents  commit 725b20c

5 files changed

Lines changed: 211 additions & 0 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### car-counting-with-python
2+
3+
```
4+
Car counting process made with Numpy, opencv on python.
5+
```
6+
if you wanna to watch the video as result of this project you can click and watch on youtube
7+
- for counting_car.py_1 project you can watch this video : [![Watch the video](https://youtu.be/Oq7JGBhgvl4)](https://youtu.be/Oq7JGBhgvl4)
8+
- for counting_car.py_2 project you can watch this video : [![Watch the video](https://www.youtube.com/watch?v=qm-Ha_ZrGrw)](https://www.youtube.com/watch?v=qm-Ha_ZrGrw)
9+
10+
#### requirements
11+
- pycharm
12+
- python 3+
13+
- numpy
14+
- cv2 (opencv)
15+
#### İmportant
16+
There is a few problem in these projects. These problems will be solved soon.

counting_car_1.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import cv2
2+
import numpy as np
3+
4+
class Kordinat:
5+
def __init__(self,x,y):
6+
self.x=x
7+
self.y=y
8+
9+
class Sensor:
10+
def __init__(self,kordinat1,kordinat2,frame_weight,frame_lenght):
11+
self.kordinat1=kordinat1
12+
self.kordinat2=kordinat2
13+
self.frame_weight=frame_weight
14+
self.frame_lenght =frame_lenght
15+
self.mask=np.zeros((frame_weight,frame_lenght,1),np.uint8)*abs(self.kordinat2.y-self.kordinat1.y)
16+
self.full_mask_area=abs(self.kordinat2.x-self.kordinat1.x)
17+
cv2.rectangle(self.mask,(self.kordinat1.x,self.kordinat1.y),(self.kordinat2.x,self.kordinat2.y),(255),thickness=cv2.FILLED)
18+
self.stuation=False
19+
self.car_number_detected=0
20+
21+
22+
video=cv2.VideoCapture("video1.mp4")
23+
ret,frame=video.read()
24+
cropped_image= frame[0:450, 0:450]
25+
fgbg=cv2.createBackgroundSubtractorMOG2()
26+
Sensor1 = Sensor(
27+
Kordinat(1, cropped_image.shape[1] - 35),
28+
Kordinat(340, cropped_image.shape[1] - 30),
29+
cropped_image.shape[0],
30+
cropped_image.shape[1])
31+
32+
kernel=np.ones((5,5),np.uint8)
33+
font=cv2.FONT_HERSHEY_TRIPLEX
34+
while (1):
35+
ret,frame=video.read()
36+
# resize frame
37+
cropped_image= frame[0:450, 0:450]
38+
# make morphology for frame
39+
deleted_background=fgbg.apply(cropped_image)
40+
opening_image=cv2.morphologyEx(deleted_background,cv2.MORPH_OPEN,kernel)
41+
ret,opening_image=cv2.threshold(opening_image,125,255,cv2.THRESH_BINARY)
42+
43+
# detect moving anything
44+
_, cnts,_=cv2.findContours(opening_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
45+
result=cropped_image.copy()
46+
47+
zeros_image=np.zeros((cropped_image.shape[0], cropped_image.shape[1], 1), np.uint8)
48+
49+
# detect moving anything with loop
50+
for cnt in cnts:
51+
x,y,w,h=cv2.boundingRect(cnt)
52+
if (w>75 and h>75 and w<160 and h<160 ):
53+
cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),thickness=2)
54+
cv2.rectangle(zeros_image,(x,y),(x+w,y+h),(255),thickness=cv2.FILLED)
55+
56+
# detect whether there is car via bitwise_and
57+
mask1=np.zeros((zeros_image.shape[0],zeros_image.shape[1],1),np.uint8)
58+
mask_result=cv2.bitwise_or(zeros_image,zeros_image,mask=Sensor1.mask)
59+
white_cell_number=np.sum(mask_result==255)
60+
61+
# detect to control whether car is passing under the red line sensor
62+
sensor_rate=white_cell_number/Sensor1.full_mask_area
63+
if sensor_rate>0:
64+
print("result : ",sensor_rate)
65+
# if car is passing under the red line sensor . red line sensor is yellow color.
66+
67+
if (sensor_rate>=0.9 and sensor_rate<3.1 and Sensor1.stuation==False):
68+
# draw the red line
69+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
70+
(0,255, 0,), thickness=cv2.FILLED)
71+
Sensor1.stuation = True
72+
elif (sensor_rate<0.9 and Sensor1.stuation==True) :
73+
# draw the red line
74+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
75+
(0, 0,255), thickness=cv2.FILLED)
76+
Sensor1.stuation = False
77+
Sensor1.car_number_detected+=1
78+
else :
79+
# draw the red line
80+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
81+
(0, 0, 255), thickness=cv2.FILLED)
82+
83+
84+
85+
cv2.putText(result,str(Sensor1.car_number_detected),(Sensor1.kordinat1.x,150),font,2,(255,255,255))
86+
87+
88+
cv2.imshow("video", result)
89+
#cv2.imshow("mask_result", mask_result)
90+
#cv2.imshow("zeros_image", zeros_image)
91+
#cv2.imshow("opening_image", opening_image)
92+
93+
k=cv2.waitKey(30) & 0xff
94+
if k == 27 :
95+
break
96+
97+
video.release()
98+
cv2.destroyAllWindows()

counting_car_2.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import cv2
2+
import numpy as np
3+
4+
class Kordinat:
5+
def __init__(self,x,y):
6+
self.x=x
7+
self.y=y
8+
9+
class Sensor:
10+
def __init__(self,kordinat1,kordinat2,frame_weight,frame_lenght):
11+
self.kordinat1=kordinat1
12+
self.kordinat2=kordinat2
13+
self.frame_weight=frame_weight
14+
self.frame_lenght =frame_lenght
15+
self.mask=np.zeros((frame_weight,frame_lenght,1),np.uint8)*abs(self.kordinat2.y-self.kordinat1.y)
16+
self.full_mask_area=abs(self.kordinat2.x-self.kordinat1.x)
17+
cv2.rectangle(self.mask,(self.kordinat1.x,self.kordinat1.y),(self.kordinat2.x,self.kordinat2.y),(255),thickness=cv2.FILLED)
18+
self.stuation=False
19+
self.car_number_detected=0
20+
21+
22+
Sensor1 = Sensor(Kordinat(1, 425), Kordinat(1080, 430), 500, 1080)
23+
video=cv2.VideoCapture("video2.mp4")
24+
fgbg=cv2.createBackgroundSubtractorMOG2()
25+
#fgbg=cv2.createBackgroundSubtractorMOG2()
26+
kernel=np.ones((5,5),np.uint8)
27+
font=cv2.FONT_HERSHEY_TRIPLEX
28+
while (1):
29+
ret,frame=video.read()
30+
# resize frame
31+
cut_image=frame[100:600,100:1180]
32+
# make morphology for frame
33+
deleted_background=fgbg.apply(cut_image)
34+
opening_image=cv2.morphologyEx(deleted_background,cv2.MORPH_OPEN,kernel)
35+
ret,opening_image=cv2.threshold(opening_image,125,255,cv2.THRESH_BINARY)
36+
37+
# detect moving anything
38+
_, cnts,_=cv2.findContours(opening_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
39+
result=cut_image.copy()
40+
41+
zeros_image=np.zeros((cut_image.shape[0],cut_image.shape[1],1),np.uint8)
42+
43+
# detect moving anything with loop
44+
for cnt in cnts:
45+
x,y,w,h=cv2.boundingRect(cnt)
46+
if (w>100 and h>100 ):
47+
cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),thickness=2)
48+
cv2.rectangle(zeros_image,(x,y),(x+w,y+h),(255),thickness=cv2.FILLED)
49+
50+
# detect whether there is car via bitwise_and
51+
mask1=np.zeros((zeros_image.shape[0],zeros_image.shape[1],1),np.uint8)
52+
mask_result=cv2.bitwise_or(zeros_image,zeros_image,mask=Sensor1.mask)
53+
white_cell_number=np.sum(mask_result==255)
54+
55+
# detect to control whether car is passing under the red line sensor
56+
sensor_rate=white_cell_number/Sensor1.full_mask_area
57+
if sensor_rate>0:
58+
print(sensor_rate)
59+
60+
# if car is passing under the red line sensor . red line sensor is yellow color.
61+
if (sensor_rate >= 1.8 and sensor_rate<2.9 and Sensor1.stuation == False):
62+
# draw the red line
63+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
64+
(0, 0, 255), thickness=cv2.FILLED)
65+
Sensor1.stuation = False
66+
Sensor1.car_number_detected += 2
67+
if (sensor_rate>=0.6 and sensor_rate<1.8 and Sensor1.stuation==False):
68+
# draw the red line
69+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
70+
(0,255, 0,), thickness=cv2.FILLED)
71+
Sensor1.stuation = True
72+
elif (sensor_rate<0.6 and Sensor1.stuation==True) :
73+
# draw the red line
74+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
75+
(0, 0,255), thickness=cv2.FILLED)
76+
Sensor1.stuation = False
77+
Sensor1.car_number_detected+=1
78+
else :
79+
# draw the red line
80+
cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y),
81+
(0, 0, 255), thickness=cv2.FILLED)
82+
83+
84+
cv2.putText(result,str(Sensor1.car_number_detected),(Sensor1.kordinat1.x,Sensor1.kordinat1.y+60),font,2,(255,255,255))
85+
86+
87+
cv2.imshow("video", result)
88+
#cv2.imshow("mask_result", mask_result)
89+
#cv2.imshow("zeros_image", zeros_image)
90+
#cv2.imshow("opening_image", opening_image)
91+
92+
k=cv2.waitKey(30) & 0xff
93+
if k == 27 :
94+
break
95+
96+
video.release()
97+
cv2.destroyAllWindows()

video1.mp4

59.3 MB
Binary file not shown.

video2.mp4

6.49 MB
Binary file not shown.

0 commit comments

Comments
 (0)