1-
21/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
32
43Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,16 +12,14 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1312See the License for the specific language governing permissions and
1413limitations 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
3330static 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
3835TfLiteStatus 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;
0 commit comments