-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·132 lines (106 loc) · 3.89 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
#coding: utf-8
import detect
import draw
import cartesian
import numpy as np
import cv2
import sys
import time
VIDEO_SOURCE = sys.argv[1]
MIN_AREA = 250
MAX_DISTANCE = 20
MEDIA_BLUR = 7
BLUR = 7
SENSIBILITY = 10
N_FRAMES_OUT = 15
FRAMES_LEARN = 100
'Parâmetros que definem as linhas de contagem da estrada'
'Padrão: [(x1, y1), (x2, y2)]'
ROAD_LINE_LEFT = [(60, 180), (210, 180)]
ROAD_LINE_RIGHT = [(210, 180), (350, 180)]
'Função de aprendizado do background'
def learnSub(backsub, video_source, n_frames=50):
capture = cv2.VideoCapture(VIDEO_SOURCE)
while capture.isOpened():
ret, frame = capture.read()
bkframe = backsub.apply(frame, None, 0.01)
if capture.get(1) == n_frames:
capture.release()
break
return bkframe
'Logger do buffer'
def logger(buffer, frame_id, count_left, count_right):
print('Left: ', count_left)
print('Right: ', count_right)
print('Total: ', count_left+count_right)
print('Frame: ', frame_id)
for v in buffer:
print(v, cartesian.distance(v['centroid'], (0,0)))
print('\n')
'Função principal'
def main():
buffer_vehicles = list()
vehicle_counter_left = 0
vehicle_counter_right = 0
backsub = cv2.bgsegm.createBackgroundSubtractorMOG(nmixtures=3)
bkframe = learnSub(backsub, VIDEO_SOURCE, FRAMES_LEARN)
capture = cv2.VideoCapture(VIDEO_SOURCE)
width = int(capture.get(3))
#Trecho de código utilizado para os testes de entrada
if 'video.mp4' in VIDEO_SOURCE:
ROAD_LINE_LEFT = [(60, 180), (210, 180)]
ROAD_LINE_RIGHT = [(210, 180), (350, 180)]
elif 'video2.mp4' in VIDEO_SOURCE:
ROAD_LINE_LEFT = [(0, 220), (175, 220)]
ROAD_LINE_RIGHT = [(175, 220), (350, 220)]
elif 'video3.mp4' in VIDEO_SOURCE:
ROAD_LINE_LEFT = [(0, 220), (170, 220)]
ROAD_LINE_RIGHT = [(170, 220), (350, 220)]
elif 'video4.mp4' in VIDEO_SOURCE:
ROAD_LINE_LEFT = [(0, 200), (180, 200)]
ROAD_LINE_RIGHT = [(180, 200), (350, 200)]
cv2.namedWindow('Background')
cv2.moveWindow('Background', 400, 0)
cv2.namedWindow('Track')
while True:
try:
frame_id = int(capture.get(1))
ret, frame = capture.read()
bkframe = backsub.apply(frame, None, 0.01)
bkframe = cv2.medianBlur(bkframe, MEDIA_BLUR)
bkframe = cv2.blur(bkframe, (BLUR,BLUR))
num, labels, stats, centroids = cv2.connectedComponentsWithStats(bkframe, ltype=cv2.CV_16U)
count_left, count_right, buffer_vehicles, frame = detect.detectVehicle(
stats,
centroids,
frame,
frame_id,
buffer_vehicles,
ROAD_LINE_LEFT,
ROAD_LINE_RIGHT)
vehicle_counter_left += count_left
vehicle_counter_right += count_right
logger(buffer_vehicles, frame_id, vehicle_counter_left, vehicle_counter_right)
draw.drawPanel(
frame,
ROAD_LINE_LEFT,
ROAD_LINE_RIGHT,
vehicle_counter_left,
vehicle_counter_right,
width
)
cv2.imshow('Track', frame)
cv2.imshow('Background', bkframe)
#cv2.imwrite('img/normal/background/'+str(frame_id)+'f.png', bkframe)
if cv2.waitKey(100) == ord('q') or not capture.isOpened():
break
except Exception as e:
print(e)
break
count_left, count_right, buffer_vehicles = detect.countVehicles(buffer_vehicles, frame_id, MAX_DISTANCE, N_FRAMES_OUT, final=True)
vehicle_counter_left += count_left
vehicle_counter_right += count_right
logger(buffer_vehicles, frame_id, vehicle_counter_left, vehicle_counter_right)
if __name__ == '__main__':
main()