# generated by maixhub, tested on maixpy3 v0.4.8
# copy files to TF card and plug into board and power on
import sensor, image, lcd, time
import KPU as kpu
import gc, sys
from fpioa_manager import fm
from machine import UART
input_size = (224, 224)
labels = ['A', 'B']
anchors = [0.75, 1.22, 1.66, 1.03, 1.5, 1.5, 1.09, 1.5, 1.44, 1.75]
def lcd_show_except(e):
import uio
err_str = uio.StringIO()
sys.print_exception(e, err_str)
err_str = err_str.getvalue()
img = image.Image(size=input_size)
img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))
lcd.display(img)
class Comm:
def __init__(self, uart):
self.uart = uart
def send_detect_result(self, objects, labels):
msg = ""
for obj in objects:
pos = obj.rect()
p = obj.value()
idx = obj.classid()
label = labels[idx]
msg += "{}:{}:{}:{}:{}:{:.2f}:{}, ".format(pos[0], pos[1], pos[2], pos[3], idx, p, label)
if msg:
msg = msg[:-2] + "\n"
self.uart.write(msg.encode())
def init_uart():
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(11, fm.fpioa.UART1_RX, force=True)
uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=256)
return uart
def main(anchors, labels = None, model_addr="/sd/m.kmodel", sensor_window=input_size, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing(sensor_window)
sensor.set_hmirror(sensor_hmirror)
sensor.set_vflip(sensor_vflip)
sensor.run(1)
lcd.init(type=1)
lcd.rotation(lcd_rotation)
lcd.clear(lcd.WHITE)
if not labels:
with open('labels.txt','r') as f:
exec(f.read())
if not labels:
print("no labels.txt")
img = image.Image(size=(320, 240))
img.draw_string(90, 110, "no labels.txt", color=(255, 0, 0), scale=2)
lcd.display(img)
return 1
try:
img = image.Image("startup.jpg")
lcd.display(img)
except Exception:
img = image.Image(size=(320, 240))
img.draw_string(90, 110, "loading model...", color=(255, 255, 255), scale=2)
lcd.display(img)
uart = init_uart()
comm = Comm(uart)
try:
task = None
task = kpu.load(model_addr)
kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1]
while(True):
img = sensor.snapshot()
t = time.ticks_ms()
objects = kpu.run_yolo2(task, img)
t = time.ticks_ms() - t
if objects:
for obj in objects:
pos = obj.rect()
img.draw_rectangle(pos)
img.draw_string(pos[0], pos[1], "%s : %.2f" %(labels[obj.classid()], obj.value()), scale=2, color=(255, 0, 0))
comm.send_detect_result(objects, labels)
img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0))
lcd.display(img)
except Exception as e:
raise e
finally:
if not task is None:
kpu.deinit(task)
if __name__ == "__main__":
try:
# main(anchors = anchors, labels=labels, model_addr=0x300000, lcd_rotation=0)
main(anchors = anchors, labels=labels, model_addr="/sd/model-128515.kmodel")
except Exception as e:
sys.print_exception(e)
lcd_show_except(e)
finally:
gc.collect()
NULL指向我
- 粉丝: 2352
- 资源: 99
最新资源
- 【胸片分割】最小误差法胸片分割系统【含GUI Matlab仿真 1065期】.zip
- 【图像隐写】变换域的可逆数字水印系统设计【含GUI Matlab仿真 1813期】.zip
- 【图像融合】双树复小波变换像素级图像融合【含Matlab仿真 2024期】.zip
- 【字符识别】模板匹配(区域生长法)字母+数字识别【含Matlab仿真 1695期】.zip
- 【疾病分类】模糊逻辑分类叶病严重程度分级系统【含GUI Matlab仿真 194期】.zip
- 【人脸识别】人数统计【含GUI Matlab仿真 2121期】.zip
- 【水果识别】苹果质量检测及分级系统【含Matlab仿真 519期】.zip
- 懒鬼插件,审计过的后的渗透插件,我凭本事打的SESSION凭什么还要我自己动手后渗透?.zip
- 基于Python模块REfO实现的知识库问答初级系统(医药知识图谱)+源码+文档说明
- STM32程序 IO模拟SRAM通讯(铁电存储器 FM22L16 无FSMC)
- PHP中小型民宿酒店管理系统源码数据库 MySQL源码类型 WebForm
- 提高渗透测试效率 #Burp插件##渗透测试##小工具#.zip
- 基于知识图谱的心理咨询智能问答系统+源码+文档说明
- 收集渗透测试中的Python脚本.zip
- 全面解析 YOLOv8 目标检测的下载与实现
- 攻防演习,渗透测试资产处理小工具,对攻防演习,渗透测试前的信息搜集到的大批量资产,域名进行存活检测、获取标题头、语料提取、常见web端口检测等 .zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