-
Notifications
You must be signed in to change notification settings - Fork 1
/
ei_image_lib.cpp
245 lines (207 loc) · 7.04 KB
/
ei_image_lib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2022 EdgeImpulse Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
// set to 1 to generate and send a test image
#define SEND_TEST_IMAGE 0
#include "firmware-sdk/ei_camera_interface.h"
#include "firmware-sdk/ei_device_info_lib.h"
#if (EI_PORTING_SONY_SPRESENSE || ARDUINO_NICLA_VISION)
#define ALLIGNED_BUFFER 1
#include "malloc.h" //for memalign
#endif
#include <memory>
#include "edge-impulse-sdk/dsp/ei_utils.h"
#include "edge-impulse-sdk/dsp/image/image.hpp"
#include "edge-impulse-sdk/porting/ei_classifier_porting.h"
#include "firmware-sdk/at_base64_lib.h"
#include "firmware-sdk/ei_device_interface.h"
#include "firmware-sdk/ei_image_lib.h"
// *********************************** AT cmd functions ***************
static void respond_and_change_to_max_baud()
{
auto device = EiDeviceInfo::get_device();
// sleep a little to let the daemon attach on the new baud rate...
ei_printf("\r\nOK");
ei_sleep(100);
device->set_max_data_output_baudrate();
ei_sleep(100);
}
static void change_to_normal_baud()
{
auto device = EiDeviceInfo::get_device();
// lower baud rate
ei_printf("\r\nOK\r\n");
ei_sleep(100);
device->set_default_data_output_baudrate();
// sleep a little to let the daemon attach on baud rate 115200 again...
ei_sleep(100);
}
static bool ei_camera_take_snapshot_encode_and_output_no_init(size_t width, size_t height)
{
using namespace ei::image::processing;
bool needs_a_resize = false;
ei_device_snapshot_resolutions_t fb_resoluton;
uint16_t final_width = width;
uint16_t final_height = height;
auto camera = EiCamera::get_camera();
EiDeviceInfo* dev = EiDeviceInfo::get_device();
EiSnapshotProperties props = dev->get_snapshot_list();
int pixel_size_B = (props.color_depth == "RGB") ? RGB888_B_SIZE : MONO_B_SIZE;
// check if minimum suitable sensor resolution is the same as
// desired snapshot resolution
// if not we need to resize later
fb_resoluton = camera->search_resolution(width, height);
if (width != fb_resoluton.width || height != fb_resoluton.height) {
needs_a_resize = true;
width = fb_resoluton.width;
height = fb_resoluton.height;
}
uint32_t size = width * height * pixel_size_B;
#if ALLIGNED_BUFFER
// 32 BYTE aligned buffer
auto image_p = std::unique_ptr<uint8_t, decltype(free) *> { reinterpret_cast<uint8_t *>(
memalign(32, size)),
free };
auto image = image_p.get();
#else // more portable version
// try to get framebuffer to be used for image transformations
// from the camera
// if the camera driver does not make it possible
// then create our own second framebuffer
uint8_t* image = nullptr;
if (!camera->get_fb_ptr(&image)) {
std::unique_ptr<uint8_t[]> image_p(new uint8_t[size]);
if (!image_p) {
ei_printf("ERR: Cannot allocate memory for framebuffer\n");
return false;
}
image = image_p.get();
}
else {
camera->get_fb_ptr(&image);
}
#endif
#if SEND_TEST_IMAGE
uint32_t counter = 0;
switch(pixel_size_B) {
case RGB888_B_SIZE:
for (int i = 0; i < size; i += 3) {
image[i] = counter & 0xff;
image[i + 1] = counter >> 8;
image[i + 2] = counter >> 16;
counter += 100;
}
break;
case MONO_B_SIZE:
for (int i = 0; i < size; i += 1) {
if (counter >= 256) { counter = 0; }
image[i] = counter;
counter += 1;
}
break;
default:
ei_printf("ERR: Wrong pixel size, not supported: %d\n", pixel_size_B);
break;
}
#else
bool isOK = false;
switch(pixel_size_B) {
case RGB888_B_SIZE:
isOK = camera->ei_camera_capture_rgb888_packed_big_endian(image, size);
break;
case MONO_B_SIZE:
isOK = camera->ei_camera_capture_grayscale_packed_big_endian(image, size);
break;
default:
ei_printf("ERR: Wrong pixel size, not supported: %d\n", pixel_size_B);
break;
}
if (!isOK) {
return false;
}
if (needs_a_resize) {
// interpolate in place
ei::image::processing::crop_and_interpolate_image(
image,
width,
height,
image,
final_width,
final_height,
pixel_size_B);
}
#endif
// recalculate size b/c now we want to send just the interpolated bytes
base64_encode(
reinterpret_cast<char *>(image),
final_height * final_width * pixel_size_B,
ei_putchar);
return true;
}
extern bool
ei_camera_take_snapshot_output_on_serial(size_t width, size_t height, bool use_max_baudrate)
{
auto camera = EiCamera::get_camera();
// sets camera sensor resolution to the best suitable
// might not be the same as final snapshot resolution
// this is why below we pass desired snapshot resolution
// to ei_camera_take_snapshot_encode_and_output_no_init
if (!camera->init(width, height)) {
ei_printf("Failed to init camera\n");
return false;
}
if (use_max_baudrate) {
respond_and_change_to_max_baud();
}
// here we pass desired snapshot resolution
// if it is different from camera sensor resolution
// we will resize before sending out the image
bool isOK = ei_camera_take_snapshot_encode_and_output_no_init(width, height);
camera->deinit();
if (use_max_baudrate) {
change_to_normal_baud();
}
else {
ei_printf("\r\nOK\r\n");
}
return isOK;
}
extern bool ei_camera_start_snapshot_stream(size_t width, size_t height, bool use_max_baudrate)
{
bool isOK = true;
ei_printf("Starting snapshot stream...\n");
auto camera = EiCamera::get_camera();
if (!camera->init(width, height)) {
ei_printf("Failed to init camera\n");
return false;
}
if (use_max_baudrate) {
respond_and_change_to_max_baud();
}
while (!ei_user_invoke_stop_lib()) {
isOK &= ei_camera_take_snapshot_encode_and_output_no_init(width, height);
ei_printf("\r\n");
}
camera->deinit();
if (use_max_baudrate) {
change_to_normal_baud();
}
return isOK;
}
//AT+SNAPSHOTSTREAM=128,96
//AT+SNAPSHOT=128,96
//AT+SNAPSHOT=640,480
//AT+SNAPSHOT=320,240