问题描述
import os, time, gc
import nncase_runtime as nn
import ulab.numpy as np
from media.sensor import *
from media.display import *
from media.media import *
kmodel_path = "/sdcard/examples/kmodel/yolov5s.kmodel"
input_w = 320
input_h = 320
rgb888p_size = [640, 360]
sensor = Sensor(fps=30)
sensor.reset()
sensor.set_framesize(w=rgb888p_size[0], h=rgb888p_size[1], chn=CAM_CHN_ID_2)
sensor.set_pixformat(Sensor.RGBP888, chn=CAM_CHN_ID_2)
Display.init(Display.ST7701, width=800, height=480, to_ide=True)
MediaManager.init()
sensor.run()
kpu = nn.kpu()
kpu.load_kmodel(kmodel_path)
input_data = np.ones((1, 3, input_h, input_w), dtype=np.uint8)
kpu_input_tensor = nn.from_numpy(input_data)
kpu.set_input_tensor(0, kpu_input_tensor)
ai2d = nn.ai2d()
ai2d.set_dtype(nn.ai2d_format.NCHW_FMT, nn.ai2d_format.NCHW_FMT, np.uint8, np.uint8)
ai2d.set_resize_param(True, nn.interp_method.tf_bilinear, nn.interp_mode.half_pixel)
ai2d_builder = ai2d.build([1, 3, rgb888p_size[1], rgb888p_size[0]], [1, 3, input_h, input_w])
fps = time.clock()
try:
while True:
os.exitpoint()
fps.tick()
img = sensor.snapshot(chn=CAM_CHN_ID_2)
img_np = img.to_numpy_ref()
shape = img_np.shape
img_chw = img_np.reshape((shape[0]*shape[1], shape[2])).transpose().copy().reshape((shape[2], shape[0], shape[1]))
ai2d_input = nn.from_numpy(img_chw)
ai2d_builder.run(ai2d_input, kpu_input_tensor)
kpu.run()
print(fps.fps())
gc.collect()
except KeyboardInterrupt:
pass
except BaseException as e:
print("exit:", e)
finally:
sensor.stop()
Display.deinit()
MediaManager.deinit()
软件版本
CanMV_K230D_MINI_micropython_local_nncase_v2.11.0