Skip to content

Commit a56a811

Browse files
committed
camera api not good enough
1 parent 8bff420 commit a56a811

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

src/modcamera.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "modcamera.h"
2929
#include "esp_err.h"
3030
#include "esp_log.h"
31+
#include "img_converters.h"
3132

3233
#define TAG "ESP32_MPY_CAMERA"
3334

@@ -240,8 +241,7 @@ void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize_t fram
240241
}
241242
}
242243

243-
mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int timeout_ms) {
244-
// Timeout not used at the moment
244+
mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, mp_camera_pixformat_t out_format) {
245245
if (!self->initialized) {
246246
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to capture image: Camera not initialized"));
247247
}
@@ -252,20 +252,44 @@ mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int timeout_ms) {
252252
ESP_LOGI(TAG, "Capturing image");
253253
self->captured_buffer = esp_camera_fb_get();
254254
if (self->captured_buffer) {
255-
if (self->camera_config.pixel_format == PIXFORMAT_JPEG) {
256-
ESP_LOGI(TAG, "Captured image in JPEG format");
257-
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
258-
} else {
259-
ESP_LOGI(TAG, "Captured image in raw format");
260-
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
261-
// TODO: Stub at the moment in order to return raw data, but it sould be implemented to return a Bitmap, see following circuitpython example:
262-
//
263-
// int width = common_hal_espcamera_camera_get_width(self);
264-
// int height = common_hal_espcamera_camera_get_height(self);
265-
// displayio_bitmap_t *bitmap = m_new_obj(displayio_bitmap_t);
266-
// bitmap->base.type = &displayio_bitmap_type;
267-
// common_hal_displayio_bitmap_construct_from_buffer(bitmap, width, height, (format == PIXFORMAT_RGB565) ? 16 : 8, (uint32_t *)(void *)result->buf, true);
268-
// return bitmap;
255+
switch (out_format) {
256+
case PIXFORMAT_JPEG:
257+
return mp_const_none;
258+
259+
case PIXFORMAT_RGB888:
260+
size_t out_len = self->captured_buffer->width * self->captured_buffer->height * 3;
261+
uint8_t *out_buf = (uint8_t *)malloc(out_len);
262+
if (!out_buf) {
263+
ESP_LOGE(TAG, "out_buf malloc failed");
264+
return mp_const_none;
265+
}
266+
if (fmt2rgb888(self->captured_buffer->buf, self->captured_buffer->len, self->captured_buffer->format, out_buf)){
267+
esp_camera_fb_return(self->captured_buffer);
268+
mp_obj_t result = mp_obj_new_bytes(out_buf, out_len);
269+
free(out_buf);
270+
return result;
271+
} else {
272+
free(out_buf);
273+
return mp_const_none;
274+
}
275+
276+
default:
277+
if (self->camera_config.pixel_format == PIXFORMAT_JPEG) {
278+
ESP_LOGI(TAG, "Captured image in JPEG format");
279+
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
280+
} else {
281+
ESP_LOGI(TAG, "Returning image as bitmap");
282+
uint8_t *out = NULL;
283+
size_t out_len = 0;
284+
if (frame2bmp(self->captured_buffer, &out, &out_len)) {
285+
esp_camera_fb_return(self->captured_buffer);
286+
mp_obj_t result = mp_obj_new_bytes(out, out_len);
287+
free(out);
288+
return result;
289+
} else {
290+
return mp_const_none;
291+
}
292+
}
269293
}
270294
} else {
271295
esp_camera_fb_return(self->captured_buffer);
@@ -283,6 +307,7 @@ const mp_rom_map_elem_t mp_camera_hal_pixel_format_table[] = {
283307
{ MP_ROM_QSTR(MP_QSTR_YUV422), MP_ROM_INT(PIXFORMAT_YUV422) },
284308
{ MP_ROM_QSTR(MP_QSTR_GRAYSCALE), MP_ROM_INT(PIXFORMAT_GRAYSCALE) },
285309
{ MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_INT(PIXFORMAT_RGB565) },
310+
{ MP_ROM_QSTR(MP_QSTR_RGB888), MP_ROM_INT(PIXFORMAT_RGB888) },
286311
};
287312

288313
const mp_rom_map_elem_t mp_camera_hal_frame_size_table[] = {

src/modcamera.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,16 @@ extern void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize
194194
* @brief Captures an image and returns it as mp_obj_t (e.g. mp_obj_new_memoryview).
195195
*
196196
* @param self Pointer to the camera object.
197-
* @param timeout_ms Timeout in milliseconds.
197+
* @param out_format Output pixelformat format.
198198
* @return Captured image as micropython object.
199199
*/
200-
extern mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int timeout_ms);
200+
extern mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, hal_camera_pixformat_t out_format);
201201

202202
/**
203203
* @brief Table mapping pixel formats API to their corresponding values at HAL.
204204
* @details Needs to be defined in the port-specific implementation.
205205
*/
206-
extern const mp_rom_map_elem_t mp_camera_hal_pixel_format_table[4];
206+
extern const mp_rom_map_elem_t mp_camera_hal_pixel_format_table[5];
207207

208208
/**
209209
* @brief Table mapping frame sizes API to their corresponding values at HAL.

src/modcamera_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ static mp_obj_t mp_camera_make_new(const mp_obj_type_t *type, size_t n_args, siz
150150
// Main methods
151151
static mp_obj_t camera_capture(size_t n_args, const mp_obj_t *args){
152152
mp_camera_obj_t *self = MP_OBJ_TO_PTR(args[0]);
153-
mp_float_t timeout = n_args < 2 ? MICROPY_FLOAT_CONST(0.25) : mp_obj_get_float(args[1]);
154-
return mp_camera_hal_capture(self, timeout);
153+
uint8_t out_format = n_args < 2 ? MP_ROM_NONE : mp_obj_get_int(args[1]);
154+
return mp_camera_hal_capture(self, out_format);
155155
}
156156
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(camera_capture_obj, 1, 2, camera_capture);
157157

@@ -211,8 +211,8 @@ static mp_obj_t mp_camera_obj___exit__(size_t n_args, const mp_obj_t *args) {
211211
}
212212
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_camera___exit___obj, 4, 4, mp_camera_obj___exit__);
213213

214-
// Camera propertiy functions
215-
// Camera sensor propertiy functions
214+
// Camera property functions
215+
// Camera sensor property functions
216216
#define CREATE_GETTER(property, get_function) \
217217
static mp_obj_t camera_get_##property(const mp_obj_t self_in) { \
218218
mp_camera_obj_t *self = MP_OBJ_TO_PTR(self_in); \

0 commit comments

Comments
 (0)