-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat42_new.py
248 lines (213 loc) · 8.15 KB
/
at42_new.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import numpy as np
import time
import tensorflow as tf
from robotPi import robotPi
import cv2
from rev_cam import rev_cam # 摄像头倒转添加
import os
import time
from robotpi_movement import Movement
from robotpi_Cmd import UPComBotCommand
# 1:[1,0,0,0] 前
# 2:[0,1,0,0] 左
# 3:[0,0,1,0] 右
# 4:[0,0,0,1] 后
yuzhi = 110#110 er zhi hua
forwardspeed = 20#20
left_threshold = 1.8e+07
# ssumyuzhi is the ending yuzhi (way:put thr robot at the ending)
ssumyuzhi = 2.0e+07
# the time of arriving ending
xianzhitime = 36#36
move_times = 2
forwardspeedend = 20
fw_time = 1000#110
pfkernel = np.ones((10, 10), np.uint8)
robot = robotPi()
width = 480
height = 180
channel = 1
inference_path = tf.Graph()
filepath = os.getcwd() + '/model/472/-472'
# 104 is model name
filepath2 = os.getcwd() + '/model/472/-472'
flag = 0
temp_image = np.zeros(width * height * channel, 'uint8')
cap = cv2.VideoCapture(0)
#tiao jie ji ba dong zuo zuo you wei zhi
def adjust():
print("adjusting")
mv = Movement()
while True:
if cap.isOpened():
ret, frame = cap.read()
frame = rev_cam(frame) # 摄像头倒转添加
resized_height = int(width * 0.75)
# 计算缩放比例
frame = cv2.resize(frame, (width, resized_height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, frame = cv2.threshold(frame, yuzhi, 255, cv2.THRESH_BINARY)
# slice the lower part of a frame
res = frame[resized_height - height:, :]
cv2.waitKey(1)
cv2.imshow("frame", res)
frame = np.array(res, dtype=np.float32)
ssum = frame.sum()
print(ssum)
if ssum > left_threshold:
print("------------moving left--------------")
mv.move_left(speed=15)
# time.sleep(0.2)
else:
# pass
break
# def moveRight():
# for i in range(move_times):
# mv.move_right(speed=15)
# time.sleep(0.5)
# print("------------moving right-------------")
# moveRight()
# mv.move_right()
time.sleep(2)
robot.movement.move_forward(speed=forwardspeedend, times=fw_time)
time.sleep(1)
def auto_pilot():
# a = np.array(frame, dtype=np.float32)
# _, prediction = model.predict(a.reshape(1, width * height))
# cap = cv2.VideoCapture(0)
with tf.Session(graph=inference_path) as sess:
init = tf.global_variables_initializer()
sess.run(init)
saver1 = tf.train.import_meta_graph(filepath + '.meta')
#saver2 = tf.train.import_meta_graph(filepath2 + '.meta')
saver1.restore(sess, filepath)
tf_X = sess.graph.get_tensor_by_name('input:0')
pred = sess.graph.get_operation_by_name('pred')
number = pred.outputs[0]
prediction = tf.argmax(number, 1)
time_start = time.time()
flag = 0
while cap.isOpened():
ret, frame = cap.read()
frame = rev_cam(frame) # 摄像头倒转添加
resized_height = int(width * 0.75)
# 计算缩放比例
frame = cv2.resize(frame, (width, resized_height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, frame = cv2.threshold(frame, yuzhi, 255, cv2.THRESH_BINARY)
# slice the lower part of a frame
res = frame[resized_height - height:, :]
cv2.waitKey(1)
time_end = time.time()
time_c = time_end - time_start
# opening = cv2.morphologyEx(res, cv2.MORPH_OPEN, pfkernel)
if time_c < xianzhitime:
res = cv2.morphologyEx(res, cv2.MORPH_CLOSE, pfkernel)
cv2.imshow("frame", res)
frame = np.array(res, dtype=np.float32)
print(frame.sum())
# if time_c<10:
# forwardspeed=15
# else:
# forwardspeed=25
ssum = frame.sum()
# if ssum>2.16e+07:
if ssum > 2.0e+07:
robot.movement.move_forward(speed=forwardspeed, times=350)
continue
if frame.sum() < 700000:
robot.movement.move_backward(speed=20, times=500)#kan jian hei se hou tui
time.sleep(0.2)
robot.movement.turn_right(speed = 10, times = 500)
#robot.movement.right_ward()
# robot.movement.right_ward()
# continue
# if time_c >= 10 and flag==0:
# print("dsdssdsdsdsdsds")
# print()
# print()
# saver2.restore(sess, filepath2)
# tf_X = sess.graph.get_tensor_by_name('input:0')
# pred = sess.graph.get_operation_by_name('pred')
# number = pred.outputs[0]
# prediction = tf.argmax(number, 1)
# flag=1
value = prediction.eval(feed_dict={tf_X: np.reshape(frame, [-1, height, width, channel])})
print('img_out:', value)
if value == 0:
print("forward")
robot.movement.move_forward(speed=forwardspeed, times=200)
elif value == 1:
print("left")
robot.movement.turn_left(speed=15,times=200)
time.sleep(0.1)
#robot.movement.left_ward()
elif value == 2:
print("right")
robot.movement.turn_right(speed=12,times=200)
time.sleep(0.1)
#robot.movement.right_ward()
elif value == 3:
print("stop sign")
if time_c >= xianzhitime and ssum < ssumyuzhi:
adjust()
time.sleep(0.5)
robot.movement.move_forward(speed=forwardspeedend, times=fw_time)
time.sleep(1)
robot.movement.hit()
break
else:
robot.movement.move_forward(speed=20, times=350)
elif value == 4:
print("Banner forward")
robot.movement.move_forward(speed=20, times=350)
if time_c >= xianzhitime and ssum < ssumyuzhi:
adjust()
time.sleep(0.5)
robot.movement.move_forward(speed=20, times=fw_time)
time.sleep(1)
robot.movement.hit()
break
elif value == 5:
print("Banner left")
robot.movement.turn_left(speed=12,times=200)
#robot.movement.left_ward()
if time_c >= xianzhitime and ssum < ssumyuzhi:
adjust()
#time.sleep(0.5)
robot.movement.move_forward(speed=20, times=fw_time)
#time.sleep(1)
robot.movement.hit()
break
elif value == 6:
print("Banner right")
robot.movement.turn_right(speed=12,times=200)
#robot.movement.right_ward()
if time_c >= xianzhitime and ssum < ssumyuzhi:
adjust()
time.sleep(0.5)
robot.movement.move_forward(speed=20, times=fw_time)
time.sleep(1)
robot.movement.hit()
break
elif cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == '__main__':
###############################################################
# startTime=datetime.datetime.now()
###############################################################
# while 1 :
# speed = 10
# robot.movement.turn_right(speed=50, time500)
#robot.movement.turn_right(speed=50, times=500)
#robot.movement.move_right(speed=50, times=500)
#robot.movement.left_ward()
auto_pilot()
#adjust()
# robot.movement.hit()
# time.sleep(0.5)
##############################################################
# endTime=datetime.datetime.now()
# print(endTime-startTime)
###############################################################