有没有直接在模型中获得roi的方法呀
6
6pzk
@6pzk
0
Reputation
4
Posts
73
Profile views
0
Followers
0
Following
Posts made by 6pzk
-
我现根据数字边缘模型实现数字识别,保存了百张数字边缘图进行模板匹配,然后根据roi在灰度图像上画框,运行太卡
import sensor, image, time, ml, uos, gc from image import SEARCH_EX, SEARCH_DS # 初始化传感器 sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) # 使用灰度图像减少内存占用 sensor.set_framesize(sensor.QQVGA) # 设置为 QQVGA(160x120)进一步减少内存占用 sensor.skip_frames(time=2000) # 等待传感器稳定 # 加载模型和标签 net = None labels = None templates_folder = "/number1" #保存多个模板 try: # 加载模型文件 net = ml.Model("ei-edge_num_identify1-openmv-v3/trained.tflite", load_to_fb=uos.stat('ei-edge_num_identify1-openmv-v3/trained.tflite')[6] > (gc.mem_free() - (64*1024))) except Exception as e: raise Exception('模型加载失败: ' + str(e)) try: :face_with_tears_of_joy: # 加载标签文件 labels = [line.rstrip('\n') for line in open("ei-edge_num_identify1-openmv-v3/labels.txt")] except Exception as e: raise Exception('标签加载失败: ' + str(e)) try: templates = [f for f in uos.listdir(templates_folder) if f.endswith('.pgm')] except Exception as e: raise Exception("无法加载模板文件夹: " + str(e)) clock = time.clock() while True: clock.tick() # 捕获灰度图像 gray_img = sensor.snapshot() # 创建边缘图像作为备份 # try: edge_img = gray_img.copy() # 备份原图像 edge_img = edge_img.find_edges(image.EDGE_CANNY, threshold=(50, 80)) # 应用边缘检测 for t in templates: template_path = templates_folder + "/" + t template = image.Image(template_path) #对每个模板遍历进行模板匹配 r = edge_img.find_template(template, 0.70, step=4, search=SEARCH_EX) if r: gray_img.draw_rectangle(r) # except MemoryError: # print("内存不足,跳过此帧") # continue # 使用模型预测 predictions_list = list(zip(labels, net.predict([gray_img])[0].flatten().tolist())) max_prediction = max(predictions_list, key=lambda x: x[1]) detected_label = max_prediction[0] # 获取预测结果 print("识别结果: %s (置信度: %f)" % (detected_label, max_prediction[1])) # 检测 ROI 区域并绘制矩形框 # for blob in edge_img.find_blobs([(0, 255)], pixels_threshold=50, area_threshold=50, merge=True): # roi = blob.rect() # 获取 ROI 矩形 # gray_img.draw_rectangle(roi, color=255) # 在灰度图像上绘制矩形框 # gray_img.draw_string(roi[0], roi[1] - 10, detected_label, color=255, scale=1) # 绘制标签 # 显示灰度图像(包含矩形框和标签) print(clock.fps(), "fps") # 打印帧率