Embedded_game/002_B_Car/使用模型3.py
2025-01-02 12:48:11 +08:00

56 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Untitled - By: nian0 - 周一 11月 20 2023
import sensor, image, lcd, time
from fpioa_manager import fm
import KPU as kpu
import gc
from Maix import GPIO
from machine import Timer, PWM, UART, Timer
# 初始化 LCD
lcd.init()
# 从SD卡加载模型
# task = kpu.load("/sd/model-96249.kmodel")
labels = ['green', 'light', 'yellow', 'red']
anchors = [0.88, 3.47, 0.69, 0.81, 0.97, 3.47, 1.12, 3.75, 1.25, 4.19]
while True:
task = None
task = kpu.load("/sd/model-96249.kmodel")
# 初始化yolo2网络识别可信概率为0.770%
a = kpu.init_yolo2(task, 0.7, 0.3, 5, anchors)
img = image.load('./sd/yellow.jpg', [size = (240, 240), [mode = "RGB"]])
# 调整图像大小
img = img.resize(224, 224)
# # 在LCD屏幕上显示图像
# lcd.display(img)
# 准备图像进行推理
img.pix_to_ai()
code = kpu.run_yolo2(task, img) # 运行yolo2网络
print(code)
if code:
for obj in code:
pos = obj.rect()
print(labels[obj.classid()])
img.draw_rectangle(pos)
img.draw_string(pos[0], pos[1], "%s : %.2f" %(labels[obj.classid()], obj.value()), scale=2, color=(255, 0, 0))
#comm.send_detect_result(code, classes)
#img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0))
a = lcd.display(img)
else:
a = lcd.display(img)
if not task is None:
kpu.deinit(task)