K230,固件是2.9.0,想实现模式a(循迹)和模式b(识别1-8数字)的转换。
实验名称:UART(串口通信)
作者:01Studio
实验平台:01Studio CanMV K230
说明:通过编程实现串口通信,跟电脑串口助手实现数据收发,当串口接收到数据时,LED点亮。
导入串口模块
from machine import UART
from machine import FPIOA
from machine import Pin # 导入Pin模块
import time
import math
from media.sensor import * # 导入sensor模块,使用摄像头相关接口
from media.display import * # 导入display模块,使用display相关接口
from media.media import * # 导入media模块,使用meida相关接口
import os
import ujson
import aicube
from libs.PipeLine import ScopedTiming
from libs.Utils import *
import nncase_runtime as nn
import ulab.numpy as np
import image
import gc
fpioa = FPIOA()
UART1代码
fpioa.set_function(3, FPIOA.UART1_TXD)
fpioa.set_function(4, FPIOA.UART1_RXD)
uart = UART(UART.UART1, 115200) # 设置串口号1和波特率
GRAYSCALE_THRESHOLD = [(0, 64)]
采样图像为QVGA 320*240,列表把roi把图像分成3个矩形,越靠近的摄像头视野(通常为图像下方)的矩形权重越大。
ROIS = [ # [ROI, weight]
(0, 200, 320, 40, 0.7), # 可以根据不同机器人情况进行调整。
(0, 100, 320, 40, 0.3),
(0, 0, 320, 40, 0.1)
]
计算以上3个矩形的权值【weight】的和,和不需要一定为1.
weight_sum = 0
for r in ROIS:
weight_sum += r[4] # r[4] 为矩形权重值.
构建led对象,GPIO52,输出
LED = Pin(52, Pin.OUT)
def mode_a():
sensor = Sensor(width=1280, height=960) # 构建摄像头对象,将摄像头长宽设置为4:3
sensor.reset() # 复位和初始化摄像头
sensor.set_framesize(width=640, height=480) # 设置帧大小,默认通道0
sensor.set_pixformat(Sensor.GRAYSCALE) # 设置输出图像格式,默认通道0
Display.init(Display.ST7701, to_ide=True) # 同时使用3.5寸mipi屏和IDE缓冲区显示图像,800x480分辨率
# Display.init(Display.VIRT, sensor.width(), sensor.height()) # 只使用IDE缓冲区显示图像
MediaManager.init() # 初始化media资源管理器
sensor.run() # 启动sensor
clock = time.clock()
while True:
clock.tick()
img = sensor.snapshot() # 拍摄一张图片
centroid_sum = 0
for r in ROIS:
blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=r[0:4], merge=True) # r[0:4] 是上面定义的roi元组.
if blobs:
# Find the blob with the most pixels.
largest_blob = max(blobs, key=lambda b: b.pixels())
# Draw a rect around the blob.
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(),
largest_blob.cy())
centroid_sum += largest_blob.cx() * r[4] # r[4] 是每个roi的权重值.
center_pos = (centroid_sum / weight_sum) # 确定直线的中心.
# 将直线中心位置转换成角度,便于机器人处理.
deflection_angle = 0
# 使用反正切函数计算直线中心偏离角度。可以自行画图理解
# 权重X坐标落在图像左半部分记作正偏,落在右边部分记为负偏,所以计算结果加负号。
# deflection_angle = -math.atan((center_pos-80)/60) # 采用图像为QQVGA 160*120时候使用
deflection_angle = -math.atan((center_pos - 160) / 120) # 采用图像为QVGA 320*240时候使用
# 将偏离值转换成偏离角度.
deflection_angle = math.degrees(deflection_angle)
# 计算偏离角度后可以控制机器人进行调整.
# print("Turn Angle: %f" % deflection_angle)
# LCD显示偏移角度,scale参数可以改变字体大小
img.draw_string_advanced(2, 2, 20, str('%.1f' % deflection_angle), color=(255, 255, 255))
# 显示图片,仅用于LCD居中方式显示
Display.show_image(img, x=round((800 - sensor.width()) / 2), y=round((480 - sensor.height()) / 2))
# 再次读取串口数据
new_text = uart.read(128)
if new_text is not None and b'b' not in new_text:
print(new_text) # 通过REPL打印串口3接收的数据
LED.value(1) # 熄灭LED
mode_b()
# 清理资源
sensor.deinit()
Display.deinit()
MediaManager.deinit()
break
def mode_b():
display_mode="lcd"
if display_mode=="lcd":
DISPLAY_WIDTH = ALIGN_UP(800, 16)
DISPLAY_HEIGHT = 480
else:
DISPLAY_WIDTH = ALIGN_UP(1920, 16)
DISPLAY_HEIGHT = 1080
OUT_RGB888P_WIDTH = ALIGN_UP(1280, 16)
OUT_RGB888P_HEIGH = 720
root_path="/sdcard/mp_deployment_source/"
config_path=root_path+"deploy_config.json"
deploy_conf={}
debug_mode=1
def two_side_pad_param(input_size,output_size):
ratio_w = output_size[0] / input_size[0] # 宽度缩放比例
ratio_h = output_size[1] / input_size[1] # 高度缩放比例
ratio = min(ratio_w, ratio_h) # 取较小的缩放比例
new_w = int(ratio * input_size[0]) # 新宽度
new_h = int(ratio * input_size[1]) # 新高度
dw = (output_size[0] - new_w) / 2 # 宽度差
dh = (output_size[1] - new_h) / 2 # 高度差
top = int(round(dh - 0.1))
bottom = int(round(dh + 0.1))
left = int(round(dw - 0.1))
right = int(round(dw - 0.1))
return top, bottom, left, right,ratio
def read_deploy_config(config_path):
# 打开JSON文件以进行读取deploy_config
with open(config_path, 'r') as json_file:
try:
# 从文件中加载JSON数据
config = ujson.load(json_file)
except ValueError as e:
print("JSON 解析错误:", e)
return config
def detection():
print("det_infer start")
# 使用json读取内容初始化部署变量
deploy_conf=read_deploy_config(config_path)
kmodel_name=deploy_conf["kmodel_path"]
labels=deploy_conf["categories"]
confidence_threshold= deploy_conf["confidence_threshold"]
nms_threshold = deploy_conf["nms_threshold"]
img_size=deploy_conf["img_size"]
num_classes=deploy_conf["num_classes"]
color_four=get_colors(num_classes)
nms_option = deploy_conf["nms_option"]
model_type = deploy_conf["model_type"]
if model_type == "AnchorBaseDet":
anchors = deploy_conf["anchors"][0] + deploy_conf["anchors"][1] + deploy_conf["anchors"][2]
kmodel_frame_size = img_size
frame_size = [OUT_RGB888P_WIDTH,OUT_RGB888P_HEIGH]
strides = [8,16,32]
# 计算padding值
top, bottom, left, right,ratio=two_side_pad_param(frame_size,kmodel_frame_size)
# 初始化kpu
kpu = nn.kpu()
kpu.load_kmodel(root_path+kmodel_name)
# 初始化ai2d
ai2d = nn.ai2d()
ai2d.set_dtype(nn.ai2d_format.NCHW_FMT,nn.ai2d_format.NCHW_FMT,np.uint8, np.uint8)
ai2d.set_pad_param(True, [0,0,0,0,top,bottom,left,right], 0, [114,114,114])
ai2d.set_resize_param(True, nn.interp_method.tf_bilinear, nn.interp_mode.half_pixel )
ai2d_builder = ai2d.build([1,3,OUT_RGB888P_HEIGH,OUT_RGB888P_WIDTH], [1,3,kmodel_frame_size[1],kmodel_frame_size[0]])
# 初始化并配置sensor
sensor = Sensor()
sensor.reset()
# 设置镜像
sensor.set_hmirror(False)
# 设置翻转
sensor.set_vflip(False)
# 通道0直接给到显示VO,格式为YUV420
sensor.set_framesize(width = DISPLAY_WIDTH, height = DISPLAY_HEIGHT)
sensor.set_pixformat(PIXEL_FORMAT_YUV_SEMIPLANAR_420)
# 通道2给到AI做算法处理,格式为RGB888
sensor.set_framesize(width = OUT_RGB888P_WIDTH , height = OUT_RGB888P_HEIGH, chn=CAM_CHN_ID_2)
sensor.set_pixformat(PIXEL_FORMAT_RGB_888_PLANAR, chn=CAM_CHN_ID_2)
# 绑定通道0的输出到vo
sensor_bind_info = sensor.bind_info(x = 0, y = 0, chn = CAM_CHN_ID_0)
Display.bind_layer(**sensor_bind_info, layer = Display.LAYER_VIDEO1)
if display_mode=="lcd":
# 设置为ST7701显示,默认800x480
Display.init(Display.ST7701, to_ide = True)
else:
# 设置为LT9611显示,默认1920x1080
Display.init(Display.LT9611, to_ide = True)
#创建OSD图像
osd_img = image.Image(DISPLAY_WIDTH, DISPLAY_HEIGHT, image.ARGB8888)
# media初始化
MediaManager.init()
# 启动sensor
sensor.run()
rgb888p_img = None
ai2d_input_tensor = None
data = np.ones((1,3,kmodel_frame_size[1],kmodel_frame_size[0]),dtype=np.uint8)
ai2d_output_tensor = nn.from_numpy(data)
while True:
with ScopedTiming("total",debug_mode > 0):
rgb888p_img = sensor.snapshot(chn=CAM_CHN_ID_2)
if rgb888p_img.format() == image.RGBP888:
ai2d_input = rgb888p_img.to_numpy_ref()
ai2d_input_tensor = nn.from_numpy(ai2d_input)
# 使用ai2d进行预处理
ai2d_builder.run(ai2d_input_tensor, ai2d_output_tensor)
# 设置模型输入
kpu.set_input_tensor(0, ai2d_output_tensor)
# 模型推理
kpu.run()
# 获取模型输出
results = []
for i in range(kpu.outputs_size()):
out_data = kpu.get_output_tensor(i)
result = out_data.to_numpy()
result = result.reshape((result.shape[0]*result.shape[1]*result.shape[2]*result.shape[3]))
del out_data
results.append(result)
# 使用aicube模块封装的接口进行后处理
det_boxes = aicube.anchorbasedet_post_process( results[0], results[1], results[2], kmodel_frame_size, frame_size, strides, num_classes, confidence_threshold, nms_threshold, anchors, nms_option)
# 绘制结果
osd_img.clear()
if det_boxes:
for det_boxe in det_boxes:
x1, y1, x2, y2 = det_boxe[2],det_boxe[3],det_boxe[4],det_boxe[5]
x=int(x1 * DISPLAY_WIDTH // OUT_RGB888P_WIDTH)
y=int(y1 * DISPLAY_HEIGHT // OUT_RGB888P_HEIGH)
w = int((x2 - x1) * DISPLAY_WIDTH // OUT_RGB888P_WIDTH)
h = int((y2 - y1) * DISPLAY_HEIGHT // OUT_RGB888P_HEIGH)
osd_img.draw_rectangle(x, y, w, h, color=color_four[det_boxe[0]][1:])
text=labels[det_boxe[0]] + " " + str(round(det_boxe[1],2))
osd_img.draw_string_advanced(x,y-40,32,text, color=color_four[det_boxe[0]][1:])
Display.show_image(osd_img, 0, 0, Display.LAYER_OSD3)
gc.collect()
rgb888p_img = None
del ai2d_input_tensor
del ai2d_output_tensor
#停止摄像头输出
sensor.stop()
#去初始化显示设备
Display.deinit()
#释放媒体缓冲区
MediaManager.deinit()
gc.collect()
time.sleep(1)
nn.shrink_memory_pool()
print("det_infer end")
return 0
if __name__=="__main__":
detection()
text = uart.read(128) # 接收128个字符
if new_text is not None and b'a' not in new_text:
print(new_text) # 通过REPL打印串口3接收的数据
mode_a()
LED.value(1) # 熄灭LED
# 清理资源
sensor.deinit()
Display.deinit()
MediaManager.deinit()
while True:
text = uart.read(128) # 接收128个字符
if text is not None:
print(text);
if b'a' in text:
print(text) # 通过REPL打印串口3接收的数据
#if b'a' in text:
LED.value(1) # 点亮LED,也可以使用led.on()
mode_a()
else:
print(text) # 通过REPL打印串口3接收的数据
#if b'a' in text:
LED.value(1) # 点亮LED,也可以使用led.on()
mode_b()
time.sleep(0.1) # 100ms