Skip to content

Commit cb67d84

Browse files
committed
feat: add fetch classes demo
1 parent d767864 commit cb67d84

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include <Seeed_Arduino_SSCMA.h>
2+
3+
SSCMA AI;
4+
JsonDocument info;
5+
6+
7+
// Helper function to decode a Base64 character
8+
inline unsigned char decode_base64_char(char c) {
9+
if (c >= 'A' && c <= 'Z') return c - 'A';
10+
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
11+
if (c >= '0' && c <= '9') return c - '0' + 52;
12+
if (c == '+') return 62;
13+
if (c == '/') return 63;
14+
return -1;
15+
}
16+
17+
// Decode Base64 string
18+
String base64_decode(const String& input) {
19+
// Check if the input length is valid
20+
if (input.length() % 4 != 0) {
21+
throw std::invalid_argument("Invalid Base64 input length");
22+
}
23+
24+
String output;
25+
std::vector<char> temp(3);
26+
27+
size_t padding = 0;
28+
if (input.length() >= 2) {
29+
if (input[input.length() - 1] == '=') padding++;
30+
if (input[input.length() - 2] == '=') padding++;
31+
}
32+
33+
for (size_t i = 0; i < input.length(); i += 4) {
34+
unsigned char b1 = decode_base64_char(input[i]);
35+
unsigned char b2 = decode_base64_char(input[i + 1]);
36+
unsigned char b3 = input[i + 2] == '=' ? 0 : decode_base64_char(input[i + 2]);
37+
unsigned char b4 = input[i + 3] == '=' ? 0 : decode_base64_char(input[i + 3]);
38+
39+
temp[0] = (b1 << 2) | (b2 >> 4);
40+
temp[1] = ((b2 & 0x0F) << 4) | (b3 >> 2);
41+
temp[2] = ((b3 & 0x03) << 6) | b4;
42+
43+
output += temp[0];
44+
if (input[i + 2] != '=') output += temp[1];
45+
if (input[i + 3] != '=') output += temp[2];
46+
}
47+
48+
return output;
49+
}
50+
51+
52+
void setup()
53+
{
54+
AI.begin();
55+
Serial.begin(9600);
56+
57+
DeserializationError error = deserializeJson(info, base64_decode(AI.info()));
58+
59+
if(error){
60+
Serial.println("Can not fetch Model Information");
61+
while(1){
62+
delay(100);
63+
}
64+
}
65+
66+
67+
}
68+
69+
void loop()
70+
{
71+
if (!AI.invoke())
72+
{
73+
Serial.println("invoke success");
74+
Serial.print("perf: prepocess=");
75+
Serial.print(AI.perf().prepocess);
76+
Serial.print(", inference=");
77+
Serial.print(AI.perf().inference);
78+
Serial.print(", postpocess=");
79+
Serial.println(AI.perf().postprocess);
80+
81+
for (int i = 0; i < AI.boxes().size(); i++)
82+
{
83+
Serial.print("Box[");
84+
Serial.print(i);
85+
Serial.print("] target=");
86+
Serial.print(info["classes"][AI.boxes()[i].target].as<String>());
87+
Serial.print(", score=");
88+
Serial.print(AI.boxes()[i].score);
89+
Serial.print(", x=");
90+
Serial.print(AI.boxes()[i].x);
91+
Serial.print(", y=");
92+
Serial.print(AI.boxes()[i].y);
93+
Serial.print(", w=");
94+
Serial.print(AI.boxes()[i].w);
95+
Serial.print(", h=");
96+
Serial.println(AI.boxes()[i].h);
97+
}
98+
for (int i = 0; i < AI.classes().size(); i++)
99+
{
100+
Serial.print("Class[");
101+
Serial.print(i);
102+
Serial.print("] target=");
103+
Serial.print(info["classes"][AI.classes()[i].target].as<String>());
104+
Serial.print(", score=");
105+
Serial.println(AI.classes()[i].score);
106+
}
107+
for (int i = 0; i < AI.points().size(); i++)
108+
{
109+
Serial.print("Point[");
110+
Serial.print(i);
111+
Serial.print("]: target=");
112+
Serial.print(info["classes"][AI.points()[i].target].as<String>());
113+
Serial.print(", score=");
114+
Serial.print(AI.points()[i].score);
115+
Serial.print(", x=");
116+
Serial.print(AI.points()[i].x);
117+
Serial.print(", y=");
118+
Serial.println(AI.points()[i].y);
119+
}
120+
for (int i = 0; i < AI.keypoints().size(); i++)
121+
{
122+
Serial.print("keypoint[");
123+
Serial.print(i);
124+
Serial.print("] target=");
125+
Serial.print(info["classes"][AI.keypoints()[i].box.target].as<String>());
126+
Serial.print(", score=");
127+
Serial.print(AI.keypoints()[i].box.score);
128+
Serial.print(", box:[x=");
129+
Serial.print(AI.keypoints()[i].box.x);
130+
Serial.print(", y=");
131+
Serial.print(AI.keypoints()[i].box.y);
132+
Serial.print(", w=");
133+
Serial.print(AI.keypoints()[i].box.w);
134+
Serial.print(", h=");
135+
Serial.print(AI.keypoints()[i].box.h);
136+
Serial.print("], points:[");
137+
for (int j = 0; j < AI.keypoints()[i].points.size(); j++)
138+
{
139+
Serial.print("[");
140+
Serial.print(AI.keypoints()[i].points[j].x);
141+
Serial.print(",");
142+
Serial.print(AI.keypoints()[i].points[j].y);
143+
Serial.print("],");
144+
}
145+
Serial.println("]");
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)