@@ -29,7 +29,6 @@ limitations under the License.
2929
3030static const char * TAG = " app_camera" ;
3131static uint16_t * display_buf;
32- static uint16_t * cam_buf;
3332
3433// Get the camera module ready
3534TfLiteStatus 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
0 commit comments