# 2021电赛F题数字识别和巡线部分

[En]

When I remembered the software picture later, I suddenly wanted to write an article and found that there was no screenshot at that time.

import sensor,image,lcd,time
import KPU as kpu
from Maix import FPIOA, GPIO
from fpioa_manager import fm
from board import board_info
from machine import UART
from Maix import GPIO

fm.register(17, fm.fpioa.GPIO0, force=True)
s_flag = GPIO(GPIO.GPIO0, GPIO.OUT)

flag=1

GRAYSCALE_THRESHOLD = [(87, 255)]    #划分的灰度值
THRESHOLD =(10, 34, -41, 74, -109, 127)   #摄像头垂直于地面

lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#sensor.set_hmirror(0)   #镜像
#sensor.set_vflip(1)     #翻转
lcd.rotation(2)     #旋转方向 参数是0～3, 分别代表顺时针旋转 0度 90度 180度 270度
sensor.set_windowing((224, 224))
#sensor.set_brightness(2)
sensor.run(1)
clock = time.clock()

fm.register(23,fm.fpioa.UART2_TX)
uart_A = UART(UART.UART2, 115200, 8, None, 1, timeout=1000, read_buf_len=4096)

f=open("anchors.txt","r")
L=[]
for i in anchor_txt.split(","):
L.append(float(i))
anchor=tuple(L)
f.close()
a = kpu.init_yolo2(task, 0.6, 0.3, 5, anchor)

f=open("lable.txt","r")
labels = labels_txt.split(",")
f.close()


[En]

I found that there are so many holes in this problem that I can’t finish it by myself, so I need some tricks.

[En]

Driving in a straight line in an open loop is very stable, but it will be disturbed at the crossroads, so I added my judgment and ignored the white areas on the left and right sides as a straight line. (in the main control, I make a comprehensive judgment based on the crossroads and numbers I have passed.)

def c_line():
img = sensor.snapshot().binary([THRESHOLD])
lcd.display(img)

roi0=(80, 0,30, 30)     #&#x7ED9;&#x8BC6;&#x522B;&#x4E2D;&#x7EBF;&#x7684;&#x4F4D;&#x7F6E;&#x6807;&#x4E0A;&#x5174;&#x8DA3;&#x6846;&#xFF0C;&#x5E76;&#x753B;&#x4E0B;&#x6765;&#xFF0C;&#x9A8C;&#x8BC1;&#x3002;
most_pixels=0
img.draw_rectangle(roi0, color =170)
blob0 = img.find_blobs(GRAYSCALE_THRESHOLD, roi=roi0,pixels_threshold=100, area_threshold=100, merge=True)
if blob0:
for n in range(len(blob0)):
#&#x76EE;&#x6807;&#x533A;&#x57DF;&#x627E;&#x5230;&#x7684;&#x989C;&#x8272;&#x5757;&#xFF08;&#x7EBF;&#x6BB5;&#x5757;&#xFF09;&#x53EF;&#x80FD;&#x4E0D;&#x6B62;&#x4E00;&#x4E2A;&#xFF0C;&#x627E;&#x5230;&#x6700;&#x5927;&#x7684;&#x4E00;&#x4E2A;&#xFF0C;&#x4F5C;&#x4E3A;&#x672C;&#x533A;&#x57DF;&#x5185;&#x7684;&#x76EE;&#x6807;&#x76F4;&#x7EBF;
if blob0[n].pixels() > most_pixels:
most_pixels = blob0[n].pixels()
#merged_blobs[i][4]&#x662F;&#x8FD9;&#x4E2A;&#x989C;&#x8272;&#x5757;&#x7684;&#x50CF;&#x7D20;&#x603B;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x6B64;&#x989C;&#x8272;&#x5757;&#x50CF;&#x7D20;&#x603B;&#x6570;&#x5927;&#x4E8E;
largest_blob = n
R = 1

else:
R = 0

roi1=(80, 200,30, 30)
img.draw_rectangle(roi1, color =170)
blob1 = img.find_blobs(GRAYSCALE_THRESHOLD, roi=roi1,pixels_threshold=100, area_threshold=100, merge=True)
if blob1:
for n in range(len(blob1)):
if blob1[n].pixels() > most_pixels:
most_pixels = blob1[n].pixels()
largest_blob = n
L = 1

else:
L = 0
uart_A.write('L')
uart_A.write(str(L))
uart_A.write('Z')

uart_A.write('R')
uart_A.write(str(R))
uart_A.write('X')

line=img.get_regression([(20, 250)],roi=(0,0,210,210),x_srtide=2,y_srtide=1,robust=False)  #&#x7070;&#x5EA6; &#x767D;&#x9ED1;
if(line):
rho_err = abs(line.rho())-img.width()/2   #piancha
#print(int(rho_err))
uart_A.write('P')
uart_A.write(str(int(rho_err)))
uart_A.write('S')
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
img.draw_line(line.line(), color = 127)

print('R',R ,'L',L)

#print(rho_err,line.magnitude(),theta_err)


def c_num():
global flag
img = sensor.snapshot()
print(anchor)
a = lcd.display(img)
if code:
for i in code:
a=img.draw_rectangle(i.rect(),(0,255,0),2)
a = lcd.display(img)
for i in code:
lcd.draw_string(i.x()+45, i.y()-5, labels[i.classid()]+" "+'%.2f'%i.value(), lcd.WHITE,lcd.GREEN)
#print(labels[i.classid()])
#print('x',i.x(),y,'i.y()')
if(labels[i.classid()]=='1'):
#print('1')
uart_A.write('N')
uart_A.write('1')
uart_A.write('M')

if(labels[i.classid()]==' 2'):
uart_A.write('N')
uart_A.write('2')
uart_A.write('M')
if(labels[i.classid()]==' 3'):
uart_A.write('N')
uart_A.write('3')
uart_A.write('M')
if(labels[i.classid()]==' 4'):
uart_A.write('N')
uart_A.write('4')
uart_A.write('M')
if(labels[i.classid()]==' 5'):
uart_A.write('N')
uart_A.write('5')
uart_A.write('M')
if(labels[i.classid()]==' 6'):
uart_A.write('N')
uart_A.write('6')
uart_A.write('M')
if(labels[i.classid()]==' 7'):
uart_A.write('N')
uart_A.write('7')
uart_A.write('M')
if(labels[i.classid()]==' 8'):
uart_A.write('N')
uart_A.write('8')
uart_A.write('M')

flag=0
s_flag.value(0)
else:
a = lcd.display(img)
#uart_A.write('N')
#uart_A.write('0')
#uart_A.write('M')


Python我并没有好好学过，几乎没写过几次，都是临时抱佛脚，代码写的很烂。 这次没有什么详细讲解，代码直接甩出去，(不负责的我，这次写完就不想补充完善了)留给大家自己慢慢研究了。我感觉除了那个灰度图像阈值需要自己调整外，其他没有什么太大问题。

[En]

Well, there was no completion material in this competition, so I didn’t take part in the creation and open source, and I didn’t get a bonus, but it wasn’t a big problem. I was lazy.

[En]

The bonus of Chuangchuang is even higher than that of the school, and the school will reduce the bonus again, because there are many people who won the prize and participated in the competition this year, so I laughed. The things of all kinds of mean people in this school will be laid out until my graduation defense is over. It’s not as good as me in a good specialty.

Original: https://blog.csdn.net/zy19981110/article/details/122179873
Author: 随风飘零翼
Title: 2021电赛F题数字识别和巡线部分

(0)