Skip to content

Commit 0424824

Browse files
Fixed broken person_detection example
- Bumped `esp32_s3_eye` dependency version number - Made changes to CMakeLists.txt to fix BSP packages not getting installed - Fixed issues with LVGL which caused the display to fail
1 parent 2312aff commit 0424824

File tree

7 files changed

+63
-55
lines changed

7 files changed

+63
-55
lines changed

examples/person_detection/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ cmake_minimum_required(VERSION 3.5)
33
set(EXTRA_COMPONENT_DIRS static_images)
44
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
55

6-
function(add_bsp SDKCONFIG BSP TARGET)
6+
function(add_bsp SDKCONFIG BSP)
77
string(REGEX MATCH "CONFIG_${BSP}=y" REGEX_RESULT ${SDKCONFIG})
88
if (REGEX_RESULT)
9-
set(ENV{${BSP}} "${TARGET}")
9+
set(ENV{${BSP}} 1)
1010
endif()
1111
endfunction()
1212

1313
# 1. Define all variables used in main/idf_component.yml
14-
set(ENV{TFLITE_USE_BSP_S3_EYE} "false")
15-
set(ENV{TFLITE_USE_BSP_KORVO_2} "false")
16-
set(ENV{TFLITE_USE_BSP_KALUGA} "false")
14+
set(ENV{TFLITE_USE_BSP_S3_EYE} 0)
15+
set(ENV{TFLITE_USE_BSP_KORVO_2} 0)
16+
set(ENV{TFLITE_USE_BSP_KALUGA} 0)
1717

1818
# 2. Set correct var to 'target'
1919
# This is a workaround idf-component-manager limitation, where only
2020
# target and idf_version can be in the if-clause
2121
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/sdkconfig)
2222
file(READ ${CMAKE_CURRENT_LIST_DIR}/sdkconfig SDKCONFIG_RULE)
2323

24-
add_bsp("${SDKCONFIG_RULE}" "TFLITE_USE_BSP_S3_EYE" "esp32s3")
25-
add_bsp("${SDKCONFIG_RULE}" "TFLITE_USE_BSP_KORVO_2" "esp32s3")
26-
add_bsp("${SDKCONFIG_RULE}" "TFLITE_USE_BSP_KALUGA" "esp32s2")
24+
add_bsp("${SDKCONFIG_RULE}" "TFLITE_USE_BSP_S3_EYE")
25+
add_bsp("${SDKCONFIG_RULE}" "TFLITE_USE_BSP_KORVO_2")
26+
add_bsp("${SDKCONFIG_RULE}" "TFLITE_USE_BSP_KALUGA")
2727
endif()
2828

2929
project(person_detection)

examples/person_detection/main/app_camera_esp.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
==============================================================================*/
1515

1616
#include "app_camera_esp.h"
17+
#include "esp_log.h"
1718
#include "sdkconfig.h"
1819

1920
#if (CONFIG_TFLITE_USE_BSP)
@@ -41,6 +42,8 @@ int app_camera_init() {
4142

4243
#if (CONFIG_TFLITE_USE_BSP)
4344
bsp_i2c_init();
45+
bsp_display_start();
46+
bsp_display_backlight_on();
4447
camera_config_t config = BSP_CAMERA_DEFAULT_CONFIG;
4548

4649
#else // CONFIG_TFLITE_USE_BSP
@@ -95,4 +98,4 @@ int app_camera_init() {
9598
ESP_LOGE(TAG, "Camera is not supported for this device!");
9699
return -1;
97100
#endif // ESP_CAMERA_SUPPORTED
98-
}
101+
}

examples/person_detection/main/detection_responder.cc

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ static lv_obj_t *camera_canvas = NULL;
3636
static lv_obj_t *person_indicator = NULL;
3737
static lv_obj_t *label = NULL;
3838

