使用云训练平台训练好了模型部署过程中,1_2_2版本的,屏幕不能正常显示

Viewed 64

我使用云训练平台训练好了模型部署过程中,先是使用了1_3版本的,检测正常,屏幕也显示正常,又试了1_2_2版本的,屏幕就不能正常显示,下面的代码是我修改过后,加了串口发送检测结果的代码,屏幕依旧不能正常显示,但是串口发送的数据是正常的,我使用的是亚博智能的k230视觉模块,请问一下是哪里的配置有问题吗?还是屏幕的分辨率不匹配吗?

2 Answers
import gc
import os
import time
import aicube
import image
import nncase_runtime as nn
import ujson
import ulab.numpy as np
from libs.PipeLine import ScopedTiming
from libs.Utils import *
from media.display import *
from media.media import *
from media.sensor import *
from ybUtils.YbUart import YbUart  # 亚博智能串口库

# 设置显示模式
display_mode = "hdmi"
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(640, 16)
OUT_RGB888P_HEIGH = 360

root_path = "/sdcard/mp_deployment_source/"
config_path = root_path + "deploy_config.json"
deploy_conf = {}
debug_mode = 1

# 初始化串口
try:
    uart = YbUart(baudrate=115200)
    print("UART initialized successfully")
except Exception as e:
    print(f"Error initializing UART: {e}")
    uart = None

# 检测结果计数器
ripe_count = 0
unripe_count = 0
spot_detected = False
last_report_time = time.time()

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():
    global ripe_count, unripe_count, spot_detected, last_report_time
    
    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,
                )
                
                # 重置计数器
                ripe_count = 0
                unripe_count = 0
                spot_detected = False
                
                # 绘制结果并统计
                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:])
                        
                        # 统计检测结果
                        class_id = det_boxe[0]
                        if class_id == 0:  # 成熟草莓
                            ripe_count += 1
                        elif class_id == 1:  # 不成熟草莓
                            unripe_count += 1
                        elif class_id == 2:  # 斑点
                            spot_detected = True
                
                # 显示结果
                Display.show_image(osd_img, 0, 0, Display.LAYER_OSD3)
                
                # 通过串口发送检测结果
                current_time = time.time()
                if uart and (current_time - last_report_time > 0.5 or ripe_count > 0 or unripe_count > 0 or spot_detected):
                    # 发送草莓检测结果
                    message = f"Ripe:{ripe_count}, Unripe:{unripe_count}\n"
                    uart.send(message)
                    
                    # 如果检测到斑点
                    if spot_detected:
                        uart.send("Spot detected\n")
                    
                    last_report_time = current_time
                
                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__":
    try:
        detection()
    except Exception as e:
        print(f"程序异常: {e}")
    finally:
        # 关闭串口
        if uart:
            uart.deinit()
            print("UART closed")

改一下屏幕分辨率,我记得亚博的屏幕不是800*480的

OUT_RGB888P_WIDTH = ALIGN_UP(640, 16)
OUT_RGB888P_HEIGH = 360
这个地方修改吗?

不是,是DISPLAY_WIDTH和DISPLAY_HEIGHT