diff --git a/c_mpos/src/webcam.c b/c_mpos/src/webcam.c index cd407f57..6d666998 100644 --- a/c_mpos/src/webcam.c +++ b/c_mpos/src/webcam.c @@ -149,6 +149,34 @@ static void deinit_webcam(webcam_obj_t *self) { self->fd = -1; } +static mp_obj_t recapture_frame(webcam_obj_t *self) { + struct v4l2_buffer buf = {0}; + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + if (ioctl(self->fd, VIDIOC_DQBUF, &buf) < 0) { + mp_raise_OSError(MP_EIO); + } + + if (!self->gray_buffer) { + mp_raise_OSError(MP_ENOMEM); + } + + yuyv_to_grayscale_240x240(self->buffers[buf.index], self->gray_buffer, WIDTH, HEIGHT); + + //char filename[32]; + //snprintf(filename, sizeof(filename), "frame_%03d.raw", self->frame_count++); + //save_raw(filename, self->gray_buffer, OUTPUT_WIDTH, OUTPUT_HEIGHT); + + //mp_obj_t result = mp_obj_new_memoryview(0x01, OUTPUT_WIDTH * OUTPUT_HEIGHT, self->gray_buffer); + mp_obj_t result = mp_const_none; + + if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) { + mp_raise_OSError(MP_EIO); + } + + return result; +} + static mp_obj_t capture_frame(webcam_obj_t *self) { struct v4l2_buffer buf = {0}; buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; @@ -212,6 +240,16 @@ static mp_obj_t webcam_capture_frame(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(webcam_capture_frame_obj, webcam_capture_frame); +static mp_obj_t webcam_recapture_frame(mp_obj_t self_in) { + webcam_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->fd < 0) { + mp_raise_OSError(MP_EIO); + } + return recapture_frame(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(webcam_recapture_frame_obj, webcam_recapture_frame); + + static const mp_obj_type_t webcam_type = { { &mp_type_type }, .name = MP_QSTR_Webcam, @@ -222,6 +260,7 @@ static const mp_rom_map_elem_t mp_module_webcam_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Webcam), MP_ROM_PTR(&webcam_type) }, { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&webcam_init_obj) }, { MP_ROM_QSTR(MP_QSTR_capture_frame), MP_ROM_PTR(&webcam_capture_frame_obj) }, + { MP_ROM_QSTR(MP_QSTR_recapture_frame), MP_ROM_PTR(&webcam_recapture_frame_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) }, }; static MP_DEFINE_CONST_DICT(mp_module_webcam_globals, mp_module_webcam_globals_table); diff --git a/internal_filesystem/apps/com.example.camtest/assets/camtest.py b/internal_filesystem/apps/com.example.camtest/assets/camtest.py index fa01d421..4dd74096 100644 --- a/internal_filesystem/apps/com.example.camtest/assets/camtest.py +++ b/internal_filesystem/apps/com.example.camtest/assets/camtest.py @@ -128,7 +128,7 @@ def try_capture_old(): def build_ui(): - global image, image_dsc,qr_label + global image, image_dsc,qr_label, cam cont = lv.obj(appscreen) cont.set_style_pad_all(0, 0) cont.set_style_border_width(0, 0) @@ -160,6 +160,7 @@ def build_ui(): image.align(lv.ALIGN.LEFT_MID, 0, 0) image.set_rotation(900) # Create image descriptor once + memview = webcam.capture_frame(cam) # Returns memoryview image_dsc = lv.image_dsc_t({ "header": { "magic": lv.IMAGE_HEADER_MAGIC, @@ -170,7 +171,7 @@ def build_ui(): "cf": lv.COLOR_FORMAT.L8 }, 'data_size': width * height, - 'data': None # Will be updated per frame + 'data': memview # Will be updated per frame }) image.set_src(image_dsc) @@ -226,7 +227,10 @@ if cam or use_webcam: while appscreen == lv.screen_active() and keepgoing is True: print(f"capture nr {count}") count += 1 - try_capture() + #try_capture() + webcam.recapture_frame(cam) + #image.invalidate() + #image.set_src(image_dsc) time.sleep_ms(100) # Allow for the MicroPython REPL to still work. Reducing it doesn't seem to affect the on-display FPS. print("App backgrounded, deinitializing camera...") if use_webcam: