3824| 4
|
[AI人工智能教程] LattePanda&AI-手写数字识别 |
LattePanda&AI-手写数字识别概述数字识别(Digit Recognition),是计算机从纸质文档,照片,或其他来源接收和理解并识别可读的数字的能力,具有很大的实际应用价值,例如手写数字识别可以应用在银行汇款单号识别中,以极大的减少人工成本。 项目基础识别手写字 硬件准备:AI主控:LattePanda 程序编写:1、双击桌面上的“startpage.sh”,打开JupyterLab,切换到“home/lattepanda/桌面/LattePanda&AI项目实战/”目录下,如下图,检查一下项目必需的3个文件;
3、双击打开“手写数字识别.ipynb”; 样例代码:
[mw_shl_code=python,false]#导入同目录下的手写数字识别文件 from handwriteDigitRecognition import * #调用同目录下已经训练好的模型文件 model, _ = Model.load("手写数字识别") #初始化识别状态为假 predictOnce = False #窗口的标题栏名称 screen = Screen("手写数字识别") #定义“清除”函数,该函数通过for循环遍历每一个黑色块 def buttonClearCallback(): for canvas in canvases: canvas.clear() #设置“清除”按键的位置和大小,背景色为白色,字体颜色为黑色,按下该按键能调用“清除”函数 buttonClear = screen.createButton(700, 0, 100, 50, "清除" , buttonClearCallback, bg = (255,255,255), color = (0,0,0)) #设置设别结果的位置和大小,背景色为白色,字体颜色为黑色 buttonResult = screen.createButton(0, 0, 300, 50, "手写数字:" , None, bg = (255,255,255), color = (0,0,0)) #6个用于手写数字的黑色块,设置位置和大小 canvases = [None]*6 canvases[0] = screen.createCanvas(60, 50, 200, 200) canvases[1] = screen.createCanvas(280, 50, 200, 200) canvases[2] = screen.createCanvas(500, 50, 200, 200) canvases[3] = screen.createCanvas(60, 270, 200, 200) canvases[4] = screen.createCanvas(280, 270, 200, 200) canvases[5] = screen.createCanvas(500, 270, 200, 200) #定义“识别”函数,该函数设置识别状态为真 def buttonRecogCallback(): global predictOnce predictOnce = True #设置“识别”按键的位置和大小,背景色为白色,字体颜色为黑色,按下该按键能调用“识别”函数 buttonClear = screen.createButton(600, 0, 100, 50, "识别" , buttonRecogCallback, bg = (255,255,255), color = (0,0,0)) #打开手写数字交互窗口,按下“Q”键退出窗口 if_run = 1 while (if_run == 1): #如果“识别”按键被按下,通过for循环对每个黑色块内手写字进行识别处理,将识别结果依次显示出来 if predictOnce: predictOnce = False buttonResult.txt = "识别结果:" for cavas in canvases: buttonResult.txt += str(Model.predict(model, cavas.get())) #打开与显示交互窗口,如果按下Q键,将无法进入下一次while循环 if screen.show(): if_run = 0 screen.quit()[/mw_shl_code] 4、运行程序,当执行到最后一个单元格时,会弹出如下交互窗口; 项目进阶交互窗口如图,我们通过代码中的参数控制交互窗口的显示界面,因此,你可以根据需要调整这些参数以更改或添加交互窗口的背景颜色、文字内容、文字位置大小、手写区域位置大小及数量...... [mw_shl_code=python,false]#导入同目录下的手写数字识别文件 from handwriteDigitRecognition import * #调用同目录下已经训练好的模型文件 model, _ = Model.load("手写数字识别") #初始化识别状态为假 predictOnce = False #窗口的标题栏名称 screen = Screen("手写密码录入系统") #定义“清除”函数,该函数通过for循环遍历每一个黑色块 def buttonClearCallback(): for canvas in canvases: canvas.clear() #设置“清除”按键的位置和大小,背景色为白色,字体颜色为黑色,按下该按键能调用“清除”函数 buttonClear = screen.createButton(700, 0, 100, 50, "清除" , buttonClearCallback, bg = (255,255,255), color = (0,0,0)) #设置设别结果的位置和大小,背景色为白色,字体颜色为黑色 buttonResult = screen.createButton(0, 0, 300, 50, "请写入密码:" , None, bg = (255,255,255), color = (0,0,0)) #6个用于手写数字的黑色块,设置位置和大小 canvases = [None]*8 canvases[0] = screen.createCanvas(40, 50, 150, 150) canvases[1] = screen.createCanvas(230, 50, 150, 150) canvases[2] = screen.createCanvas(420, 50, 150, 150) canvases[3] = screen.createCanvas(610, 50, 150, 150) canvases[4] = screen.createCanvas(40, 270, 150, 150) canvases[5] = screen.createCanvas(230, 270, 150, 150) canvases[6] = screen.createCanvas(420, 270, 150, 150) canvases[7] = screen.createCanvas(610, 270, 150, 150) #定义“识别”函数,该函数设置识别状态为真 def buttonRecogCallback(): global predictOnce predictOnce = True #设置“识别”按键的位置和大小,背景色为白色,字体颜色为黑色,按下该按键能调用“识别”函数 buttonClear = screen.createButton(600, 0, 100, 50, "完成" , buttonRecogCallback, bg = (255,255,255), color = (0,0,0)) #打开手写数字交互窗口,按下“Q”键退出窗口 if_run = 1 while (if_run == 1): #如果“识别”按键被按下,通过for循环对每个黑色块内手写字进行识别处理,将识别结果依次显示出来 if predictOnce: predictOnce = False buttonResult.txt = "计算机密码录入:" for cavas in canvases: buttonResult.txt += str(Model.predict(model, cavas.get())) #打开与显示交互窗口,如果按下Q键,将无法进入下一次while循环 if screen.show(): if_run = 0 screen.quit()[/mw_shl_code] 运行效果: |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed