logo

训练了识别七段数码管0-9的数字,现在想把小数点也加进去,训练完的小数点识别效果很差有什么标注建议或者解决方案吗

2025-01-06 13:10
L
dnkr
# Edge Impulse - OpenMV FOMO Object Detection Example
#
# This work is licensed under the MIT license.
# Copyright (c) 2013-2024 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE

import sensor, image, time, os, ml, math, uos, gc
from ulab import numpy as np
from pyb import UART

uart = UART(3, 19200, timeout_char=200)
sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.

#定义一个空列表,处理数据使用。
handedata_list = [] * 4

net = None
labels = None
# 设置最小置信度阈值
min_confidence = 0.95
#加载模型和异常处理
try:
    # load the model, alloc the model file on the heap if we have at least 64K free after loading
    net = ml.Model("trained.tflite", load_to_fb=True)
except Exception as e:
    raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

try:
    labels = [line.rstrip('\n') for line in open("labels.txt")]
except Exception as e:
    raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

colors = [ # Add more colors if you are detecting more than 7 types of classes at once.
    (128,   0, 128),    #紫色
    (  0, 255,   0),    #绿色
    (255,  0 ,   0),    #红色
    (  0,   0, 255),    #蓝色
    (255, 165,   0),    #橙色
    (128,   0, 255),    #紫色
    (255, 255,   0),    #黄色
    (0  , 255, 255),    #青色
    (255, 105, 180),    #粉色
    (144, 238, 144),    #浅绿色
    (169, 169, 169),    #灰色
    (255, 255, 255),    #白色
]

# 定义置信度阈值列表,进行图像处理时使用
threshold_list = [(math.ceil(min_confidence * 255), 255)]  #返回一个整数,将 x 四舍五入
# print(threshold_list)
# time.sleep(10)
# 定义FOMO后处理函数,处理模型输出,提取检测到的对象信息
def fomo_post_process(model, inputs, outputs):
    ob, oh, ow, oc = model.output_shape[0] # 获取模型输出的形状,包含对象数量、输出高度、输出宽度和输出通道数
    # 计算输入图像与输出图像的比例
    x_scale = inputs[0].roi[2] / ow    # 输入图像宽度与输出宽度的比例
    y_scale = inputs[0].roi[3] / oh    # 输入图像高度与输出高度的比例

    scale = min(x_scale, y_scale)       # 取最小比例作为缩放因子
    # 计算输出图像的偏移量 以便将检测结果调整到输入图像的坐标系
    x_offset = ((inputs[0].roi[2] - (ow * scale)) / 2) + inputs[0].roi[0]
    y_offset = ((inputs[0].roi[3] - (ow * scale)) / 2) + inputs[0].roi[1]

    # 初始化一个列表,用于存储每个输出通道的检测结果
    l = [[] for i in range(oc)]

    for i in range(oc):
        img = image.Image(outputs[0][0, :, :, i] * 255)
        blobs = img.find_blobs(
            threshold_list, x_stride=1, y_stride=1, area_threshold=1, pixels_threshold=1
        )   #x_stride 是从左上角开始搜索blob要跳过的像素数量,为1既在x方向一个一个找
            #过滤掉边界框面积和像素小于area_threshold,pixels_threshold的blobs
            #通过灰度范围筛选可能的检测区域,后续搭配置信度来保留可信的检测结果
        for b in blobs:
            rect = b.rect()    #返回一个矩形元组 (x, y, w, h) 表示blobs的边界框
            x, y, w, h = rect
            score = (
                img.get_statistics(thresholds=threshold_list, roi=rect).l_mean() / 255.0
            ) #按照颜色划分,实际过程是置信度转化成灰度值
            x = int((x * scale) + x_offset) #下面的一系列操作均为将识别到的边界框重新放置到原图上
            y = int((y * scale) + y_offset)
            w = int(w * scale)
            h = int(h * scale)
            l[i].append((x, y, w, h, score))
    return l    #这个列表中是各种类别的坐标和置信度得分

clock = time.clock()
while(True):
    clock.tick()

    img = sensor.snapshot()
    #双循环寻找类别,寻找到哪个,就输出哪个 enumerate函数生成一个可迭代对象,比如这里列表内嵌套多个元组
    for i, detection_list in enumerate(net.predict([img], callback=fomo_post_process)):
        if i == 0: continue  # background class
        if len(detection_list) == 0: continue  # no detections for this class?

        print("********** %s **********" % labels[i]) #数字检测中这个i就是数字,小数点另行处理
        for x, y, w, h, score in detection_list:     #这个图像中,这个类别有多少个目标,这个循环就走多少次
            center_x = math.floor(x + (w / 2))
            center_y = math.floor(y + (h / 2))
            # print(f"x {center_x}\ty {center_y}\tscore {score}")
            img.draw_circle((center_x, center_y, 12), color=colors[i]) 
            # print(f"x {x}\ty {y}\tscore {score}")
            # img.draw_rectangle((x, y, w , h), color=colors[i])    #画圆改成画矩形

            # detection_list的结构是一个列表,列表内的成员是元组
            # 将x坐标提取出来以元组形式(x,labels[i])存储到列表中
            data_tulpe = (x , int(labels[i]))
            handedata_list.append(data_tulpe)
    # 循环结束后按x的大小对列表进行排序
    sorted_list = sorted(handedata_list, key=lambda x: x[0])  # 按照匹配结果的左上角坐标排序结构内部是元组
    # 排序后将第二位数据标签提出来
    result_list = [item[1] for item in sorted_list if len(item) >= 2]
    # 这个列表中的数据就是识别到的数字,将其配合协议通过串口一起发送即可
    print(result_list)
    result_list.clear()
    handedata_list.clear()
    print(clock.fps(), "fps", end="\n\n")
    # time.sleep(0.5)
请在这里粘贴代码