RuntimeError: get display buffer failed

Viewed 83

我想实现先b(识别数字),再a(巡线),再b的功能,但是在第二次进入b模式的时候显示RuntimeError: get display buffer failed的错误,请问该如何修改呢?
import time, os, sys, math,gc,image,ujson,aicube
from machine import UART
from machine import FPIOA
from machine import Pin # 导入Pin模块
from media.sensor import * # 导入sensor模块,使用摄像头相关接口
from media.display import * # 导入display模块,使用display相关接口
from media.media import * # 导入media模块,使用meida相关接口
from libs.PipeLine import ScopedTiming
from libs.Utils import *
import nncase_runtime as nn
import ulab.numpy as np
cnt=0
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)
LED.value(0)

def a():
print('init')
sensor = Sensor(width=1280, height=960)
print('init0')
sensor.reset() # 复位和初始化摄像头
print('init1')
sensor.set_framesize(width=640, height=480) # 设置帧大小,默认通道0
print('init2')
sensor.set_pixformat(Sensor.GRAYSCALE) # 设置输出图像格式,默认通道0
print('init3')
Display.init(Display.ST7701, to_ide=True) # 同时使用3.5寸mipi屏和IDE缓冲区显示图像,800x480分辨率
print('init4')
#Display.show_image(img, x=round((800 - sensor.width()) / 2), y=round((480 - sensor.height()) / 2))
# Display.init(Display.VIRT, sensor.width(), sensor.height()) # 只使用IDE缓冲区显示图像
#MediaManager.deinit()
print('init4.5')
#MediaManager.init() # 初始化media资源管理器
print('init5')
sensor.run() # 启动sensor
print('init6')
clock = time.clock()
while True:
text = uart.read(1)
if text == b'b':
print(text)

        #MediaManager.deinit()
        print('init2')
        
        #media.deinit()
        print('init3')
        sensor.stop()
        sensor.deinit()
        #__del__ (sensor)
        #去初始化显示设备
        Display.deinit()
        #释放媒体缓冲区
        MediaManager.deinit()
        gc.collect()
        time.sleep(1)
        nn.shrink_memory_pool()
        break
    gc.collect()
    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)
    #time.sleep(3)

    # LCD显示偏移角度,scale参数可以改变字体大小
    img.draw_string_advanced(2, 2, 20, str('%.1f' % deflection_angle), color=(255, 255, 255))
    print('init7')
    # 显示图片,仅用于LCD居中方式显示
    #Display.show_image(img, x=round((800 - sensor.width()) / 2), y=round((480 - sensor.height()) / 2))
    print("hello")
    uart.write("%f\n" % deflection_angle)#发送一条数据

def 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")
    global cnt
    
    # 使用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)
    print("start")
    # 初始化kpu
    kpu = nn.kpu()
    kpu.load_kmodel(root_path+kmodel_name)
    # 初始化ai2d
    print("start1")
    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
    print("start2")
    sensor = Sensor()
    print("start3")
    sensor.reset()
    print("start4")
    # 设置镜像
    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)
    print("start5")
    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初始化
    print("start6")
    
    if(cnt==0):
        MediaManager.init()
        cnt=1
        '''
    if(cnt==1):
        MediaManager._config(config)
        MediaManager.init()
        MediaManager.Buffer.get()
        '''
    print("start7")
    # 启动sensor
    sensor.run()
    print("start8")
    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:
        text = uart.read(1)
        if text == b'a':
            print(text)
            #media.deinit()
            #sensor.deinit()
            #Display.deinit()
            #MediaManager.deinit()
            break
        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)
                time.sleep(1)
                os.exitpoint()
                gc.collect()
            rgb888p_img = None
    del ai2d_input_tensor
    del ai2d_output_tensor
    #停止摄像头输出
    sensor.stop()
    #去初始化显示设备
    print("start9")
    Display.deinit()
    print("start10")
    #print("")
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    #释放媒体缓冲区
    MediaManager.deinit()
    #buffer.__del__()
    gc.collect()
    time.sleep(1)
    nn.shrink_memory_pool()
    print("det_infer end")
    return 0

if __name__=="__main__":
    detection()

while True:
gc.collect()

#MediaManager.init()
while True:
    text = uart.read(1)
    
#MediaManager.deinit()
    if text:
        if text ==b'a':
            print(text)
            gc.collect()
            a()
        if text ==b'b':
            gc.collect()
            b()
            
            '''
            media.sensor.deinit()
            media.media.deinit()
            Display.deinit()
            MediaManager.deinit()
            '''
            LED.value(1)
            time.sleep(1)
            LED.value(0)
            time.sleep(1)
1 Answers

你好,是show_image到了多个不同的osd layer吗?但是在Display.init的时候传入的osd_num为1?

请问osd是什么意思?
这个问题如何解决呢?

Display.init(Display.XXXXXX, to_ide = True, osd_num = 2), 这里增大osd num

还是不行,卡在start8了