-
Notifications
You must be signed in to change notification settings - Fork 1
/
电赛.txt
459 lines (421 loc) · 21.8 KB
/
电赛.txt
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import sensor, image, time, os, math,machine
from pyb import UART
import json
from pyb import LED
import lcd
from machine import I2C
import time
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.set_windowing((320, 240)) # Set 240x240 window.
sensor.set_auto_whitebal(False, rgb_gain_db=(65.2256,60.2071,61.9736))
sensor.set_auto_exposure(False, 40000)#修改曝光度
sensor.set_auto_gain(False, 22)
#sensor.set_auto_whitebal(False)
#sensor.set_auto_exposure(False)
#sensor.set_auto_gain(False)
sensor.skip_frames(time=2000) # Let the camera adjust.
clock = time.clock()
#数字阈值
black_lab=(15,60,-20,10,-10,20)#黑色数字
#走线阈值
red_lab2=(0, 75, 30, 80, 10, 40)#红色交叉路口
black_lab2=(0,45,-20,10,-10,20)#黑色终点
black_lab_first=(15,45,-20,0,-10,20)#第一次数字识别
#串口通信
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1) #8位数据位,无校验位,1位停止位
#返回最大色块(也就是返回最近的垃圾)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
#参数
area=[0,0,320,240];area_1=[0,0,320,240];area_2=[0,0,320,240]
area_3=[0,0,320,240];area_3_1=[0,0,320,240];area_4=[0,0,320,240]
#数字识别--条件判断部分
def num(wh,di_chang,ding_chang,zhong_3,zhong_3_xia,zhong_6):
if wh<0.6:return 1#1
else:#非1
if di_chang>0.9:return 2
else:#非1,2
if di_chang<0.3:#4和7
if ding_chang<0.3:return 4#4
else:return 7
else:#非1,4,7(3,5,6,8)
if zhong_3==0:return 3
else:#非1,4,7,3(5,6,8)
if zhong_3_xia==0:return 5
else:#非1,4,7,3,5(6,8)
if zhong_6==0:return 6
else:return 8
#判断数字在左边还是右边
def num_zuo_you(num_cx):
if num_cx<160:return 9#数字在左边
else:return 10#数字在右边
#识别多个数字,返回记录的数字和数字在左边还是右边
def num_main(blacks):
balck_numbers=blacks
j=0#色块数量
y=0#第几个色块
num_data=[0,0,0,0,0,0,0,0,0,0,0,0,0,0]#14
if balck_numbers:
for x in balck_numbers:j=j+1
if black_numbers:#如果有黑色色块
for i in black_numbers:#框出所有的黑色色块
img.draw_rectangle(i.rect(),color=(0,0,0),thickness=2)
if i.x()>20 and i.x()+i.w()<300 and i.y()>20 and i.area()>1000:
#print(1)
#参数重置
wh=0;di_chang=0;ding_chang=0;zhong_3=0;zhong_3_xia=0;zhong_6=0
#1的识别
wh=i.w()/i.h()
area[0]=i.x();area[1]=i.y()+int(i.h()/2);area[2]=i.w();area[3]=int(i.h()/2);
#底边参数
area_2[0]=i.x();area_2[1]=i.y()+i.h()-5;area_2[2]=i.w();area_2[3]=5;
blob_2s = img.find_blobs([black_lab],roi=area_2, pixels_threshold=20, area_threshold=20,merge=True)
if blob_2s:
blob_2=find_max(blob_2s);
img.draw_rectangle(blob_2.rect(),color=(225,0,0),thickness=2)
di_chang=blob_2.w()/i.w()
#顶边参数
area_1[0]=i.x();area_1[1]=i.y();area_1[2]=i.w();area_1[3]=5;
blob_1s = img.find_blobs([black_lab],roi=area_1, pixels_threshold=20, area_threshold=20,merge=True)
if blob_1s:
blob_1=find_max(blob_1s);
img.draw_rectangle(blob_1.rect(),color=(225,0,0),thickness=2)
ding_chang=blob_1.w()/i.w()
#3的左中间
area_3[0]=i.x();area_3[1]=i.y()+int(i.h()/3);area_3[2]=int(i.w()/3);area_3[3]=int(i.h()/3);
blob_3s = img.find_blobs([black_lab],roi=area_3, pixels_threshold=20, area_threshold=20,merge=True)
if blob_3s:
blob_3=find_max(blob_3s);
img.draw_rectangle(blob_3.rect(),color=(0,225,0),thickness=2)
zhong_3=blob_3.area()
#3中间的下面
area_3_1[0]=blob_3.x();area_3_1[1]=blob_3.y()+blob_3.h();area_3_1[2]=blob_3.w();area_3_1[3]=5;
blob_3_1s = img.find_blobs([black_lab],roi=area_3_1, pixels_threshold=20, area_threshold=20,merge=True)
if blob_3_1s:
blob_3_1=find_max(blob_3_1s);
img.draw_rectangle(blob_3_1.rect(),color=(0,0,225),thickness=2)
zhong_3_xia=blob_3_1.area()
#6的中间
area_4[0]=i.x()+int(i.w()/3);area_4[1]=i.y()+int(i.h()*2/9);area_4[2]=int(i.w()*5/7);area_4[3]=5;
blob_4s = img.find_blobs([black_lab],roi=area_4, pixels_threshold=20, area_threshold=20,merge=True)
if blob_4s:
blob_4=find_max(blob_4s);
img.draw_rectangle(blob_4.rect(),color=(255,0,0),thickness=2)
zhong_6=blob_4.area()
#保存结果--------
#识别数字
num_data[y]=num(wh,di_chang,ding_chang,zhong_3,zhong_3_xia,zhong_6)
y=y+1
#判断左9,右10
num_data[y]=num_zuo_you(i.cx())
#绘画出结果-------
img.draw_string(i.x(),i.y(),'(%d)'%(num_data[y-1]), color=(0,225,0),scale=4)#字符
#
#img.draw_rectangle(i.rect(),color=(0,0,0),thickness=2)
y=y+1
#有数字,判断上面的红色,有红色是第二个十字路口
if num_data[0]!=0:
red_cross = img.find_blobs([red_lab2],roi = (0,0,320,50), pixels_threshold=200, area_threshold=200,merge=True)
if red_cross:num_data[13]=9
#返回识别结果-----
return num_data
#识别一个数字,返回识别的数字
def num_frist(black_numbers):
for i in black_numbers:#如果有黑色色块
img.draw_rectangle(i.rect(),color=(0,0,0),thickness=2)
if i.x()>40 and i.x()<280 and abs(i.cx()-160)<90 and i.y()+i.h()<220:#放中心才可以识别
#参数重置
wh=0;di_chang=0;ding_chang=0;zhong_3=0;zhong_3_xia=0;zhong_6=0
#1的识别
wh=i.w()/i.h()
area[0]=i.x();area[1]=i.y()+int(i.h()/2);area[2]=i.w();area[3]=int(i.h()/2);
#底边参数
area_2[0]=i.x();area_2[1]=i.y()+i.h()-5;area_2[2]=i.w();area_2[3]=5;
blob_2s = img.find_blobs([black_lab],roi=area_2, pixels_threshold=20, area_threshold=20,merge=True)
if blob_2s:
blob_2=find_max(blob_2s);
img.draw_rectangle(blob_2.rect(),color=(225,0,0),thickness=2)
di_chang=blob_2.w()/i.w()
#顶边参数
area_1[0]=i.x();area_1[1]=i.y();area_1[2]=i.w();area_1[3]=5;
blob_1s = img.find_blobs([black_lab],roi=area_1, pixels_threshold=20, area_threshold=20,merge=True)
if blob_1s:
blob_1=find_max(blob_1s);
img.draw_rectangle(blob_1.rect(),color=(225,0,0),thickness=2)
ding_chang=blob_1.w()/i.w()
#3的左中间
area_3[0]=i.x();area_3[1]=i.y()+int(i.h()/3);area_3[2]=int(i.w()/3);area_3[3]=int(i.h()/3);
blob_3s = img.find_blobs([black_lab],roi=area_3, pixels_threshold=20, area_threshold=20,merge=True)
if blob_3s:
blob_3=find_max(blob_3s);
img.draw_rectangle(blob_3.rect(),color=(0,225,0),thickness=2)
zhong_3=blob_3.area()
#3中间的下面
area_3_1[0]=blob_3.x();area_3_1[1]=blob_3.y()+blob_3.h();area_3_1[2]=blob_3.w();area_3_1[3]=5;
blob_3_1s = img.find_blobs([black_lab],roi=area_3_1, pixels_threshold=20, area_threshold=20,merge=True)
if blob_3_1s:
blob_3_1=find_max(blob_3_1s);
img.draw_rectangle(blob_3_1.rect(),color=(0,0,225),thickness=2)
zhong_3_xia=blob_3_1.area()
#6的中间
area_4[0]=i.x()+int(i.w()/3);area_4[1]=i.y()+int(i.h()*2/9);area_4[2]=int(i.w()*5/7);area_4[3]=5;
blob_4s = img.find_blobs([black_lab],roi=area_4, pixels_threshold=20, area_threshold=20,merge=True)
if blob_4s:
blob_4=find_max(blob_4s);
img.draw_rectangle(blob_4.rect(),color=(255,0,0),thickness=2)
zhong_6=blob_4.area()
#识别结果
num_data1=num(wh,di_chang,ding_chang,zhong_3,zhong_3_xia,zhong_6)
#绘画出结果-------
img.draw_string(i.x(),i.y(),'(%d)'%(num_data1), color=(225,0,0),scale=4)#字符
#框出每一个数字
img.draw_rectangle(i.rect(),color=(225,0,0),thickness=2)
#print(di_chang,ding_chang)
#返回识别结果
return num_data1
#第一次识别----------
LED(1).on()
num1=0
while(num1==0 or num1 ==None):
for i in range(100):
clock.tick()
img = sensor.snapshot()
black_cx=0
black_numbers = img.find_blobs([black_lab_first], pixels_threshold=200, area_threshold=200,merge=True)
num1=num_frist(black_numbers)
print(num1)
LED(1).off()
LED(2).on()
#主循环-------------------------------------------
#参数
num_datas=[0,0,0,0,0,0,0,0,0,0,0,0,0,0]#14
while(0):
clock.tick()
img = sensor.snapshot()
#多识别
black_numbers = img.find_blobs([black_lab], pixels_threshold=200, area_threshold=200,merge=True)
num_datas=num_main(black_numbers)
print(num_datas)
data_car=b'2'
data_car2=b'0'
zhuan=0
t_5=0
while(1):
time.sleep_ms(100)
clock.tick()
img = sensor.snapshot()
car_data=uart.read(1)
if car_data==b'1' or car_data==b'2':
data_car=car_data
red_cross = img.find_blobs([red_lab2],roi = (0,60,320,120), pixels_threshold=200, area_threshold=200,merge=True)
red_cross_l = img.find_blobs([red_lab2],roi = (0,60,40,75), pixels_threshold=200, area_threshold=200,merge=True)
red_cross_r = img.find_blobs([red_lab2],roi = (280,60,40,75), pixels_threshold=200, area_threshold=200,merge=True)
red_cross_z = img.find_blobs([red_lab2],roi = (80,170,160,70), pixels_threshold=200, area_threshold=200,merge=True)
red_cross_ding = img.find_blobs([red_lab2],roi = (80,0,160,70), pixels_threshold=200, area_threshold=200,merge=True)
red_cross_d_l = img.find_blobs([red_lab2],roi = (0,175,50,65), pixels_threshold=200, area_threshold=200,merge=True)
red_cross_d_r = img.find_blobs([red_lab2],roi = (270,175,50,65), pixels_threshold=200, area_threshold=200,merge=True)
black_over = img.find_blobs([black_lab2], roi = (0,100,320,150),pixels_threshold=20, area_threshold=20)
black_l = img.find_blobs([black_lab2],roi = (0,60,70,75), pixels_threshold=200, area_threshold=200,merge=True)
black_r = img.find_blobs([black_lab2],roi = (250,60,70,75), pixels_threshold=200, area_threshold=200,merge=True)
img.draw_rectangle((0,60,320,120),color=(225,0,0),thickness=2)#识别红色中间线的限制框
img.draw_rectangle((0,60,40,75),color=(0,225,0),thickness=2)#识别红色左边的限制框
img.draw_rectangle((280,60,40,75),color=(0,225,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle((80,170,160,70),color=(0,225,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle((0,175,50,65),color=(0,225,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle((270,175,50,65),color=(0,225,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle((0,60,70,75),color=(0,225,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle((250,60,70,75),color=(0,225,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle((80,0,160,70),color=(0,225,0),thickness=2)#识别红色右边的限制框
#黑色用于后退判断----
black1_area=0;black2_area=0
if black_l:
black1=find_max(black_l);
black1_area=black1.area()
img.draw_rectangle(black1.rect(),color=(225,0,0),thickness=2)#识别红色右边的限制框
if black_r:
black2=find_max(black_r);
black2_area=black2.area()
img.draw_rectangle(black2.rect(),color=(225,0,0),thickness=2)#识别红色右边的限制框
black_cha=(black1_area-black2_area)/(black1_area+black2_area+1)
#print('-------------',black_cha).
#红色用于后退判断----
red_arae1=[0,0,320,240];red_arae2=[0,0,320,240]
if red_cross:
red_xian=find_max(red_cross)
red_arae1[0]=red_xian.x();red_arae1[1]=red_xian.y();red_arae1[2]=int(red_xian.w()/2);red_arae1[3]=int(red_xian.w()/2);
red_arae2[0]=red_xian.x()+int(red_xian.w()/2);red_arae2[1]=red_xian.y();red_arae2[2]=int(red_xian.w()/2);red_arae2[3]=int(red_xian.w()/2);
img.draw_rectangle(red_arae1,color=(255,0,0),thickness=2)#识别红色右边的限制框
img.draw_rectangle(red_arae2,color=(255,0,0),thickness=2)#识别红色右边的限制框
red_pix1=0;red_pix2=0
red_1=img.find_blobs([red_lab2],roi = red_arae1, pixels_threshold=20, area_threshold=20,merge=True)
red_2=img.find_blobs([red_lab2],roi = red_arae2, pixels_threshold=20, area_threshold=20,merge=True)
if red_1:
red_1m=find_max(red_1);
red_pix1=red_1m.pixels();
#print(1,red_pix1)
if red_2:
red_2m=find_max(red_2);
red_pix2=red_2m.pixels();
#print(2,red_pix2)
#第三个路口判断
if (red_cross_d_r or red_cross_d_l) and red_cross_z:
if red_cross_ding:
t_5=0
else:
t_5=1
print("T字")
red_cha=red_pix1-red_pix2#差值
if red_cross:
for blob1 in red_cross:
img.draw_rectangle(blob1.rect(),color=(0,0,225),thickness=2)#识别红色右边的限制框
if(((red_cross_l and red_cross_z )or (red_cross_r and red_cross_z) or (red_cross_r and red_cross_l)) and data_car2==b'0'):#判断是否拐弯
uart.write("%d"%(902))#小车停下
#uart.write("%d"%(500))#色块右移--小车右转
print('停下')
#time.sleep_ms(3000)
#识别
if num1!=1 and num1!=2:
i = 0;j=0;hav=0;x=0
num_datas=[0,0,0,0,0,0,0,0,0,0,0,0,0,0]#14
for x in range(2):
#多识别
clock.tick()
img = sensor.snapshot()
black_numbers = img.find_blobs([black_lab], pixels_threshold=200, area_threshold=200,merge=True)
num_datas=num_main(black_numbers)
print(num_datas)
if num_datas==None:num_datas=[0,0,0,0,0,0,0,0,0,0,0,0,0,0]#14
for x in range(1):
if num1==num_datas[0] or num1==num_datas[2] or num1==num_datas[4] and zhuan==0:#判断是不是
if num1==num_datas[0]:#是,左右转
if num_datas[1]==9:#左右转
print('左')
if num_datas[4]==0:#十字路口判断
zhuan=921
else:
zhuan=901
print('----------------')
if num_datas[1]==10:
print('右')
if num_datas[4]==0:#十字路口判断
zhuan=920;print('右')#色块右移--小车右转#左
else:zhuan=900
if num1==num_datas[2]:#是,左右转
if num_datas[3]==9:
print('左')
if num_datas[4]==0:
zhuan=921#uart.write("%d"%(501));print('左')#色块右移--小车右转#左
else:zhuan=901
if num_datas[3]==10:
print('右')
if num_datas[4]==0:
zhuan=920#uart.write("%d"%(501));print('左')#色块右移--小车右转#左
else:zhuan=900
if num1==num_datas[4]:#是,左右转
if num_datas[5]==9:zhuan=901;print('左')#色块右移--小车右转#左
if num_datas[5]==10:zhuan=900;print('右')#右
else:
if zhuan==0:
if num_datas[0]==0:zhuan=660#uart.write("%d"%(blob1.cx()+100))#直行
if num_datas[4]==0 and num_datas[0]!=0:
print('----------------',num_datas[13])
if t_5!=1:
zhuan=160#uart.write("%d"%(blob1.cx()+100))#直行
print('-----------------')
else:
zhuan=900
print('+++++++++++++++++')
if num_datas[4]!=0:
zhuan=900#u+art.write("%d"%(500))#右
print('9999999999999999999999999999999')
for x in range(5):
time.sleep_ms(10)
uart.write("%d"%(660))
print('前')
elif (red_cross_d_l and red_cross_z )or (red_cross_d_r and red_cross_z) or (red_cross_d_r and red_cross_d_l):#判断是否拐弯
print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if num1==1:
for x in range(100):
print("十字路口---左转")#返回像素数量
uart.write("%d"%(901))#色块右移--小车左转
if num1==2:
for x in range(100):
print("十字路口---右转")#返回像素数量
uart.write("%d"%(900))#色块右移--小车右转
if num1!=1 and num1!=2 and data_car2==b'0' and zhuan!=0:
for x in range(10):
uart.write("%d"%(zhuan))
print("十字路口---------转",zhuan)#返回像素数量
zhuan=0
if data_car2==b'1':
uart.write("%d"%(900))
print("十字路口---右转")#返回像素数量
else:#微调直行
if data_car==b'2':#前进微调
img.draw_rectangle(blob1.rect(),color=(0,0,255),thickness=2)
#print("像素数量:",blob1.pixels())#返回像素数量
#print(blob1.cx())
print('微调坐标',blob1.cx()+500)
uart.write("%d"%(blob1.cx()+500))
else:#差值后退
data_car2=b'1'
img.draw_rectangle(blob1.rect(),color=(0,0,255),thickness=2)
#print("像素数量:",blob1.pixels())#返回像素数量
#print(blob1.cx())
print('微调坐标',blob1.cx()+500)
uart.write("%d"%(blob1.cx()+500))
#uart.write("%d"%(int(black_cha*100+200)))
#print('差值',(int(black_cha*100+200)))
elif black_over:#黑色终点
i=0
for blob2 in black_over:
if(blob2.area() > 100):
i = i + 1
#print("像素数量:",blob2.pixels())#返回像素数量
img.draw_rectangle(blob2.rect(),color=(0,0,255),thickness=2)
if(i >=5) and data_car2==b'0':#去的停
if num1==1 or num1==2:
time.sleep_ms(200)
print("停止11111111111111")
uart.write("%d"%(992))#小车停止
print(992)
else:
time.sleep_ms(200)
print("停止11111111111111")
uart.write("%d"%(992))#小车停止
print(992)
if(i >=5) and data_car2==b'1':#回来的停
if num1==1 or num1==2:
print("停止22222222222222")
time.sleep_ms(200)
uart.write("%d"%(992))#小车停止
print(992)
else:#长距离反转刹车
print("停止22222222222222")
uart.write("%d"%(912))#小车停止
print(912)
else:
uart.write("%d"%(500))
print(500)
##time.sleep_ms(20)
#print("停止222222222222222")
#uart.write("%d"%(902))#小车停止
#print(902)
#print(i)#黑点的个数
#else:
#i=0
#if black_over:#黑色终点
#for i in black_over:
#if i.x()>30 and i.x()<210 and i.cy()>210:
#print("停止")
#uart.write("%d"%(902))#小车停止
#print(902)
##print(i)#黑点的个数