前言
本节课主要学习K210识别数字的功能,能识别手写的数字和打印的数字。
一、实验准备
请先将模型文件导入内存卡上,再将内存卡插入到K210开发板的内存卡插槽上,具体操作步骤请参考:
AI嵌入式K210项目(21)-AI模型文件导入至TF卡
本实验使用/sd/KPU/mnist/uint8_mnist_cnn_model.kmodel模型;
数字识别需要用的内存卡加载模型文件,所以需要提前将模型文件导入内存卡,再将内存卡插入K210开发板的内存卡卡槽里,如果无法读取到内存卡里的模型文件,则会报错。
二、实验过程
导入相关库,并初始化摄像头和LCD显示屏;
import sensor, image, time, lcd
from maix import KPU
import gc
lcd.init(freq=15000000)
sensor.reset() # Reset and initialize the sensor. It will
# run automatically, call sensor.run(0) to stop
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((224, 224))
sensor.skip_frames(time = 1000) # Wait for settings take effect.
clock = time.clock() # Create a clock object to track the FPS.
初始化KPU相关的参数,kpu需要加载kmodel文件,本次实验需要的模型文件路径为:/sd/KPU/mnist/uint8_mnist_cnn_model.kmodel
kpu = KPU()
kpu.load_kmodel("/sd/KPU/mnist/uint8_mnist_cnn_model.kmodel")
新建while循环读取摄像头画面,然后复制一个112*112大小的画面,对像素进行取反等处理,再将图像传入KPU里进行计算,与模型文件做运算,最终得到最优识别结果和识别分数。
while True:
gc.collect()
img = sensor.snapshot()
img_mnist1=img.to_grayscale(1) #convert to gray
img_mnist2=img_mnist1.resize(112,112)
a=img_mnist2.invert() #invert picture as mnist need
a=img_mnist2.strech_char(1) #preprocessing pictures, eliminate dark corner
a=img_mnist2.pix_to_ai()
out = kpu.run_with_output(img_mnist2, getlist=True)
max_mnist = max(out)
index_mnist = out.index(max_mnist)
#score = KPU.sigmoid(max_mnist)
display_str = "num: %d" % index_mnist
print(display_str)
a=img.draw_string(4,3,display_str,color=(0,0,0),scale=2)
lcd.display(img)
kpu.deinit()
更多API接口可以参考https://developer.canaan-creative.com/canmv/main/canmv/library/index.html
三、实验结果
将K210开发板通过TYPE-C数据线连接到电脑上,CanMV IDE点击连接按钮,连接完成后点击运行按钮,运行例程代码。也可以将代码作为main.py下载到K210开发板上运行。
等待系统初始化完成后,LCD显示摄像头画面,将摄像头拍摄自己手写的数字或打印的数字,屏幕左上角会显示当前识别到的数字。
总结
本实验使用K210的KPU和yolov2模型实现数字识别功能,使用IDE示例中代码,对实现的功能模块加以分析;可以通过调节阈值调整识别的准确率;