JeVois Tutorials  1.22
JeVois Smart Embedded Machine Vision Tutorials
 
Share this page:
Loading...
Searching...
No Matches
arduyolo.C
Go to the documentation of this file.
1// JeVois + Arduino blink for X from YOLO
2
3// Pin for LED, will turn on as we detect the desired object:
4#define LEDPIN 17
5
6// Serial port to use: on chips with USB (e.g., 32u4), that usually is Serial1.
7// On chips without USB, use Serial:
8#define SERIAL Serial1
9
10// Buffer for received serial port bytes:
11#define INLEN 256
12char instr[INLEN + 1];
13
14// Our desired object: should be one of the 1000 ImageNet category names
15#define CATEGORY "dog"
16
17// Our desired minimum object width in standardized coordinates:
18#define MIN_WIDTH 200.0F
19
20void setup()
21{
22 SERIAL.begin(115200);
23 SERIAL.setTimeout(500);
24
25 pinMode(LEDPIN, OUTPUT);
26 digitalWrite(LEDPIN, HIGH);
27}
28
29void loop()
30{
31 byte len = SERIAL.readBytesUntil('\n', instr, INLEN);
32 instr[len] = 0;
33
34 char * tok = strtok(instr, " \r\n");
35 int state = 0, i; float score, left, top, width, height;
36
37 while (tok)
38 {
39 // State machine:
40 // 0: start parsing; if we get N2, move to state 1, otherwise state 1000
41 // 1: decode category name; if it is the one we want, move to state 2, otherwise state 1000
42 // 2: decode left and move to state 3
43 // 3: decode top and move to state 4
44 // 4: decode width and move to state 5
45 // 5: decode height and move to state 6
46 // 6: we got a full message, stay in this state until we run out of tokens
47 // 1000: we stay in this state until we run out of tokens
48 switch (state)
49 {
50 // First token should be: N2
51 case 0:
52 if (strcmp(tok, "N2") == 0) state = 1; else state = 1000;
53 // We are done with this token. Break from the switch() statement
54 break;
55
56 // Second token should be: category:score
57 case 1:
58 // Find the ':' between category and score:
59 i = strlen(tok) - 1;
60 while (i >= 0 && tok[i] != ':') --i;
61
62 // If i is >= 0, we found a ':'; terminate the tok string at that ':':
63 if (i >= 0)
64 {
65 tok[i] = '\0';
66 score = atof(&tok[i+1]);
67 }
68
69 // Is the category name what we want?
70 if (strcmp(tok, CATEGORY) == 0) state = 2; else state = 1000;
71
72 // We are done with this token. Break from the switch() statement
73 break;
74
75 // Third token: left
76 case 2:
77 left = atof(tok);
78 state = 3;
79 break;
80
81 // Fourth token: top
82 case 3:
83 top = atof(tok);
84 state = 4;
85 break;
86
87 // Fifth token: width
88 case 4:
89 width = atof(tok);
90 state = 5;
91 break;
92
93 // Sixth token: height
94 case 5:
95 height = atof(tok);
96 state = 6;
97 // We got a whole message!
98 break;
99
100 // In any other state: do nothing
101 default:
102 break;
103 }
104
105 // Move to the next token:
106 tok = strtok(0, " \r\n");
107 }
108
109 // If we are in state 6, we successfully parsed a whole message and the category is a match.
110 // We just need to test the width and activate the LED accordingly:
111 if (state == 6 && width >= MIN_WIDTH)
112 digitalWrite(LEDPIN, LOW); // turn LED on (it has inverted logic)
113 else
114 digitalWrite(LEDPIN, HIGH); // turn LED off
115}
#define LEDPIN
Definition arduyolo.C:4
void setup()
Definition arduyolo.C:20
#define INLEN
Definition arduyolo.C:11
#define CATEGORY
Definition arduyolo.C:15
#define SERIAL
Definition arduyolo.C:8
char instr[INLEN+1]
Definition arduyolo.C:12
#define MIN_WIDTH
Definition arduyolo.C:18
void loop()
Definition arduyolo.C:29