39-
static void create_gui(void)
39+
void create_gui(void)
4040
{
41-
bsp_display_start();
42-
bsp_display_backlight_on(); // Set display brightness to 100%
4341
bsp_display_lock(0);
4442
camera_canvas = lv_canvas_create(lv_scr_act());
4543
assert(camera_canvas);
@@ -62,21 +60,22 @@ void RespondToDetection(float person_score, float no_person_score) {
6260
int person_score_int = (person_score) * 100 + 0.5;
6361
(void) no_person_score; // unused
6462
#if DISPLAY_SUPPORT
65-
if (!camera_canvas) {
66-
create_gui();
67-
}
63+
if (!camera_canvas) {
64+
create_gui();
65+
}
6866

69-
uint16_t *buf = (uint16_t *) image_provider_get_display_buf();
67+
uint16_t *buf = (uint16_t *) image_provider_get_display_buf();
7068

71-
bsp_display_lock(0);
72-
if (person_score_int < 60) { // treat score less than 60% as no person
73-
lv_led_off(person_indicator);
74-
} else {
75-
lv_led_on(person_indicator);
76-
}
77-
lv_canvas_set_buffer(camera_canvas, buf, IMG_WD, IMG_HT, LV_IMG_CF_TRUE_COLOR);
78-
bsp_display_unlock();
69+
bsp_display_lock(0);
70+
if (person_score_int < 60) { // treat score less than 60% as no person
71+
lv_led_off(person_indicator);
72+
} else {
73+
lv_led_on(person_indicator);
74+
}
75+
76+
lv_canvas_set_buffer(camera_canvas, buf, IMG_WD, IMG_HT, LV_COLOR_FORMAT_RGB565);
77+
bsp_display_unlock();
7978
#endif // DISPLAY_SUPPORT
8079
MicroPrintf("person score:%d%%, no person score %d%%",
8180
person_score_int, 100 - person_score_int);
82-
}
81+
}

examples/person_detection/main/detection_responder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ limitations under the License.
2929
// particular applications.
3030
void RespondToDetection(float person_score, float no_person_score);
3131

32-
#endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_DETECTION_RESPONDER_H_
32+
// Initialize GUI components
33+
void create_gui();
34+
35+
#endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_DETECTION_RESPONDER_H_

examples/person_detection/main/idf_component.yml

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ dependencies:
22
espressif/esp-tflite-micro:
33
version: "*"
44
override_path: "../../../"
5-
6-
espressif/esp32-camera: "~2.0.5"
7-
5+
espressif/esp32_s2_kaluga_kit:
6+
rules:
7+
- if: "$TFLITE_USE_BSP_KALUGA == 1"
8+
version: 3.*
89
espressif/esp32_s3_eye:
9-
version: "3.*"
1010
rules:
11-
- if: "target == $TFLITE_USE_BSP_S3_EYE"
12-
11+
- if: "$TFLITE_USE_BSP_S3_EYE == 1"
12+
version: 4.*
1313
espressif/esp32_s3_korvo_2:
14-
version: "2.*"
15-
rules:
16-
- if: "target == $TFLITE_USE_BSP_KORVO_2"
17-
18-
espressif/esp32_s2_kaluga_kit:
19-
version: "3.*"
2014
rules:
21-
- if: "target == $TFLITE_USE_BSP_KALUGA"
15+
- if: "$TFLITE_USE_BSP_KORVO_2 == 1"
16+
version: 2.*

examples/person_detection/main/image_provider.cc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
32
43
Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,16 +12,14 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1312
See the License for the specific language governing permissions and
1413
limitations under the License.
1514
==============================================================================*/
16-
#include <cstdlib>
17-
#include <cstring>
18-
#include <iostream>
1915

16+
#include "string.h"
2017
#include "freertos/FreeRTOS.h"
2118
#include "freertos/task.h"
19+
20+
#include "esp_heap_caps.h"
2221
#include "esp_log.h"
23-
#include "esp_spi_flash.h"
24-
#include "esp_system.h"
25-
#include "esp_timer.h"
22+
#include "bsp/esp-bsp.h"
2623

2724
#include "app_camera_esp.h"
2825
#include "esp_camera.h"
@@ -31,8 +28,8 @@ limitations under the License.
3128
#include "esp_main.h"
3229

3330
static const char* TAG = "app_camera";
34-
35-
static uint16_t *display_buf; // buffer to hold data to be sent to display
31+
static uint16_t* display_buf;
32+
static uint16_t* cam_buf;
3633

