Skip to content

Commit a7b44b6

Browse files
major refactoring of classes and layout
1 parent 5fb8f3f commit a7b44b6

File tree

512 files changed

+24009
-1784
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

512 files changed

+24009
-1784
lines changed

README.md

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ interface to load a model and run inferences.
88

99
## Install
1010

11-
Clone this repo in you Arduino libraries folder.
11+
EloquentTinyML is available from the Arduino IDE Library Manager or
12+
you can clone this repo in you Arduino libraries folder.
1213

1314
```bash
1415
git clone https://github.com/eloquentarduino/EloquentTinyML.git
1516
```
1617

18+
**Be sure you install version 2.4.0 or newer.**
19+
1720
## Export TensorFlow Lite model
1821

1922
To run a model on your microcontroller, you should first have a model.
@@ -22,42 +25,65 @@ I suggest you use [`tinymlgen`](https://github.com/eloquentarduino/tinymlgen) to
2225
it will export your TensorFlow Lite model to a C array ready to be loaded
2326
by this library.
2427

28+
```python
29+
from tinymlgen import port
30+
31+
32+
tf_model = create_tf_network()
33+
print(port(tf_model))
34+
```
35+
2536

2637
## Use
2738

2839
```cpp
2940
#include <EloquentTinyML.h>
41+
#include <eloquent_tinyml/tensorflow.h>
42+
43+
// sine_model.h contains the array you exported from Python with xxd or tinymlgen
3044
#include "sine_model.h"
3145

32-
#define NUMBER_OF_INPUTS 1
33-
#define NUMBER_OF_OUTPUTS 1
46+
#define N_INPUTS 1
47+
#define N_OUTPUTS 1
48+
// in future projects you may need to tweak this value: it's a trial and error process
3449
#define TENSOR_ARENA_SIZE 2*1024
3550

36-
Eloquent::TinyML::TfLite<
37-
NUMBER_OF_INPUTS,
38-
NUMBER_OF_OUTPUTS,
39-
TENSOR_ARENA_SIZE> ml;
51+
Eloquent::TinyML::TensorFlow::TensorFlow<N_INPUTS, N_OUTPUTS, TENSOR_ARENA_SIZE> tf;
4052

4153

4254
void setup() {
4355
Serial.begin(115200);
44-
ml.begin(sine_model);
56+
delay(4000);
57+
tf.begin(sine_model);
58+
59+
// check if model loaded fine
60+
if (!tf.isOk()) {
61+
Serial.print("ERROR: ");
62+
Serial.println(tf.getErrorMessage());
63+
64+
while (true) delay(1000);
65+
}
4566
}
4667

4768
void loop() {
48-
float x = 3.14 * random(100) / 100;
49-
float y = sin(x);
50-
float input[1] = { x };
51-
float predicted = ml.predict(input);
52-
53-
Serial.print("sin(");
54-
Serial.print(x);
55-
Serial.print(") = ");
56-
Serial.print(y);
57-
Serial.print("\t predicted: ");
58-
Serial.println(predicted);
59-
delay(1000);
69+
for (float i = 0; i < 10; i++) {
70+
// pick x from 0 to PI
71+
float x = 3.14 * i / 10;
72+
float y = sin(x);
73+
float input[1] = { x };
74+
float predicted = tf.predict(input);
75+
76+
Serial.print("sin(");
77+
Serial.print(x);
78+
Serial.print(") = ");
79+
Serial.print(y);
80+
Serial.print("\t predicted: ");
81+
Serial.println(predicted);
82+
}
83+
84+
delay(10000);
6085
}
86+
6187
```
6288

6389
## Compatibility
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#define CAMERA_MODEL_M5STACK_WIDE
2+
#include <EloquentVision.h>
3+
4+
Eloquent::Vision::ESP32Camera camera;
5+
camera_fb_t *frame;
6+
7+
8+
9+
/**
10+
* Configure camera
11+
*/
12+
void initCamera() {
13+
camera.begin(FRAMESIZE_QVGA, PIXFORMAT_GRAYSCALE, 20000000);
14+
}
15+
16+
17+
/**
18+
* Capture frame from ESP32 camera
19+
*/
20+
uint8_t* captureFrame() {
21+
frame = camera.capture();
22+
23+
return frame->buf;
24+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <EloquentTinyML.h>
2+
#include <eloquent_tinyml/tensorflow/person_detection.h>
3+
4+
#if defined(ESP32)
5+
#include "ESP32Camera.h"
6+
#else
7+
#include "PortentaVision.h"
8+
#endif
9+
10+
const uint16_t imageWidth = 320;
11+
const uint16_t imageHeight = 240;
12+
13+
14+
Eloquent::TinyML::TensorFlow::PersonDetection<imageWidth, imageHeight> detector;
15+
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
delay(5000);
20+
initCamera();
21+
22+
// configure a threshold for "robust" person detection
23+
// if no threshold is set, "person" would be detected everytime person_score > not_person_score
24+
// even if just by 1
25+
// by trial and error, considering that scores range from 0 to 255, a threshold of 190-200
26+
// dramatically reduces the number of false positives
27+
detector.setDetectionAbsoluteThreshold(190);
28+
detector.begin();
29+
30+
// abort if an error occurred
31+
if (!detector.isOk()) {
32+
Serial.print("Setup error: ");
33+
Serial.println(detector.getErrorMessage());
34+
35+
while (true) delay(1000);
36+
}
37+
}
38+
39+
void loop() {
40+
uint8_t *frame = captureFrame();
41+
bool isPersonInFrame = detector.detectPerson(frame);
42+
43+
if (!detector.isOk()) {
44+
Serial.print("Loop error: ");
45+
Serial.println(detector.getErrorMessage());
46+
47+
delay(10000);
48+
return;
49+
}
50+
51+
Serial.print(isPersonInFrame ? "Person detected" : "No person detected");
52+
Serial.print(" (it took ");
53+
Serial.print(detector.getElapsedTime());
54+
Serial.println("ms to detect)");
55+
Serial.print("\t > Person score: ");
56+
Serial.println(detector.getPersonScore());
57+
Serial.print("\t > Not person score: ");
58+
Serial.println(detector.getNotPersonScore());
59+
delay(1000);
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "camera.h"
2+
3+
CameraClass cam;
4+
uint8_t frame[320*240];
5+
6+
7+
/**
8+
* Configure camera
9+
*/
10+
void initCamera() {
11+
cam.begin(CAMERA_R320x240, 30);
12+
}
13+
14+
15+
/**
16+
* Capture frame from Vision shield
17+
*/
18+
uint8_t* captureFrame() {
19+
cam.grab(frame);
20+
21+
return frame;
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <EloquentTinyML.h>
2+
#include <eloquent_tinyml/tensorflow.h>
3+
4+
// sine_model.h contains the array you exported from Python with xxd or tinymlgen
5+
#include "sine_model.h"
6+
7+
#define N_INPUTS 1
8+
#define N_OUTPUTS 1
9+
// in future projects you may need to tweak this value: it's a trial and error process
10+
#define TENSOR_ARENA_SIZE 2*1024
11+
12+
Eloquent::TinyML::TensorFlow::TensorFlow<N_INPUTS, N_OUTPUTS, TENSOR_ARENA_SIZE> tf;
13+
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
delay(4000);
18+
tf.begin(sine_model);
19+
20+
// check if model loaded fine
21+
if (!tf.isOk()) {
22+
Serial.print("ERROR: ");
23+
Serial.println(tf.getErrorMessage());
24+
25+
while (true) delay(1000);
26+
}
27+
}
28+
29+
void loop() {
30+
for (float i = 0; i < 10; i++) {
31+
// pick x from 0 to PI
32+
float x = 3.14 * i / 10;
33+
float y = sin(x);
34+
float input[1] = { x };
35+
float predicted = tf.predict(input);
36+
37+
Serial.print("sin(");
38+
Serial.print(x);
39+
Serial.print(") = ");
40+
Serial.print(y);
41+
Serial.print("\t predicted: ");
42+
Serial.println(predicted);
43+
}
44+
45+
delay(1000);
46+
}

0 commit comments

Comments
 (0)