K230多次调用PipeLine摄像头无法初始化

Viewed 73

重现步骤

1.当初次调用PipeLine时,摄像头正常初始化
2.当再次调用PipeLine时,就卡在这个步骤了
补充材料

代码如下

def image_recognition(sensor_num):
    # 初始化PipeLine
    pl=PipeLine(rgb888p_size=rgb888p_size,display_mode=display_mode)
    # 构建摄像头,使用1280x960分辨率,运行流畅,画面畸变不严重
    sensor = Sensor(id=sensor_num, width=1280, height = 960)
    # 复位 sensor
    sensor.reset()
    # 创建PipeLine
    pl.create(sensor)
    # 设置display_size
    display_size = pl.get_display_size()
    # 初始化YOLO11实例
    yolo=YOLO11(task_type = "obb", mode = "image", kmodel_path = kmodel_path, labels=labels, rgb888p_size = rgb888p_size, model_input_size = model_input_size, conf_thresh = confidence_threshold, nms_thresh = nms_threshold, max_boxes_num = 1, debug_mode = 0)
    # YOLO11 预处理配置函数
    yolo.config_preprocess()
    # 获取当前帧照片
    img = pl.get_frame()
    # 对照片进行推测
    res = yolo.run(img)
    # 打印识别结果
    print(res)
    # 将结果渲染到屏幕上
    yolo.draw_result(res, pl.osd_img)
    # 释放 yolo 资源
    yolo.deinit()
    # 销毁PipeLine实例
    pl.destroy()
    # 内存回收
    gc.collect()
    # 返回识别结果
    return res
2 Answers

不要多次调用,初始化做一次就够了

我每次需要调用不同的摄像头

可以把PipeLine的初始化过程拿到外边来,然后一次性初始化多个摄像头

那我在获取图像的时候,该如何选择对应的摄像头呢

# 初始化变量
sensor = None
sensor1 = None
# kmodel配置
kmodel_path = "/data/yolo11s.kmodel"
# 定义标签
labels = ['pen']
# 模型输入分辨率
model_input_size=[480,480]
# 添加显示模式,默认hdmi,可选hdmi/lcd/lt9611/st7701/hx8399,其中hdmi默认置为lt9611,分辨率1920*1080;lcd默认置为st7701,分辨率800*480
display_mode="lcd"
# 推理帧分辨率
rgb888p_size=[1280,960]
# 置信度阈值
confidence_threshold = 0.1
# nms阈值
nms_threshold=0.6
# 启动自动垃圾回收
gc.enable()

try:
    # 构建摄像头,使用1280x960分辨率,运行流畅,画面畸变不严重
    sensor = Sensor(id=1, width=1280, height = 960)
    # 复位 sensor
    sensor.reset()
    sensor.set_framesize(width=1280, height=960)
    # 设置通道0的输出像素格式为RGB565
    sensor.set_pixformat(Sensor.RGB565)



    # 构建摄像头,使用1280x960分辨率,运行流畅,画面畸变不严重
    sensor1 = Sensor(id=2, width=1280, height = 960)
    # 复位 sensor
    sensor1.reset()
    sensor1.set_framesize(width=1280, height=960)
    # 设置通道0的输出像素格式为RGB565
    sensor1.set_pixformat(Sensor.RGB565)

    MediaManager.init()

    sensor.run()
    yolo=YOLO11(task_type = "obb", mode = "image", kmodel_path = kmodel_path, labels=labels, rgb888p_size = rgb888p_size, model_input_size = model_input_size, conf_thresh = confidence_threshold, nms_thresh = nms_threshold, max_boxes_num = 1, debug_mode = 0)
    # YOLO11 预处理配置函数
    yolo.config_preprocess()
    print("温度:", machine.temperature())
    print("内存占用情况:", gc.sys_heap())


except BaseException as e:
    print(f"异常: '{e}'")

你把模型换成kmodel_path = "/sdcard/examples/kmodel/yolo11n-obb.kmodel"试试,我使用你的代码测试挺正常的

有可能是模型转的有问题,或者sensor有问题,你加一下打印,看看是哪一行出的问题

应该不是模型的问题,我执行图片识别案例和视频识别案例都没有问题,单独运行sensor也没有问题,使用PipeLine单摄像头结合YOLO也没有问题,唯独使用sensor结合YOLO时会卡死