This commit is contained in:
xdl 2025-02-25 18:52:11 +08:00
parent 4ece90a2a1
commit db5111ee0d
6 changed files with 3850 additions and 0 deletions

View File

@ -0,0 +1,112 @@
# generated by maixhub, tested on maixpy3 v0.4.8
# copy files to TF card and plug into board and power on
import sensor, image, lcd, time
import KPU as kpu
from machine import UART
import gc, sys
from fpioa_manager import fm
input_size = (224, 224)
labels = ['light']
anchors = [0.47, 0.47, 0.59, 0.59, 0.78, 0.81, 0.69, 0.69, 0.97, 0.94]
def lcd_show_except(e):
import uio
err_str = uio.StringIO()
sys.print_exception(e, err_str)
err_str = err_str.getvalue()
img = image.Image(size=input_size)
img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))
lcd.display(img)
class Comm:
def __init__(self, uart):
self.uart = uart
def send_detect_result(self, objects, labels):
msg = ""
for obj in objects:
pos = obj.rect()
p = obj.value()
idx = obj.classid()
label = labels[idx]
msg += "{}:{}:{}:{}:{}:{:.2f}:{}, ".format(pos[0], pos[1], pos[2], pos[3], idx, p, label)
if msg:
msg = msg[:-2] + "\n"
self.uart.write(msg.encode())
def init_uart():
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(11, fm.fpioa.UART1_RX, force=True)
uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=256)
return uart
def main(anchors, labels = None, model_addr="/sd/m.kmodel", sensor_window=input_size, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing(sensor_window)
sensor.set_hmirror(sensor_hmirror)
sensor.set_vflip(sensor_vflip)
sensor.run(1)
lcd.init(type=1)
lcd.rotation(lcd_rotation)
lcd.clear(lcd.WHITE)
if not labels:
with open('labels.txt','r') as f:
exec(f.read())
if not labels:
print("no labels.txt")
img = image.Image(size=(240, 240))
img.draw_string(90, 110, "no labels.txt", color=(255, 0, 0), scale=2)
lcd.display(img)
return 1
try:
img = image.Image("startup.jpg")
lcd.display(img)
except Exception:
img = image.Image(size=(240, 240))
img.draw_string(90, 110, "loading model...", color=(255, 255, 255), scale=2)
lcd.display(img)
uart = init_uart()
comm = Comm(uart)
try:
task = None
task = kpu.load(model_addr)
kpu.init_yolo2(task, 0.2, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1]
while(True):
img = sensor.snapshot()
t = time.ticks_ms()
objects = kpu.run_yolo2(task, img)
t = time.ticks_ms() - t
if objects:
for obj in objects:
pos = obj.rect()
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(objects, labels)
img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0))
img.draw_string(0, 2, "Upgrade to MaixCAM to use YOLOv8", scale=1.2, color=(255, 0, 0))
img.draw_string(0, 30, "wiki.sipeed.com/maixcam", scale=1.2, color=(255, 0, 0))
lcd.display(img)
except Exception as e:
raise e
finally:
if not task is None:
kpu.deinit(task)
if __name__ == "__main__":
try:
# main(anchors = anchors, labels=labels, model_addr=0x300000, lcd_rotation=0)
main(anchors = anchors, labels=labels, model_addr="/sd/model-179117.kmodel")
except Exception as e:
sys.print_exception(e)
lcd_show_except(e)
finally:
gc.collect()

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,112 @@
# generated by maixhub, tested on maixpy3 v0.4.8
# copy files to TF card and plug into board and power on
import sensor, image, lcd, time
import KPU as kpu
from machine import UART
import gc, sys
from fpioa_manager import fm
input_size = (224, 224)
labels = ['light']
anchors = [1.31, 1.34, 1.5, 1.22, 1.13, 1.19, 1.16, 1.06, 0.94, 0.94]
def lcd_show_except(e):
import uio
err_str = uio.StringIO()
sys.print_exception(e, err_str)
err_str = err_str.getvalue()
img = image.Image(size=input_size)
img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))
lcd.display(img)
class Comm:
def __init__(self, uart):
self.uart = uart
def send_detect_result(self, objects, labels):
msg = ""
for obj in objects:
pos = obj.rect()
p = obj.value()
idx = obj.classid()
label = labels[idx]
msg += "{}:{}:{}:{}:{}:{:.2f}:{}, ".format(pos[0], pos[1], pos[2], pos[3], idx, p, label)
if msg:
msg = msg[:-2] + "\n"
self.uart.write(msg.encode())
def init_uart():
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(11, fm.fpioa.UART1_RX, force=True)
uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=256)
return uart
def main(anchors, labels = None, model_addr="/sd/m.kmodel", sensor_window=input_size, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing(sensor_window)
sensor.set_hmirror(sensor_hmirror)
sensor.set_vflip(sensor_vflip)
sensor.run(1)
lcd.init(type=1)
lcd.rotation(lcd_rotation)
lcd.clear(lcd.WHITE)
if not labels:
with open('labels.txt','r') as f:
exec(f.read())
if not labels:
print("no labels.txt")
img = image.Image(size=(320, 240))
img.draw_string(90, 110, "no labels.txt", color=(255, 0, 0), scale=2)
lcd.display(img)
return 1
try:
img = image.Image("startup.jpg")
lcd.display(img)
except Exception:
img = image.Image(size=(320, 240))
img.draw_string(90, 110, "loading model...", color=(255, 255, 255), scale=2)
lcd.display(img)
uart = init_uart()
comm = Comm(uart)
try:
task = None
task = kpu.load(model_addr)
kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1]
while(True):
img = sensor.snapshot()
t = time.ticks_ms()
objects = kpu.run_yolo2(task, img)
t = time.ticks_ms() - t
if objects:
for obj in objects:
pos = obj.rect()
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(objects, labels)
img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0))
img.draw_string(0, 2, "Upgrade to MaixCAM to use YOLOv8", scale=1.2, color=(255, 0, 0))
img.draw_string(0, 30, "wiki.sipeed.com/maixcam", scale=1.2, color=(255, 0, 0))
lcd.display(img)
except Exception as e:
raise e
finally:
if not task is None:
kpu.deinit(task)
if __name__ == "__main__":
try:
# main(anchors = anchors, labels=labels, model_addr=0x300000, lcd_rotation=0)
main(anchors = anchors, labels=labels, model_addr="/sd/model-179147.kmodel")
except Exception as e:
sys.print_exception(e)
lcd_show_except(e)
finally:
gc.collect()

