Skip to content

Commit b84e745

Browse files
committed
maixhub client now send result with maix protocol(uart by default)
1 parent 6f70f5b commit b84e745

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

examples/protocol/comm_protocol_yolov5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
def encode_objs(objs):
1818
'''
1919
encode objs info to bytes body for protocol
20-
2B x(LE) + 2B y(LE) + 2B w(LE) + 2B h(LE) + 2B idx ...
20+
2B x(LE) + 2B y(LE) + 2B w(LE) + 2B h(LE) + 2B idx + 4B score(float) ...
2121
'''
2222
body = b''
2323
for obj in objs:
24-
body += struct.pack("<hhHHH", obj.x, obj.y, obj.w, obj.h, obj.class_id)
24+
body += struct.pack("<hhHHHf", obj.x, obj.y, obj.w, obj.h, obj.class_id, obj.score)
2525
return body
2626

2727
def decode_objs(body):

projects/app_maixhub_client/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
id: maixhub
22
name: MaixHub Client
33
name[zh]: MaixHub 客户端
4-
version: 1.0.2
4+
version: 1.1.0
55
author: Sipeed
66
icon: app.png
77
desc: MaixHub client to collect image or deploy AI model etc.

projects/app_maixhub_client/maixhub/demos/classifier.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1-
from maix import nn, image
2-
1+
from maix import nn, image, comm
2+
import struct
33

44
class Demo:
55
def __init__(self, model_path):
66
self.model_path = model_path
77
self.classifier = None
8+
self.p = None
9+
self.APP_CMD_CLASSIFY_RES = 0x02
10+
11+
def encode_objs(self, res):
12+
'''
13+
encode objs info to bytes body for protocol
14+
2B max idx, 4B prob, 2B second idx, 4B prob, 2B third idx, 4B prob
15+
'''
16+
body = b''
17+
for obj in res:
18+
body += struct.pack("<Hf", obj[0], obj[1])
19+
return body
820

921
def init(self):
1022
# camera.config(size=self.input_size)
1123
print("-- load model:", self.model_path)
1224
self.classifier = nn.Classifier(model=self.model_path, dual_buff = True)
1325
print("-- load ok")
26+
self.p = comm.CommProtocol(buff_size = 1024)
1427

1528
def input_size(self):
1629
return self.classifier.input_size()
1730

1831
def loop(self, img, dis_img, key):
1932
res = self.classifier.classify(img)
33+
34+
body = self.encode_objs(res)
35+
self.p.report(self.APP_CMD_CLASSIFY_RES, body)
36+
2037
max_idx, max_prob = res[0]
2138
msg = "{:.2f}: {}".format(max_prob, self.classifier.labels[max_idx])
2239
h = image.string_size("A", scale = 2, thickness = 2)[1]

projects/app_maixhub_client/maixhub/demos/yolov5.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
from maix import nn, image
1+
from maix import nn, image, comm
2+
import struct
23

34
class Demo:
45
def __init__(self, model_path):
56
self.model_path = model_path
67
self.detector = None
8+
self.p = None
9+
self.APP_CMD_DETECT_RES = 0x02
10+
self.report_on = True
11+
12+
def encode_objs(self, objs):
13+
'''
14+
encode objs info to bytes body for protocol
15+
2B x(LE) + 2B y(LE) + 2B w(LE) + 2B h(LE) + 2B idx + 4B score(float) ...
16+
'''
17+
body = b''
18+
for obj in objs:
19+
body += struct.pack("<hhHHHf", obj.x, obj.y, obj.w, obj.h, obj.class_id, obj.score)
20+
return body
721

822
def init(self):
923
self.detector = nn.YOLOv5(model=self.model_path, dual_buff = True)
24+
self.p = comm.CommProtocol(buff_size = 1024)
1025

1126
def input_size(self):
1227
return self.detector.input_size()
1328

1429
def loop(self, img, dis_img, key):
1530
objs = self.detector.detect(img, conf_th = 0.5, iou_th = 0.45)
31+
32+
if len(objs) > 0 and self.report_on:
33+
body = self.encode_objs(objs)
34+
self.p.report(self.APP_CMD_DETECT_RES, body)
35+
1636
for obj in objs:
1737
rect = image.resize_map_pos(img.width(), img.height(), dis_img.width(), dis_img.height(), image.Fit.FIT_CONTAIN, obj.x, obj.y, obj.w, obj.h)
1838
str_h = image.string_size(self.detector.labels[obj.class_id])[1]

0 commit comments

Comments
 (0)