-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_01.py
184 lines (169 loc) · 5.32 KB
/
st_01.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import time
from FPS.utils import FPS
from detect import *
from line.line import *
from loguru_config.config import *
from playsound import playsound
from undistort import undistort
def init():
"""
部分设置初始化
"""
setlog()
cap_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
lane_resize = 2
LeftLine.set_img(cap_height // lane_resize, cap_width // lane_resize)
RightLine.set_img(cap_height // lane_resize, cap_width // lane_resize)
MidLine.set_img(cap_height // lane_resize, cap_width // lane_resize)
MidLine.upper_x = 160
MidLine.lower_x = 160
if not cap.isOpened():
logger.error('Cannot open video file')
exit()
return time.time(), 0, 0, cap_width, cap_height
def lane(img):
"""
跑道检测
"""
resize_img = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
lane_img = show_lane(resize_img, 2)
return lane_img
def find(img, mode, flag=0):
"""
物块检测、特征匹配
:param img: 传入图片
:param mode: 检测模式
:param flag: 偏差
"""
global anti_shake
# 斑马新检测
if mode == 1:
if ZebraCross_find(img):
playsound('Audio/doorbell.mp3')
mode += 1
# 变道识别
elif mode == 2:
if anti_shake == 0:
roi_img = img[240:480, 240:400]
if A4_find(roi_img):
anti_shake = 1
elif anti_shake == 1:
roi_img = img[320:480, 200:440]
change = line_change(roi_img)
if change == 1:
print(f'Left {blue_left_matcher.goodnum} {blue_right_matcher.goodnum}')
anti_shake = 0
mode += 1
flag = 1
elif change == 2:
print(f'Right {blue_left_matcher.goodnum} {blue_right_matcher.goodnum}')
anti_shake = 0
mode += 1
flag = 2
# 锥桶检测
elif mode == 3:
if yellow_cone_detect.sum < 3:
if anti_shake == 0:
roi_img = img[240:480, 200:440]
cv2.imshow('cone_roi', roi_img)
cone_bool = cone_detect(roi_img, yellow_cone, yellow_cone_detect)
if cone_bool:
logger.info(f'{yellow_cone_detect.sum} Cone Has Found')
flag = yellow_cone_detect.sum
anti_shake = 15
else:
anti_shake -= 1
else:
anti_shake = 0
mode += 1
# A、B停车
elif mode == 4:
if anti_shake == 0:
roi_img = img[320:480, 80:560]
if A4_find(roi_img):
anti_shake = 1
elif anti_shake == 1:
parking = AB_recognition(img)
if parking == 1:
flag = 1
print(f'Left A {blue_A_matcher.goodnum} {blue_B_matcher.goodnum}')
elif parking == 2:
flag = 2
print(f'Right B {blue_A_matcher.goodnum} {blue_B_matcher.goodnum}')
return img, mode, flag
def angle_calc(angle, mode, flag):
"""
角度计算
@param angle: 以舵机中值为作为0参考
@param mode: find函数mode
@param flag: find函数flag
@return:
"""
angle = angle + (MidLine.upper_x - width // 4) * 0.0005
if mode == 2:
if flag == 1:
angle += 5
time.sleep(1)
angle -= 5
time.sleep(1)
elif flag == 2:
angle -= 5
time.sleep(1)
angle += 5
time.sleep(1)
elif mode == 3:
if flag == 1 or flag == 3:
angle += 2
time.sleep(0.5)
angle -= 2
time.sleep(0.5)
elif flag == 2:
angle -= 2
time.sleep(0.5)
angle += 2
time.sleep(0.5)
elif mode == 4:
if flag == 1:
angle += 5
time.sleep(1)
angle -= 5
time.sleep(1)
elif flag == 2:
angle -= 5
time.sleep(1)
angle += 5
time.sleep(1)
return angle
if __name__ == '__main__':
cap = cv2.VideoCapture('Video/output_20240919_112114.mp4')
fps = FPS().start()
start_time, frame_count, anti_shake, width, height = init()
find_mode = 1
find_flag = 0
angle = 0
try:
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
if not width == 640:
frame = cv2.resize(frame, (640, 480))
# lane(frame)
_, find_mode, find_flag = find(frame, find_mode)
# angle = angle_calc(angle, find_mode, find_flag)
# print(f'\rDrop Rate: {MidLine.lose_count / frame_count * 100:.2f}% '
# f'error_num: {MidLine.error_num} {MidLine.upper_x - width // 4} '
# f'angle: {angle}', end=' ')
if find_mode == 5:
break
if cv2.waitKey(50) & 0xFF == ord('q'):
break
fps.update()
except KeyboardInterrupt:
logger.error('Ctrl+C pressed')
fps.stop()
cap.release()
logger.info(f'Elapsed Time: {fps.elapsed():.2f} Approx. FPS: {fps.fps():.2f}')
logger.info('Program finished')