3734
// Get the camera module ready
3835
TfLiteStatus InitCamera() {
@@ -51,6 +48,14 @@ TfLiteStatus InitCamera() {
5148
ESP_LOGE(TAG, "Couldn't allocate display buffer");
5249
return kTfLiteError;
5350
}
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+
}
5459
#endif // DISPLAY_SUPPORT
5560

5661
#if ESP_CAMERA_SUPPORTED
@@ -84,13 +89,18 @@ TfLiteStatus GetImage(int image_width, int image_height, int channels, int8_t* i
8489
// In case if display support is enabled, we initialise camera in rgb mode
8590
// Hence, we need to convert this data to grayscale to send it to tf model
8691
// For display we extra-polate the data to 192X192
92+
93+
memcpy(cam_buf, fb->buf, fb->len);
94+
lv_draw_sw_rgb565_swap(fb->buf, 96 * 96);
95+
8796
for (int i = 0; i < kNumRows; i++) {
8897
for (int j = 0; j < kNumCols; j++) {
8998
uint16_t pixel = ((uint16_t *) (fb->buf))[i * kNumCols + j];
99+
uint16_t inference_pixel = ((uint16_t *) (cam_buf))[i * kNumCols + j];
90100

91101
// for inference
92-
uint8_t hb = pixel & 0xFF;
93-
uint8_t lb = pixel >> 8;
102+
uint8_t hb = inference_pixel & 0xFF;
103+
uint8_t lb = inference_pixel >> 8;
94104
uint8_t r = (lb & 0x1F) << 3;
95105
uint8_t g = ((hb & 0x07) << 5) | ((lb & 0xE0) >> 3);
96106
uint8_t b = (hb & 0xF8);
@@ -103,7 +113,6 @@ TfLiteStatus GetImage(int image_width, int image_height, int channels, int8_t* i
103113

104114
image_data[i * kNumCols + j] = grey_pixel;
105115

106-
// to display
107116
display_buf[2 * i * kNumCols * 2 + 2 * j] = pixel;
108117
display_buf[2 * i * kNumCols * 2 + 2 * j + 1] = pixel;
109118
display_buf[(2 * i + 1) * kNumCols * 2 + 2 * j] = pixel;

examples/person_detection/main/main_functions.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ limitations under the License.
3232
#include <esp_log.h>
3333
#include "esp_main.h"
3434

35-
// Globals, used for compatibility with Arduino-style sketches.
3635
namespace {
3736
const tflite::Model* model = nullptr;
3837
tflite::MicroInterpreter* interpreter = nullptr;
@@ -56,7 +55,6 @@ constexpr int kTensorArenaSize = 100 * 1024 + scratchBufSize;
5655
static uint8_t *tensor_arena;//[kTensorArenaSize]; // Maybe we should move this to external
5756
} // namespace
5857

59-
// The name of this function is important for Arduino compatibility.
6058
void setup() {
6159
// Map the model into a usable data structure. This doesn't involve any
6260
// copying or parsing, it's a very lightweight operation.
@@ -68,7 +66,7 @@ void setup() {
6866
}
6967

7068
if (tensor_arena == NULL) {
71-
tensor_arena = (uint8_t *) heap_caps_malloc(kTensorArenaSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
69+
tensor_arena = (uint8_t *) heap_caps_malloc(kTensorArenaSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
7270
}
7371
if (tensor_arena == NULL) {
7472
printf("Couldn't allocate memory of %d bytes\n", kTensorArenaSize);
@@ -113,11 +111,12 @@ void setup() {
113111
MicroPrintf("InitCamera failed\n");
114112
return;
115113
}
114+
115+
create_gui();
116116
#endif
117117
}
118118

119119
#ifndef CLI_ONLY_INFERENCE
120-
// The name of this function is important for Arduino compatibility.
121120
void loop() {
122121
// Get image from provider.
123122
if (kTfLiteOk != GetImage(kNumCols, kNumRows, kNumChannels, input->data.int8)) {

0 commit comments

Comments
 (0)