K230脱机调节阈值与保存

Viewed 44

怎么增加脱机调节阈值的功能,并且把阈值存放在TF卡里

import time, os, sys, math
import json

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


# 配置引脚
fpioa = FPIOA()
# 将指定引脚配置为 UART 功能
fpioa.set_function(11, FPIOA.UART2_TXD)
fpioa.set_function(12, FPIOA.UART2_RXD)

sensor_id = 1
sensor = None

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

#阈值存储
threshold_dict = {'rect': [(52, 150)], 'red_point':\
[(73, 100, -4, 23, -14, 10)]}


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

    # 无需进行镜像翻转
    # 设置水平镜像
    # sensor.set_hmirror(False)
    # 设置垂直翻转
    # sensor.set_vflip(False)

    # 设置通道0的输出尺寸为128x160
    sensor.set_framesize(Sensor.QVGA, chn=CAM_CHN_ID_0)
    # 设置通道0的输出像素格式为RGB565
    sensor.set_pixformat(Sensor.RGB565, chn=CAM_CHN_ID_0)

    # 使用IDE的帧缓冲区作为显示输出
    Display.init(Display.ST7701, width=800, height=480, to_ide=True)
    # 初始化媒体管理器
    MediaManager.init()
    # 启动传感器
    sensor.run()
    clock = time.clock()
    while True:
        os.exitpoint()
        clock.tick()
        # 捕获通道0的图像
        img = sensor.snapshot(chn=CAM_CHN_ID_0)

        img_rect = img.to_grayscale(copy=True)
        img_rect = img_rect.binary([(18, 63, -20, 13, -12, 24)])

        c_x = -1
        c_y = -1

        # 在图像中寻找矩形
        for r in img_rect.find_rects(threshold = 120000):
            # 判断矩形边长是否符合要求
            if r.w() > 20 and r.h() > 20:
                # 在屏幕上框出矩形
                img.draw_rectangle(r.rect(), color = (255, 0, 0), scale = 4)
                # 获取矩形角点位置
                corner = r.corners()
                # 在屏幕上圈出矩形角点
                img.draw_circle(corner[0][0], corner[0][1], 5, color = (255, 0, 0), thickness = 2, fill = False)
                img.draw_circle(corner[1][0], corner[1][1], 5, color = (0, 255, 0), thickness = 2, fill = False)
                img.draw_circle(corner[2][0], corner[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
                img.draw_circle(corner[3][0], corner[3][1], 5, color = (255, 255, 0), thickness = 2, fill = False)

                c_x = sum([corner[k][0] for k in range(4)])/4
                c_y = sum([corner[k][1] for k in range(4)])/4

                data = "{},{};".format(round(c_x),round(c_y))

 #               print("{}".data)

                uart.write(data)
                c_x = -1
                c_y = -1
        print(clock.fps()) #打印FPS
        # 显示捕获的图像
        Display.show_image(img,x = 240,y = 120)

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()
2 Answers

你好,可以使用触摸屏,点击对应的区域,将对应的像素点设置为目标值,进行阈值的调整,然后保存到/sdcard/分区的配置文件