1.K210如何在一个程序中实现不同的识别
1.1.怎么实现不同的识别模块在一个程序
要实现不同的识别在同一个程序,而不重新加载模型,导致的内存溢出。是这一部分的关键,需要有外部改变条件,怎么改变条件呢,下面的串口便是(标题2),话不多说直接上代码:
import sensor, image, time, lcd
from machine import UART
from fpioa_manager import fm
from maix import KPU
import gc
fm.register(7, fm.fpioa.UART1_TX, force=True) #10为tx接rx
fm.register(6, fm.fpioa.UART1_RX, force=True) #11为rx接tx
uart_A = UART(UART.UART1, 921600, 8, 1, 0, timeout=1000, read_buf_len=4096)
lcd.init()
lcd.clear(lcd.RED)
sensor.reset()
sensor.set_hmirror(0)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
def program_a():
gc.collect()
print('Running Program A.')
od_img1 = image.Image(size=(320,256)) # 设置图像尺寸//-16.9
anchor = (0.893, 1.463, 0.245, 0.389, 1.55, 2.58, 0.375, 0.594, 3.099, 5.038, 0.057, 0.090, 0.567, 0.904, 0.101, 0.160, 0.159, 0.255)
kpu1 = KPU()
kpu1.load_kmodel('/sd/yolo_face_detect.kmodel') # 模型保存在SD卡中,从SD卡中直接加载模型
kpu1.init_yolo2(anchor, anchor_num=9, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.3, classes=1)
while True:
clock.tick()
img1 = sensor.snapshot()
a = od_img1.draw_image(img1, 0,0)
od_img1.pix_to_ai()
# 对摄像头采集到的图像传递到KPU单元进行yolo模型运算//-25
kpu1.run_with_output(od_img1)
dect = kpu1.regionlayer_yolo2()
fps = clock.fps()
if len(dect) > 0: # 识别到人脸
print("dect:",dect)
for l in dect : # 画矩形框,框起人脸
a = img1.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0))
a = img1.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0) #在LCD上显示帧率
lcd.display(img1)
compressed_img = img1.compress(quality=60)
image_bytes = compressed_img.to_bytes()
#print("send len: 压缩后的字节数 ", len(image_bytes))
uart_A.write(image_bytes)
#uart_B.write(image_bytes)
gc.collect() # 内存回收机制
#kpu1.deinit() # 释放KPU资源
#del od_img1 # 删除图像对象
#gc.collect() # 再次进行内存回收//+16
if uart_A.any():
data = uart_A.read(128)
print("Received data:", data.decode())
if '0' in data.decode():
print("EXIT A PROCESS")
kpu1.deinit() # 释放KPU资源
del od_img1 # 删除图像对象
gc.collect() # 再次进行内存回收//+16
break
else:
print("Invalid input from UART. Please send '1-5' for Program A-E")
def program_b():
gc.collect()
print('Running Program B.')
#人脸检测模型需要320*256图输入,这里初始化一个image
od_img2 = image.Image(size=(320,256), copy_to_fb=False)
anchor = (0.156250, 0.222548, 0.361328, 0.489583, 0.781250, 0.983133, 1.621094, 1.964286, 3.574219, 3.94000)
# 创建一个kpu对象,用于人脸检测
kpu2= KPU()
print("ready load model")
# 加载模型
kpu2.load_kmodel("/sd/detect_5.kmodel")
# yolo2初始化
kpu2.init_yolo2(anchor, anchor_num=5, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.4, classes=2)
while True:
#print("mem free:",gc.mem_free())
clock.tick() # 更新计算帧率的clock
img2 = sensor.snapshot() # 拍照,获取一张图像
a = od_img2.draw_image(img2, 0,0) # 将img图像写到od_img图像的坐标(0,0)位置处
od_img2.pix_to_ai() # 对rgb565的image生成ai运算需要的r8g8b8格式存储
kpu2.run_with_output(od_img2) # 对输入图像进行kpu运算
dect = kpu2.regionlayer_yolo2() # yolo2后处理
fps = clock.fps() # 获取帧率
# 画出人脸框,并判断是否带有口罩
if len(dect) > 0:
#uart_A.write('34')
#print("dect:",dect)
for l in dect :
if l[4] :
a = img2.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0))
a = img2.draw_string(l[0],l[1]-24, "with mask", color=(0, 255, 0), scale=2)
else:
a = img2.draw_rectangle(l[0],l[1],l[2],l[3], color=(255, 0, 0))
a = img2.draw_string(l[0],l[1]-24, "without mask", color=(255, 0, 0), scale=2)
a = img2.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0)
lcd.display(img2)
compressed_img2 = img2.compress(quality=40)
image_bytes2 = compressed_img2.to_bytes()
uart_A.write(image_bytes2)
gc.collect()
if uart_A.any():
data = uart_A.read(128)
print("Received data:", data.decode())
if '0' in data.decode():
print("EXIT B PROCESS")
kpu2.deinit() # 释放KPU资源
del od_img2 # 删除图像对象
gc.collect() # 再次进行内存回收
break
else:
print("Invalid input from UART. Please send '1-5' for Program A-E")
while True:
gc.collect() # 内存回收机制
clock.tick()
img = sensor.snapshot()
lcd.display(img)
print("71 mem free:",gc.mem_free()) # 查询剩余内存
if uart_A.any():
data = uart_A.read(128)
print("Received data:", data.decode())
if '1' in data.decode():
program_a()
elif '2' in data.decode():
program_b()
elif '3' in data.decode():
program_c()
elif '4' in data.decode():
program_d()
elif '5' in data.decode():
program_e()
else:
print("Invalid input from UART. Please send '1-5' for Program A-E")
上面的代码恒容易看出来,只需要通过输入端改变串口输入内容,同时保证占用内存资源的变量及时释放内存。
下面将串口程序单独罗列便于查看。
2.K210串口使用进行数据上传
直接上代码
from machine import UART
from fpioa_manager import fm
fm.register(7, fm.fpioa.UART1_TX, force=True) #10为tx接rx
fm.register(6, fm.fpioa.UART1_RX, force=True) #11为rx接tx
uart_A = UART(UART.UART1, 921600, 8, 1, 0, timeout=1000, read_buf_len=4096)
uart_A.write("hello")
加载包,初始化,使用,简单明了。