庐山派双摄像头如何同时运行AI和色块检测程序

Viewed 85

期待结果和实际结果
我希望一个sensor2运行AI程序、sensor1运行检测色块之类的程序
但尝试了很多次一直报各种错或者效果不理想、有没有大佬能帮助一下

重现步骤
以下是我的双摄像头显示程序
#作 用: 导入模块

from media.sensor import *
from media.display import *
from media.media import *
from libs.PlatTasks import DetectionApp
from libs.Utils import *
import image
import time, gc, ujson
#------------------------------------定义变量区---------------------------------------------------
sensor2 = None
sensor1=None

#-----------------------------------------------------------------------------------------------


#作 用: 主函数执行
#填入值:无
#返回值:无
def main():
    try:
#------------------------------------构建摄像头与显示对象-----------------------------------------
        #构建 Sensor 对象 sensor2
        sensor2 = Sensor(id=2)
        sensor2.reset()
        sensor2.set_framesize(width = 400, height = 240, chn = CAM_CHN_ID_0)
        sensor2.set_pixformat(Sensor.RGB565, chn = CAM_CHN_ID_0)
        sensor2.set_framesize(width=400,height=240, chn = CAM_CHN_ID_1)
        sensor2.set_pixformat(Sensor.RGB888, chn = CAM_CHN_ID_1)
        # 构建 Sensor 对象 sensor1
        sensor1 = Sensor(id=1)
        sensor1.reset()
        sensor1.set_framesize(width=400, height=240, chn = CAM_CHN_ID_0)
        sensor1.set_pixformat(Sensor.RGB565, chn = CAM_CHN_ID_0)
        sensor1.set_framesize(width=400,height=240,chn = CAM_CHN_ID_1)
        sensor1.set_pixformat(Sensor.RGB565, chn = CAM_CHN_ID_1)
        sensor1.set_hmirror(True)
        sensor1.set_vflip(True)
        #构建Display对象
        Display.init(Display.ST7701, to_ide = True, osd_num = 2)
        MediaManager.init()
        sensor1.run()
        sensor2.run()
#----------------------------------------------------------------------------------------------
        clock = time.clock()        #初始化帧率

        while True:
            clock.tick()            #记录当前时间(毫秒),可用于FPS计算
            os.exitpoint()
            img1 = sensor1.snapshot(chn = CAM_CHN_ID_0)
            img2 = sensor2.snapshot(chn = CAM_CHN_ID_0)
#------------------------------------主要函数区--------------------------------------------------




#----------------------------------------------------------------------------------------------
            fps  =f'FPS: {clock.fps():.3f}'                            #接收帧率信息
            img1.draw_string_advanced(0,0,20,fps,color=(255,255,255))   #绘制出帧率信息
            Display.show_image(img1, x=0,y=0, layer = Display.LAYER_OSD0)
            Display.show_image(img2, x=400,y=240, layer = Display.LAYER_OSD1)

    except KeyboardInterrupt as e:
        print("user stop: ", e)
    except BaseException as e:
        print(f"Exception {e}")
    finally:
        os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
        sensor1.stop()
        sensor2.stop()
        # deinit display
        Display.deinit()
        time.sleep_ms(100)
        # release media buffer
        MediaManager.deinit()


#作 用: 设置主函数
#填入值:无
#返回值:无
if __name__=="__main__":
    main()


以下是我的AI程序

# -*- coding: utf-8 -*-
'''
Script: deploy_det_video.py
脚本名称:deploy_det_video.py

Description:
    This script runs a real-time object detection application on an embedded device.
    It uses a pipeline to capture video frames, performs inference using a pre-trained Kmodel,
    and displays the detection results (bounding boxes, class labels) on screen.

    The model configuration is loaded from the Canaan online training platform via a JSON config file.

脚本说明:
    本脚本在嵌入式设备上运行实时目标检测应用。它通过捕获视频帧,使用预训练的 Kmodel 进行推理,并在屏幕上显示检测结果(边界框、类别标签)。

    模型配置文件通过 Canaan 在线训练平台从 JSON 文件加载。

Author: Canaan Developer
作者:Canaan 开发者
'''


import os, gc
from libs.PlatTasks import DetectionApp
from libs.PipeLine import PipeLine
from libs.Utils import *

# Set display mode: options are 'hdmi', 'lcd', 'lt9611', 'st7701', 'hx8399'
# 'hdmi' defaults to 'lt9611' (1920x1080); 'lcd' defaults to 'st7701' (800x480)
display_mode = "lt9611"

# Define the input size for the RGB888P video frames
rgb888p_size = [1280, 720]

# Set root directory path for model and config
root_path = "/sdcard/mp_deployment_source/"

