2021电赛F题视觉教程+代码免费开源
最近好多要电赛题的源码,其他csdn营销号下载都需要会员或钱,正好最近课设又要做一遍电赛小车题,哥们先把代码开源了,饿死营销号
电赛宝藏链接:
四天三夜,那布满着未知与紧迫感的气息让荷尔蒙无比兴奋,挑战着脑力与体力的极限!
2021全国大学生电子设计竞赛F题参赛简记
2021年小车题完整代码及教程!
2021电赛F题视觉教程+代码免费开源
其他年份的经典题目的源码都在这里啦!
【电赛源码合集】19电磁炮.zip、17板球.zip、15风力摆.zip、13倒立摆.zip、全国大学生电子设计竞赛历年真题(1994-2021).zip
参加电赛必看的经验贴!
全国电子设计竞赛指导 (老学长挥泪经验之谈)
论文模板及评分标准!
全国大学生电子设计竞赛论文写作模板及评分标准
正文开始咯:
视觉的一个任务是视觉上位机模块识别数字并进行滤波和判断处理,传指令给下位机;另一个任务是红线循迹。
不多bb,先上代码和效果图~
- 链接:GitHub——2021电赛F题所有程序
- 链接:GitHub——K210数字识别模型文件及主程序
- 链接:GitHub——openmv循迹主程序分享
- 链接:百度网盘——小车主板pcb文件 提取码:3n50 –来自百度网盘超级会员V3的分享
- README:蓝牙连接后,发送字符 " t " 给小车进入测试程序
K210数字识别效果:(我媛哥的手真好看)
openmv红线循迹效果:
小车整体图:
硬件工具:K210、openmv
软件工具:Maixpy IDE、OpenMV IDE
IDE可到官网下载:
https://www.sipeed.com/index.html
文章目录
1. K210数字识别、滤噪、判断
正常操作是上位机是识别,给下位机传数据,处理放到下位机;假如你有个坑比队友,处理还是放上位机干吧;
经过算法优化,现在几乎不存在掉帧。
1.1 功能难点及对应函数实现分析
1、在小车行驶过程中,会存在数字不能完全进入视野中情况等存在有很多帧误识情况,要进行滤噪处理
2、每一帧要做处理的同时每个目标值的矩形框进行处理,每个目标值的矩形框还要进行滤波
3、在上位机上不断的优化算法,减少掉帧
4、在Maixpy IDE中python 的numpy、pandas巴拉巴拉包都调不了
5、图中最多出现四个,但这里设了读六个矩形框,留两个容错,不然有误识别的直接死机
6、给下位机传送指令:
一开始初始化识别传送对应的数字
若为12,则后面不在传送指令
若为34,则后面路口处传"l"or"r"
若为5678,则后面两个路口传送两次"l"or“r”
7、设计思路
通过初始化函数识别一开始给入的数字并保存下来;
根据识别的情况进入不同的程序;
12号病房,识别完了以后是不用在路上在识别的,直接关掉程序;
34号病房,初始化识别完了,加两秒延迟,避免在开始的时候连续识别传指令,要在路上识别一次,要保证返回来的时候不识别不然又给下位机传指令了,还要保证初始化识别完了以后不会紧接着识别误认为是路上的标签给下位机传指令;
5678也一样,但是路上得识别两次穿两次指令;
1.2 YOLOV5神经网络模型训练
训练集:
测试集:
训练输出结果:
将通过拍摄3403张赛道数字照片,使用labellmg进行标签标定,制作数据集,yolov5训练
(哥们和队友从晚上十点标到凌晨两点(哭脸))
详细过程可看下面文章:
YOLOV5:数据集制作
YOLOV5:不懂代码也能使用YOLOV5跑项目
注意:这里yolov5得到的是.pt文件,需要进行模型转换,转化成可以加载在K210板上的.kmodel模型(后面细讲)
1.3 K210操作步骤
1.3.1 下载K210的IDE
https://www.sipeed.com/index.html
1.3.2 烧入最新固件库
官方教程:
https://wiki.sipeed.com/soft/maixpy/zh/get_started/upgrade_maixpy_firmware.html
更新固件库:
1.3.3 将文件放入TF卡中
文件目录:
将上述文件全复制到tf卡中,tf卡插入k210
注意:tf卡先格式成FAT32格式,部分牌子的tf卡格式了也不能挂载到k210上,建议多换几张卡试试
1.3.4 IED中查看效果
IDE中运行效果:
1.3.5 通过串口调试助手测试指令通信
引脚9是TX,引脚10是RX,波特率为115200
通过usb转ttl连接到电脑上
初始识别数字为6,两次转向是向左
1.4 上代码含详细注释
第一版本:
import sensor,image,lcd,time
import KPU as kpu
from machine import UART
from fpioa_manager import fm
lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.run(1)#加载yolov5模型
task = kpu.load("/sd/yolov5.kmodel")
f=open("anchors.txt","r")
anchor_txt=f.read()
L=[]for i in anchor_txt.split(","):
L.append(float(i))
anchor=tuple(L)
f.close()
f=open("lable.txt","r")
lable_txt=f.read()
lable = lable_txt.split(",")
f.close()#设置RX、RT引脚
fm.register(9, fm.fpioa.UART1_TX, force=True)
fm.register(10, fm.fpioa.UART1_RX, force=True)#设置串口通信
uart_A = UART(UART.UART1,115200,8,1,0, timeout=1000, read_buf_len=4096)
anchor =(0.1766,0.1793,0.4409,0.3797,0.6773,0.5954,1.0218,0.9527,2.158,1.6841)
sensor.set_windowing((224,224))
a = kpu.init_yolo2(task,0.5,0.3,5, anchor)
classes=["9","1","4","2","3","8","5","6","7"]#全局变量,保存初始化识别的数字
intnum =0#初始化识别函数defbegin(intnum):
TF =1#得分序列,放1-8识别的次数,每一帧识别成哪个,对应的位置加一,1-8哪个先到10即最终识别为哪个
List_score01 =[0]*8while(TF):
img = sensor.snapshot()#code是yolov5返回的每个矩形框的参数#例图中出现两个目标区域的时候:[{"x":9, "y":99, "w":55, "h":82, "value":0.697979, "classid":8, "index":0, "objnum":2}, {"x":137, "y":105, "w":56, "h":67, "value":0.939132, "classid":4, "index":1, "objnum":2}]
code = kpu.run_yolo2(task, img)#print(code)if code:for i in code:#画目标区域矩形框
a = img.draw_rectangle(i.rect())
a = lcd. display(img)
list1=list(i.rect())#print(classes[i.classid()])#识别到的加1
List_score01[int(classes[i.classid()])-1]+=1#print(List_score01)if(List_score01[0]>=10):
intnum =1#给下位机通信传指令
uart_A.write('1')#print(1)#退出初始化循环
TF =0if(List_score01[1]>=10):
intnum =2
uart_A.write('2')#print(2)
TF =0if(List_score01[2]>=10):
intnum =3
uart_A.write('3')#print(3)
TF =0if(List_score01[3]>=10):
intnum =4
uart_A.write('4')#print(4)
TF =0if(List_score01[4]>=10):
intnum =5
uart_A.write('5')#print(5)
TF =0if(List_score01[5]>=10):
intnum =6
uart_A.write('6')#print(6)
TF =0if(List_score01[6]>=10):
intnum =7
uart_A.write('7')#print(7)
TF =0if(List_score01[7]>=10):
intnum =8
uart_A.write('8')print(8)
TF =0else:
a = lcd.display(img)return intnum
#34道路识别函数defthreefour(intnum):#设置二维矩阵,存放每一个矩形框中不同数字识别的次数
List_score02 =[[0]*8]*6
intnum02 =[0]*6
TF =1while(TF):#加载一帧图像
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)if code:
int_i =0for i in code:
int_i +=1
a=img.draw_rectangle(i.rect())
a = lcd. display(img)
list1=list(i.rect())
b=(list1[0]+list1[2])/2#对应的矩阵值加一
List_score02[int_i][int(classes[i.classid()])-1]+=1print(List_score02[int_i])#该目标框逐个数字分析是否出现次数到达10,即为该目标区域中的数字for ii inrange(8):if(List_score02[int_i][ii]>=10):
intnum02[int_i]= ii+1#该目标区域中的数字是否是初始化识别的数字if(intnum == intnum02[int_i]):#判断位置,从而判断先做向右转if(b <75):
uart_A.write("l")print("l")
TF =0if(b >75):
uart_A.write("r")print("r")
TF =0else:
a = lcd.display(img)return0#5678道路识别函数deffivesixseveneight(intnum):#设置二维矩阵,存放每一个矩形框中不同数字识别的次数,一帧中最多有四个目标区域,但这里设置六个,防止有误识别的造成有很多矩形框程序暴死
List_score02 =[[0]*8]*6
intnum02 =[0]*6
TF =2#5678道路识别要识别两次,传两次指令,两次直接通过延时函数断开while(TF):
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)if code:
int_i =0for i in code:
int_i +=1
a=img.draw_rectangle(i.rect())
a = lcd. display(img)
list1=list(i.rect())
b=(list1[0]+list1[2])/2
List_score02[int_i][int(classes[i.classid()])-1]+=1print(List_score02[int_i])for ii inrange(8):if(List_score02[int_i][ii]>=10):
intnum02[int_i]= ii+1if(intnum == intnum02[int_i]):if(b <75):
uart_A.write("l")print("l")
TF -=1
List_score02 =[[0]*8]*6
intnum02 =[0]*6
time.sleep(3)if(b >75):
uart_A.write("r")print("r")
TF -=1
List_score02 =[[0]*8]*6
intnum02 =[0]*6
time.sleep(3)else:
a = lcd.display(img)return0#执行程序
intnum = begin(intnum)
time.sleep(3)if(intnum ==3or intnum ==4):
threefour(intnum)if(intnum ==5or intnum ==6or intnum ==7or intnum ==8):
fivesixseveneight(intnum)
uart_A.deinit()del uart_A
a = kpu.deinit(task)
第二版本:
import sensor,image,lcd,time
import KPU as kpu
from machine import UART
from fpioa_manager import fm
lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.run(1)#加载yolov5模型
task = kpu.load("/sd/yolov5.kmodel")
f=open("anchors.txt","r")
anchor_txt=f.read()
L=[]for i in anchor_txt.split(","):
L.append(float(i))
anchor=tuple(L)
f.close()
f=open("lable.txt","r")
lable_txt=f.read()
lable = lable_txt.split(",")
f.close()#设置RX、RT引脚
fm.register(9, fm.fpioa.UART1_TX, force=True)
fm.register(10, fm.fpioa.UART1_RX, force=True)#设置串口通信
uart_A = UART(UART.UART1,9600,8,1,0, timeout=1000, read_buf_len=4096)
anchor =(0.1766,0.1793,0.4409,0.3797,0.6773,0.5954,1.0218,0.9527,2.158,1.6841)
sensor.set_windowing((224,224))
a = kpu.init_yolo2(task,0.5,0.3,5, anchor)
classes=["9","1","4","2","3","8","5","6","7"]#全局变量,保存初始化识别的数字
intnum =0#初始化识别函数defbegin(intnum):
TF =1#得分序列,放1-8识别的次数,每一帧识别成哪个,对应的位置加一,1-8哪个先到10即最终识别为哪个
List_score01 =[0]*8while(TF):
img = sensor.snapshot()#code是yolov5返回的每个矩形框的参数#例图中出现两个目标区域的时候:[{"x":9, "y":99, "w":55, "h":82, "value":0.697979, "classid":8, "index":0, "objnum":2}, {"x":137, "y":105, "w":56, "h":67, "value":0.939132, "classid":4, "index":1, "objnum":2}]
code = kpu.run_yolo2(task, img)#print(code)if code:for i in code:#画目标区域矩形框
a = img.draw_rectangle(i.rect())
a = lcd. display(img)
list1=list(i.rect())#print(classes[i.classid()])#识别到的加1
List_score01[int(classes[i.classid()])-1]+=1#print(List_score01)if(List_score01[0]>=10):
intnum =1#给下位机通信传指令
uart_A.write('A')print(1)#退出初始化循环
TF =0if(List_score01[1]>=10):
intnum =2
uart_A.write('B')print(2)
TF =0if(List_score01[2]>=10):
intnum =3
uart_A.write('C')print(3)
TF =0if(List_score01[3]>=10):
intnum =4
uart_A.write('D')print(4)
TF =0if(List_score01[4]>=10):
intnum =5
uart_A.write('E')print(5)
TF =0if(List_score01[5]>=10):
intnum =6
uart_A.write('F')print(6)
TF =0if(List_score01[6]>=10):
intnum =7
uart_A.write('G')#print(7)
TF =0if(List_score01[7]>=10):
intnum =8
uart_A.write('H')print(8)
TF =0else:
a = lcd.display(img)return intnum
#道路识别函数defthen(intnum):while(1):#设置二维矩阵,存放每一个矩形框中不同数字识别的次数,一帧中最多有四个目标区域,但这里设置六个,防止有误识别的造成有很多矩形框程序暴死
List_score02 =[[0]*8]*6
intnum02 =[0]*6
TF =1while(TF):
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)if code:
int_i =-1for i in code:
int_i +=1
a=img.draw_rectangle(i.rect())
a = lcd. display(img)
list1=list(i.rect())
b=(list1[0]+list1[2])/2
List_score02[int_i][int(classes[i.classid()])-1]+=1#print(int_i)print(str(List_score02[int_i])+str(int_i))for ii inrange(8):#int_i是第几个框,ii是第几个框中的每个数字if(List_score02[int_i][ii]==10and intnum == ii+1):if(b <75):
uart_A.write("L")print("L")
TF =0
List_score02 =[[0]*8]*6
intnum02 =[0]*6if(b >75):
uart_A.write("R")print("R")
TF =0
List_score02 =[[0]*8]*6
intnum02 =[0]*6else:
a = lcd.display(img)
TF =0
List_score02 =[[0]*8]*6
intnum02 =[0]*6return0#执行程序
time.sleep(3)
uart_A.write("E")
intnum = begin(intnum)
time.sleep(3)
then(intnum)
uart_A.deinit()del uart_A
a = kpu.deinit(task)
2. OPENMV红线循迹
openmv就是玩具
——某位老哥(收录名人名言)
用openmv红线循迹,这部分比较简单,就不多说了吧
循迹测试:
测试结果:
虽然2021电赛过去了,但是现在回味起来依然很香~
版权归原作者 zhaohaobingSUI 所有, 如有侵权,请联系我们删除。