Binary file not shown.

View File

@ -0,0 +1,688 @@
{
"anchors": [
1.31,
1.34,
1.5,
1.22,
1.13,
1.19,
1.16,
1.06,
0.94,
0.94
],
"inputs": {
"input0": [
224,
224,
3
]
},
"outputs": {
"output0": [
7,
7,
30
]
},
"params_quantity": 1856046,
"loss": [
[
1,
0.17034
],
[
2,
0.08795
],
[
3,
0.06357
],
[
4,
0.04993
],
[
5,
0.04176
],
[
6,
0.08768
],
[
7,
0.04041
],
[
8,
0.02928
],
[
9,
0.02397
],
[
10,
0.02016
],
[
11,
0.01708
],
[
12,
0.01466
],
[
13,
0.01245
],
[
14,
0.01075
],
[
15,
0.00928
],
[
16,
0.00803
],
[
17,
0.00706
],
[
18,
0.00627
],
[
19,
0.00557
],
[
20,
0.00505
],
[
21,
0.00455
],
[
22,
0.00422
],
[
23,
0.00386
],
[
24,
0.0037
],
[
25,
0.00341
],
[
26,
0.00332
],
[
27,
0.00306
],
[
28,
0.00289
],
[
29,
0.00279
],
[
30,
0.0027
]
],
"acc": [
[
1,
0.22327
],
[
2,
0.2719
],
[
3,
0.26417
],
[
4,
0.31976
],
[
5,
0.33196
],
[
6,
0.35342
],
[
7,
0.40681
],
[
8,
0.42154
],
[
9,
0.43038
],
[
10,
0.42909
],
[
11,
0.4312
],
[
12,
0.43028
],
[
13,
0.43497
],
[
14,
0.42992
],
[
15,
0.42735
],
[
16,
0.4279
],
[
17,
0.42413
],
[
18,
0.42213
],
[
19,
0.42105
],
[
20,
0.42054
],
[
21,
0.41878
],
[
22,
0.41614
],
[
23,
0.41723
],
[
24,
0.41587
],
[
25,
0.41487
],
[
26,
0.41579
],
[
27,
0.41559
],
[
28,
0.41341
],
[
29,
0.41367
],
[
30,
0.4133
]
],
"lr": [
[
1,
0.001
],
[
2,
0.00093
],
[
3,
0.00086
],
[
4,
0.00079
],
[
5,
0.00074
],
[
6,
0.00068
],
[
7,
0.00063
],
[
8,
0.00058
],
[
9,
0.00054
],
[
10,
0.0005
],
[
11,
0.00046
],
[
12,
0.00043
],
[
13,
0.0004
],
[
14,
0.00037
],
[
15,
0.00034
],
[
16,
0.00032
],
[
17,
0.00029
],
[
18,
0.00027
],
[
19,
0.00025
],
[
20,
0.00023
],
[
21,
0.00022
],
[
22,
0.0002
],
[
23,
0.00018
],
[
24,
0.00017
],
[
25,
0.00016
],
[
26,
0.00015
],
[
27,
0.00014
],
[
28,
0.00013
],
[
29,
0.00012
],
[
30,
0.00011
]
],
"val_acc": [
[
10,
0.0
],
[
20,
0.63384
],
[
30,
0.90099
]
],
"AP_light": [
[
10,
0.0
],
[
20,
0.63384
],
[
30,
0.90099
]
],
"mean": 123.5,
"std": 58.395,
"label_type": "detection",
"data_type": "image",
"labels": [
"light"
],
"best_eval": {
"epoch": 30,
"val_acc": 0.901,
"val_info": [
{
"name": "187da55666d54decb7dee52f76f2c530.jpg",
"nickname": "hld2_140_8.jpg",
"pred": [
[
74,
30,
113,
75,
0.8115190267562866,
0.0
],
[
66,
106,
110,
157,
0.8789578080177307,
0.0
],
[
65,
193,
114,
233,
0.8545116782188416,
0.0
]
],
"gt": [
[
72,
34,
111,
75,
1.0,
0.0
],
[
64,
108,
109,
155,
1.0,
0.0
],
[
63,
192,
100,
232,
1.0,
0.0
]
],
"ok": true
},
{
"name": "93fbaa2bd1f64683b1aab71587c21cea.jpg",
"nickname": "hld2_123_20.jpg",
"pred": [
[
49,
33,
93,
83,
0.8799240589141846,
0.0
],
[
49,
103,
83,
151,
0.7734171748161316,
0.0
],
[
44,
182,
91,
217,
0.7748101949691772,
0.0
]
],
"gt": [
[
44,
30,
91,
80,
1.0,
0.0
],
[
42,
106,
90,
153,
1.0,
0.0
],
[
44,
175,
93,
215,
1.0,
0.0
]
],
"ok": true
},
{
"name": "42847f8f4bf145b4866a0f3f775a68c1.jpg",
"nickname": "hld2_130_8.jpg",
"pred": [
[
59,
41,
101,
88,
0.7852646112442017,
0.0
],
[
59,
122,
96,
164,
0.8069536685943604,
0.0
],
[
51,
199,
106,
238,
0.8835567831993103,
0.0
]
],
"gt": [
[
50,
45,
109,
92,
1.0,
0.0
],
[
50,
115,
102,
166,
1.0,
0.0
],
[
46,
198,
106,
239,
1.0,
0.0
]
],
"ok": true
},
{
"name": "8e49b309cff64b47a742253c3d7ab160.jpg",
"nickname": "hld2_122_9.jpg",
"pred": [
[
39,
62,
74,
107,
0.6478549838066101,
0.0
],
[
31,
187,
64,
214,
0.6859835982322693,
0.0
]
],
"gt": [
[
38,
64,
79,
99,
1.0,
0.0
],
[
36,
120,
70,
155,
1.0,
0.0
],
[
20,
178,
77,
218,
1.0,
0.0
]
],
"ok": false
},
{
"name": "6fb92ac2f0d647e49c76125500ea4c52.jpg",
"nickname": "hld2_110_9.jpg",
"pred": [],
"gt": [
[
42,
45,
79,
82,
1.0,
0.0
],
[
36,
106,
77,
156,
1.0,
0.0
],
[
37,
171,
71,
209,
1.0,
0.0
]
],
"ok": false
}
]
}
}