# Load deployment configuration
deploy_conf = read_json(root_path + "/deploy_config.json")
kmodel_path = root_path + deploy_conf["kmodel_path"]              # KModel path
labels = deploy_conf["categories"]                                # Label list
confidence_threshold = deploy_conf["confidence_threshold"]        # Confidence threshold
nms_threshold = deploy_conf["nms_threshold"]                      # NMS threshold
model_input_size = deploy_conf["img_size"]                        # Model input size
nms_option = deploy_conf["nms_option"]                            # NMS strategy
model_type = deploy_conf["model_type"]                            # Detection model type
anchors = []
if model_type == "AnchorBaseDet":
    anchors = deploy_conf["anchors"][0] + deploy_conf["anchors"][1] + deploy_conf["anchors"][2]

# Inference configuration
inference_mode = "video"                                          # Inference mode: 'video'
debug_mode = 0                                                    # Debug mode flag

# Create and initialize the video/display pipeline
pl = PipeLine(rgb888p_size=rgb888p_size, display_mode=display_mode)
pl.create()
display_size = pl.get_display_size()

# Initialize object detection application
det_app = DetectionApp(inference_mode,kmodel_path,labels,model_input_size,anchors,model_type,confidence_threshold,nms_threshold,rgb888p_size,display_size,debug_mode=debug_mode)

# Configure preprocessing for the model
det_app.config_preprocess()

# Main loop: capture, run inference, display results
while True:
    with ScopedTiming("total", 1):
        img = pl.get_frame()                          # Capture current frame
        res = det_app.run(img)                        # Run inference
        det_app.draw_result(pl.osd_img, res)          # Draw detection results
        pl.show_image()                               # Show result on display
        gc.collect()                                  # Run garbage collection

# Cleanup: These lines will only run if the loop is interrupted (e.g., by an IDE break or external interruption)
det_app.deinit()                                      # De-initialize detection app
pl.destroy()                                          # Destroy pipeline instance

以下是我的色块检测类程序

import time, os, sys

from machine import UART
from machine import FPIOA
from media.sensor import *
from media.display import *
from media.media import *

try:
    # 配置引脚
    fpioa = FPIOA()
    fpioa.set_function(11, FPIOA.UART2_TXD)
    fpioa.set_function(12, FPIOA.UART2_RXD)

    # 初始化UART2,波特率115200,8位数据位,无校验,1位停止位
    uart = UART(UART.UART2, baudrate=115200, bits=UART.EIGHTBITS, parity=UART.PARITY_NONE, stop=UART.STOPBITS_ONE)

    # 获取传感器数据(假设为变量sensor_value)
    sensor_value = 0  # 示例数据

    # 构造一个具有默认配置的摄像头对象
    sensor = Sensor(id=2)
    # 重置摄像头sensor
    sensor.reset()

    # 设置水平镜像和设置垂直翻转
    # sensor.set_hmirror(False)  sensor.set_vflip(False)

    # 设置通道0的输出尺寸为显示分辨率
    sensor.set_framesize(width=800, height=480, chn=CAM_CHN_ID_0)
    # 设置通道0的输出像素格式为RGB565
    sensor.set_pixformat(Sensor.RGB565, chn=CAM_CHN_ID_0)

    # 根据模式初始化显示器
    Display.init(Display.ST7701, width=800, height=480, to_ide=True)
    # 初始化媒体管理器
    MediaManager.init()
    # 启动传感器
    sensor.run()

    while True:
        # 捕获通道0的图像
        img = sensor.snapshot(chn=CAM_CHN_ID_0)

        #color_threshold 是要寻找的颜色的阈值,area_threshold 表示过滤掉小于此面积的色块。
        blobs = img.find_blobs([(34, 92, -49, 127, -61, 50)],roi=(0,240,800,240),area_threshold = 10000)

        if blobs:
            # 遍历每个检测到的颜色块
            for blob in blobs:
                # 绘制颜色块的外接矩形
                # blob[0:4] 表示颜色块的矩形框 [x, y, w, h],
                img.draw_rectangle(blob[0:4])

                # 在颜色块的中心绘制一个十字
                # blob[5] 和 blob[6] 分别是颜色块的中心坐标 (cx, cy)
                img.draw_line(blob[5],240,blob[5],480,color=(0,255,255),thickness=5)

                # 中线
                img.draw_line(400,0,400,480,color=(0,0,0),thickness=3)

                # 在控制台输出颜色块的中心坐标
                print("Blob Center: X={}, Y={}".format(blob[5], blob[6]))
                message = "Y:{}\n".format(blob[5])
                uart.write(message)

        # 显示捕获的图像,中心对齐,居中显示
        Display.show_image(img)
        os.exitpoint()
except KeyboardInterrupt as e:
    print("用户停止: ", e)
except BaseException as e:
    print(f"异常: {e}")
finally:
    # 停止传感器运行
    if isinstance(sensor, Sensor):
        sensor.stop()
    # 反初始化显示模块
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # 释放媒体缓冲区
    MediaManager.deinit()

软硬件版本信息
庐山派+屏幕+最新固件+canMV IDE

补充材料
9e4569505188832c750787e5c1107348.jpg

1 Answers

pipeline类里面有关于sensor的初始化吧(个人猜想),按嘉楠的示例来看,sensor没有显式初始化就能运行摄像头,应该是被封装了