Skip to content

Commit 7e6a7c7

Browse files
Added Support for Person Detection for ESP32
1 parent 99c28c5 commit 7e6a7c7

File tree

9 files changed

+763
-0
lines changed

9 files changed

+763
-0
lines changed

tensorflow/lite/micro/examples/person_detection/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,75 @@ From the log, we can see that it took around 170 ms to capture and read the
171171
image data from the camera module, 180 ms to decode the JPEG and convert it to
172172
greyscale, and 18.6 seconds to run inference.
173173

174+
175+
## Running on ESP32
176+
177+
The following instructions will help you build and deploy this sample
178+
to [ESP32](https://www.espressif.com/en/products/hardware/esp32/overview)
179+
devices using the [ESP IDF](https://github.com/espressif/esp-idf).
180+
181+
The sample has been tested on ESP-IDF version 4.0 with the following devices:
182+
- [ESP32-DevKitC](http://esp-idf.readthedocs.io/en/latest/get-started/get-started-devkitc.html)
183+
- [ESP-EYE](https://github.com/espressif/esp-who/blob/master/docs/en/get-started/ESP-EYE_Getting_Started_Guide.md)
184+
185+
ESP-EYE is a board which has a built-in camera which can be used to run this example , if you want to use other esp boards you will have to connect camera externally and write your own +image_provider.cc+ and +esp_app_camera.cc+.
186+
### Install the ESP IDF
187+
188+
Follow the instructions of the
189+
[ESP-IDF get started guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html)
190+
to setup the toolchain and the ESP-IDF itself.
191+
192+
The next steps assume that the
193+
[IDF environment variables are set](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html#step-4-set-up-the-environment-variables) :
194+
195+
* The `IDF_PATH` environment variable is set
196+
* `idf.py` and Xtensa-esp32 tools (e.g. `xtensa-esp32-elf-gcc`) are in `$PATH`
197+
* `esp32-camera` should be downloaded in `comopnents/` dir of example as explained in `Building the example`(below)
198+
199+
### Generate the examples
200+
The example project can be generated with the following command:
201+
```
202+
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=esp generate_person_detection_esp_project
203+
```
204+
205+
### Building the example
206+
207+
Go the the example project directory
208+
```
209+
cd tensorflow/lite/micro/tools/make/gen/esp_xtensa-esp32/prj/person_detection/esp-idf
210+
```
211+
212+
As the `person_detection` example requires an external component `esp32-camera` for functioning hence we will have to manually clone it in `components/` directory of the example with following command.
213+
```
214+
git clone https://github.com/espressif/esp32-camera.git components/esp32-camera
215+
```
216+
217+
Then build with `idf.py`
218+
```
219+
idf.py build
220+
```
221+
222+
### Load and run the example
223+
224+
225+
226+
To flash (replace `/dev/ttyUSB0` with the device serial port):
227+
```
228+
idf.py --port /dev/ttyUSB0 flash
229+
```
230+
231+
Monitor the serial output:
232+
```
233+
idf.py --port /dev/ttyUSB0 monitor
234+
```
235+
236+
Use `Ctrl+]` to exit.
237+
238+
The previous two commands can be combined:
239+
```
240+
idf.py --port /dev/ttyUSB0 flash monitor
241+
```
242+
174243
## Running on SparkFun Edge
175244

176245
The following instructions will help you build and deploy this sample on the
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
menu "Camera Pins"
2+
choice CAMERA_MODEL
3+
bool "Select Camera Pinout"
4+
default CAMERA_MODEL_WROVER_KIT
5+
help
6+
Select Camera Pinout.
7+
8+
config CAMERA_MODEL_WROVER_KIT
9+
bool "WROVER-KIT With OV2640 Module"
10+
config CAMERA_MODEL_ESP_EYE
11+
bool "ESP_EYE DevKit"
12+
config CAMERA_MODEL_M5STACK_PSRAM
13+
bool "M5Stack Camera With PSRAM"
14+
config CAMERA_MODEL_M5STACK_WIDE
15+
bool "M5Stack Camera F (Wide)"
16+
config CAMERA_MODEL_AI_THINKER
17+
bool "ESP32-CAM by AI-Thinker"
18+
config CAMERA_MODEL_CUSTOM
19+
bool "Custom Camera Pinout"
20+
endchoice
21+
22+
config CAMERA_PIN_PWDN
23+
depends on CAMERA_MODEL_CUSTOM
24+
int "Power Down pin"
25+
range -1 33
26+
default -1
27+
help
28+
Select Power Down pin or -1 for unmanaged.
29+
30+
config CAMERA_PIN_RESET
31+
depends on CAMERA_MODEL_CUSTOM
32+
int "Reset pin"
33+
range -1 33
34+
default -1
35+
help
36+
Select Camera Reset pin or -1 for software reset.
37+
38+
config CAMERA_PIN_XCLK
39+
depends on CAMERA_MODEL_CUSTOM
40+
int "XCLK pin"
41+
range 0 33
42+
default 21
43+
help
44+
Select Camera XCLK pin.
45+
46+
config CAMERA_PIN_SIOD
47+
depends on CAMERA_MODEL_CUSTOM
48+
int "SIOD pin"
49+
range 0 33
50+
default 26
51+
help
52+
Select Camera SIOD pin.
53+
54+
config CAMERA_PIN_SIOC
55+
depends on CAMERA_MODEL_CUSTOM
56+
int "SIOC pin"
57+
range 0 33
58+
default 27
59+
help
60+
Select Camera SIOC pin.
61+
62+
config CAMERA_PIN_VSYNC
63+
depends on CAMERA_MODEL_CUSTOM
64+
int "VSYNC pin"
65+
range 0 39
66+
default 25
67+
help
68+
Select Camera VSYNC pin.
69+
70+
config CAMERA_PIN_HREF
71+
depends on CAMERA_MODEL_CUSTOM
72+
int "HREF pin"
73+
range 0 39
74+
default 23
75+
help
76+
Select Camera HREF pin.
77+
78+
config CAMERA_PIN_PCLK
79+
depends on CAMERA_MODEL_CUSTOM
80+
int "PCLK pin"
81+
range 0 39
82+
default 25
83+
help
84+
Select Camera PCLK pin.
85+
86+
config CAMERA_PIN_Y2
87+
depends on CAMERA_MODEL_CUSTOM
88+
int "Y2 pin"
89+
range 0 39
90+
default 4
91+
help
92+
Select Camera Y2 pin.
93+
94+
config CAMERA_PIN_Y3
95+
depends on CAMERA_MODEL_CUSTOM
96+
int "Y3 pin"
97+
range 0 39
98+
default 5
99+
help
100+
Select Camera Y3 pin.
101+
102+
config CAMERA_PIN_Y4
103+
depends on CAMERA_MODEL_CUSTOM
104+
int "Y4 pin"
105+
range 0 39
106+
default 18
107+
help
108+
Select Camera Y4 pin.
109+
110+
config CAMERA_PIN_Y5
111+
depends on CAMERA_MODEL_CUSTOM
112+
int "Y5 pin"
113+
range 0 39
114+
default 19
115+
help
116+
Select Camera Y5 pin.
117+
118+
config CAMERA_PIN_Y6
119+
depends on CAMERA_MODEL_CUSTOM
120+
int "Y6 pin"
121+
range 0 39
122+
default 36
123+
help
124+
Select Camera Y6 pin.
125+
126+
config CAMERA_PIN_Y7
127+
depends on CAMERA_MODEL_CUSTOM
128+
int "Y7 pin"
129+
range 0 39
130+
default 39
131+
help
132+
Select Camera Y7 pin.
133+
134+
config CAMERA_PIN_Y8
135+
depends on CAMERA_MODEL_CUSTOM
136+
int "Y8 pin"
137+
range 0 39
138+
default 34
139+
help
140+
Select Camera Y8 pin.
141+
142+
config CAMERA_PIN_Y9
143+
depends on CAMERA_MODEL_CUSTOM
144+
int "Y9 pin"
145+
range 0 39
146+
default 35
147+
help
148+
Select Camera Y9 pin.
149+
150+
endmenu
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Adding some esp specific files in the main CMakeLists.txt
2+
ESP_PERSON_DETECTION_SRCS := \
3+
tensorflow/lite/micro/examples/person_detection/esp/app_camera_esp.c
4+
5+
ESP_PERSON_DETECTION_HDRS := \
6+
tensorflow/lite/micro/examples/person_detection/esp/app_camera_esp.h
7+
8+
CCFLAGS += -Wno-nonnull
9+
CXXFLAGS += -Wno-return-type -Wno-strict-aliasing
10+
person_detection_SRCS += $(ESP_PERSON_DETECTION_SRCS)
11+
person_detection_HDRS += $(ESP_PERSON_DETECTION_HDRS)
12+
MAIN_SRCS += $(ESP_PERSON_DETECTION_SRCS)
13+
GRAYSCALE_SRCS := tensorflow/lite/micro/tools/make/downloads/person_model_grayscale/person_detect_model_data.cc
14+
person_detection_SRCS := $(filter-out $(GRAYSCALE_SRCS), $(person_detection_SRCS))
15+
MICROLITE_CC_SRCS += $(GRAYSCALE_SRCS)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# TensorFlow Lite Micro ESP-IDF Project
2+
3+
This folder has been autogenerated by TensorFlow, and contains source, header,
4+
and project files needed to build a single TensorFlow Lite Micro target using
5+
Espressif's [ESP-IDF](https://docs.espressif.com/projects/esp-idf/en/latest/).
6+
7+
## Usage
8+
9+
### Install the ESP IDF
10+
11+
Follow the instructions of the
12+
[ESP-IDF get started guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html)
13+
to setup the toolchain and the ESP-IDF itself.
14+
15+
The next steps assume that the
16+
[IDF environment variables are set](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html#step-4-set-up-the-environment-variables) :
17+
* The `IDF_PATH` environment variable is set.
18+
* `idf.py` and Xtensa-esp32 tools (e.g., `xtensa-esp32-elf-gcc`) are in `$PATH`.
19+
* `esp32-camera` should be downloaded in `comopnents/` dir of example as explained in `Build the example`(below)
20+
21+
## Build the example
22+
23+
As the `person_detection` example requires an external component `esp32-camera` for functioning hence we will have to manually clone it in `components/` directory of the example with following command.
24+
25+
```
26+
git clone https://github.com/espressif/esp32-camera.git components/esp32-camera
27+
```
28+
29+
To build this, run:
30+
31+
```
32+
idf.py build
33+
```
34+
35+
### Load and run the example
36+
37+
To flash (replace `/dev/ttyUSB0` with the device serial port):
38+
```
39+
idf.py --port /dev/ttyUSB0 flash
40+
```
41+
42+
Monitor the serial output:
43+
```
44+
idf.py --port /dev/ttyUSB0 monitor
45+
```
46+
47+
Use `Ctrl+]` to exit.
48+
49+
The previous two commands can be combined:
50+
```
51+
idf.py --port /dev/ttyUSB0 flash monitor
52+
```
53+
54+
## Project Generation
55+
56+
See
57+
[tensorflow/lite/micro](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro)
58+
for details on how projects like this can be generated from the main source
59+
tree.
60+
61+
## License
62+
63+
TensorFlow's code is covered by the Apache2 License included in the repository,
64+
and third party dependencies are covered by their respective licenses, in the
65+
third_party folder of this package.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* ESPRESSIF MIT License
2+
*
3+
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
4+
*
5+
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
6+
* it is free of charge, to any person obtaining a copy of this software and associated
7+
* documentation files (the "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
10+
* to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include "app_camera_esp.h"
24+
25+
static const char *TAG = "app_camera";
26+
27+
int app_camera_init()
28+
{
29+
#if CONFIG_CAMERA_MODEL_ESP_EYE
30+
/* IO13, IO14 is designed for JTAG by default,
31+
* to use it as generalized input,
32+
* firstly declair it as pullup input */
33+
gpio_config_t conf;
34+
conf.mode = GPIO_MODE_INPUT;
35+
conf.pull_up_en = GPIO_PULLUP_ENABLE;
36+
conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
37+
conf.intr_type = GPIO_INTR_DISABLE;
38+
conf.pin_bit_mask = 1LL << 13;
39+
gpio_config(&conf);
40+
conf.pin_bit_mask = 1LL << 14;
41+
gpio_config(&conf);
42+
#endif
43+
camera_config_t config;
44+
config.ledc_channel = LEDC_CHANNEL_0;
45+
config.ledc_timer = LEDC_TIMER_0;
46+
config.pin_d0 = Y2_GPIO_NUM;
47+
config.pin_d1 = Y3_GPIO_NUM;
48+
config.pin_d2 = Y4_GPIO_NUM;
49+
config.pin_d3 = Y5_GPIO_NUM;
50+
config.pin_d4 = Y6_GPIO_NUM;
51+
config.pin_d5 = Y7_GPIO_NUM;
52+
config.pin_d6 = Y8_GPIO_NUM;
53+
config.pin_d7 = Y9_GPIO_NUM;
54+
config.pin_xclk = XCLK_GPIO_NUM;
55+
config.pin_pclk = PCLK_GPIO_NUM;
56+
config.pin_vsync = VSYNC_GPIO_NUM;
57+
config.pin_href = HREF_GPIO_NUM;
58+
config.pin_sscb_sda = SIOD_GPIO_NUM;
59+
config.pin_sscb_scl = SIOC_GPIO_NUM;
60+
config.pin_pwdn = PWDN_GPIO_NUM;
61+
config.pin_reset = -1;//RESET_GPIO_NUM;
62+
config.xclk_freq_hz = XCLK_FREQ;
63+
config.pixel_format = CAMERA_PIXEL_FORMAT;
64+
config.frame_size = CAMERA_FRAME_SIZE;
65+
config.jpeg_quality = 10;
66+
config.fb_count = 1;
67+
68+
// camera init
69+
esp_err_t err = esp_camera_init(&config);
70+
if (err != ESP_OK) {
71+
ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
72+
return -1;
73+
}
74+
return 0;
75+
}

0 commit comments

Comments
 (0)