Skip to content

Commit db6f4a6

Browse files
Simplified GetImage and enabled esp_cli for all modes
- `GetImage` function now uses only `display_buf`, `cam_buf` points to a certain region in this buffer - `esp_cli` commands like cpu-dump, mem-dump, task-dump are now always accessible. `detect_image` is accessible only for `CLI_ONLY_INFERENCE`
1 parent 4e256f9 commit db6f4a6

File tree

6 files changed

+50
-28
lines changed

6 files changed

+50
-28
lines changed

examples/person_detection/main/app_camera_esp.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ int app_camera_init() {
4242

4343
#if (CONFIG_TFLITE_USE_BSP)
4444
bsp_i2c_init();
45-
bsp_display_start();
46-
bsp_display_backlight_on();
4745
camera_config_t config = BSP_CAMERA_DEFAULT_CONFIG;
4846

4947
#else // CONFIG_TFLITE_USE_BSP

examples/person_detection/main/detection_responder.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ static lv_obj_t *label = NULL;
3838

3939
void create_gui(void)
4040
{
41+
bsp_display_cfg_t cfg = {
42+
.lvgl_port_cfg = {
43+
.task_priority = CONFIG_BSP_DISPLAY_LVGL_TASK_PRIORITY,
44+
.task_stack = 6144,
45+
.task_affinity = 1,
46+
.task_max_sleep_ms = CONFIG_BSP_DISPLAY_LVGL_MAX_SLEEP,
47+
.timer_period_ms = CONFIG_BSP_DISPLAY_LVGL_TICK,
48+
},
49+
.buffer_size = BSP_LCD_DRAW_BUFF_SIZE,
50+
.double_buffer = BSP_LCD_DRAW_BUFF_DOUBLE,
51+
.flags = {
52+
.buff_dma = false,
53+
.buff_spiram = true,
54+
}
55+
};
56+
bsp_display_start_with_config(&cfg);
57+
bsp_display_backlight_on();
58+
4159
bsp_display_lock(0);
4260
camera_canvas = lv_canvas_create(lv_scr_act());
4361
assert(camera_canvas);

examples/person_detection/main/esp_cli.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "esp_cli.h"
2525
#include "esp_timer.h"
2626

27+
#if CLI_ONLY_INFERENCE
2728
#define IMAGE_COUNT 10
2829
static uint8_t *image_database[IMAGE_COUNT];
2930

@@ -38,6 +39,7 @@ extern const uint8_t image6_start[] asm("_binary_image6_start");
3839
extern const uint8_t image7_start[] asm("_binary_image7_start");
3940
extern const uint8_t image8_start[] asm("_binary_image8_start");
4041
extern const uint8_t image9_start[] asm("_binary_image9_start");
42+
#endif
4143

4244
static const char *TAG = "[esp_cli]";
4345

@@ -96,6 +98,7 @@ static int mem_dump_cli_handler(int argc, char *argv[])
9698
return 0;
9799
}
98100

101+
#if CLI_ONLY_INFERENCE
99102
static int inference_cli_handler(int argc, char *argv[])
100103
{
101104
/* Just to go to the next line */
@@ -121,6 +124,18 @@ static int inference_cli_handler(int argc, char *argv[])
121124
return 0;
122125
}
123126

127+
int esp_cli_register_inference_command() {
128+
esp_console_cmd_t command = {
129+
.command = "detect_image",
130+
.help = "detect_image <image_number>"
131+
"Note: image numbers ranging from 0 - 9 only are valid",
132+
.func = inference_cli_handler,
133+
};
134+
esp_console_cmd_register(&command);
135+
return 0;
136+
}
137+
#endif
138+
124139
static esp_console_cmd_t diag_cmds[] = {
125140
{
126141
.command = "mem-dump",
@@ -137,12 +152,6 @@ static esp_console_cmd_t diag_cmds[] = {
137152
.help = "",
138153
.func = cpu_dump_cli_handler,
139154
},
140-
{
141-
.command = "detect_image",
142-
.help = "detect_image <image_number>"
143-
"Note: image numbers ranging from 0 - 9 only are valid",
144-
.func = inference_cli_handler,
145-
},
146155
};
147156

148157
int esp_cli_register_cmds()
@@ -158,6 +167,7 @@ int esp_cli_register_cmds()
158167

159168
static void image_database_init()
160169
{
170+
#if CLI_ONLY_INFERENCE
161171
image_database[0] = (uint8_t *) image0_start;
162172
image_database[1] = (uint8_t *) image1_start;
163173
image_database[2] = (uint8_t *) image2_start;
@@ -168,7 +178,7 @@ static void image_database_init()
168178
image_database[7] = (uint8_t *) image7_start;
169179
image_database[8] = (uint8_t *) image8_start;
170180
image_database[9] = (uint8_t *) image9_start;
171-
181+
#endif
172182
}
173183

174184
int esp_cli_start()

examples/person_detection/main/esp_cli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern "C" {
1919
#endif
2020

2121
int esp_cli_start();
22+
int esp_cli_register_inference_command();
2223

2324
#ifdef __cplusplus
2425
}

examples/person_detection/main/image_provider.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ limitations under the License.
2929

3030
static const char* TAG = "app_camera";
3131
static uint16_t* display_buf;
32-
static uint16_t* cam_buf;
3332

3433
// Get the camera module ready
3534
TfLiteStatus InitCamera() {
@@ -48,14 +47,6 @@ TfLiteStatus InitCamera() {
4847
ESP_LOGE(TAG, "Couldn't allocate display buffer");
4948
return kTfLiteError;
5049
}
51-
52-
if (cam_buf == NULL) {
53-
cam_buf = (uint16_t *) heap_caps_malloc(96 * 96 * sizeof(uint16_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
54-
}
55-
if (display_buf == NULL) {
56-
ESP_LOGE(TAG, "Couldn't allocate camera buffer");
57-
return kTfLiteError;
58-
}
5950
#endif // DISPLAY_SUPPORT
6051

6152
#if ESP_CAMERA_SUPPORTED
@@ -90,13 +81,14 @@ TfLiteStatus GetImage(int image_width, int image_height, int channels, int8_t* i
9081
// Hence, we need to convert this data to grayscale to send it to tf model
9182
// For display we extra-polate the data to 192X192
9283

93-
memcpy(cam_buf, fb->buf, fb->len);
94-
lv_draw_sw_rgb565_swap(fb->buf, 96 * 96);
84+
// point to the last quarter of buffer
85+
uint16_t* cam_buf = display_buf + (96 * 96 * 3);
86+
memcpy((uint8_t*)cam_buf, fb->buf, fb->len);
87+
esp_camera_fb_return(fb);
9588

9689
for (int i = 0; i < kNumRows; i++) {
9790
for (int j = 0; j < kNumCols; j++) {
98-
uint16_t pixel = ((uint16_t *) (fb->buf))[i * kNumCols + j];
99-
uint16_t inference_pixel = ((uint16_t *) (cam_buf))[i * kNumCols + j];
91+
uint16_t inference_pixel = cam_buf[i * kNumCols + j];
10092

10193
// for inference
10294
uint8_t hb = inference_pixel & 0xFF;
@@ -112,7 +104,14 @@ TfLiteStatus GetImage(int image_width, int image_height, int channels, int8_t* i
112104
int8_t grey_pixel = ((305 * r + 600 * g + 119 * b) >> 10) - 128;
113105

114106
image_data[i * kNumCols + j] = grey_pixel;
107+
}
108+
}
115109

110+
// for display
111+
lv_draw_sw_rgb565_swap(cam_buf, 96 * 96);
112+
for (int i = 0; i < kNumRows; i++) {
113+
for (int j = 0; j < kNumCols; j++) {
114+
uint16_t pixel = cam_buf[i * kNumCols + j];
116115
display_buf[2 * i * kNumCols * 2 + 2 * j] = pixel;
117116
display_buf[2 * i * kNumCols * 2 + 2 * j + 1] = pixel;
118117
display_buf[(2 * i + 1) * kNumCols * 2 + 2 * j] = pixel;
@@ -127,8 +126,6 @@ TfLiteStatus GetImage(int image_width, int image_height, int channels, int8_t* i
127126
image_data[i] = ((uint8_t *) fb->buf)[i] ^ 0x80;
128127
}
129128
#endif // DISPLAY_SUPPORT
130-
131-
esp_camera_fb_return(fb);
132129
/* here the esp camera can give you grayscale image directly */
133130
return kTfLiteOk;
134131
#else

examples/person_detection/main/main.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ limitations under the License.
2020
#include "freertos/task.h"
2121

2222
#include "esp_main.h"
23-
24-
#if CLI_ONLY_INFERENCE
2523
#include "esp_cli.h"
26-
#endif
2724

2825
void tf_main(void) {
2926
setup();
30-
#if CLI_ONLY_INFERENCE
3127
esp_cli_start();
28+
#if CLI_ONLY_INFERENCE
29+
esp_cli_register_inference_command();
3230
vTaskDelay(portMAX_DELAY);
3331
#else
3432
while (true) {

0 commit comments

Comments
 (0)