From a1c8e65e1f3d6acda1fe18f6ede1ee8edf7ef03e Mon Sep 17 00:00:00 2001 From: lxowalle Date: Fri, 26 Jul 2024 20:56:06 +0800 Subject: [PATCH] * camera blocks when reading too fast --- components/vision/include/maix_camera.hpp | 1 + components/vision/src/maix_camera.cpp | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/components/vision/include/maix_camera.hpp b/components/vision/include/maix_camera.hpp index fa74d812..827633d2 100644 --- a/components/vision/include/maix_camera.hpp +++ b/components/vision/include/maix_camera.hpp @@ -391,5 +391,6 @@ namespace maix::camera bool _open_set_regs; CameraBase *_impl; // used by implement code bool _check_format(image::Format format); + uint64_t _last_read_ms; }; } diff --git a/components/vision/src/maix_camera.cpp b/components/vision/src/maix_camera.cpp index 91433a06..0a3104b4 100644 --- a/components/vision/src/maix_camera.cpp +++ b/components/vision/src/maix_camera.cpp @@ -128,6 +128,7 @@ namespace maix::camera #endif #ifdef PLATFORM_MAIXCAM + _last_read_ms = time::ticks_ms(); if (fps == -1 && _width <= 1280 && _height <= 720) { _fps = 80; } else if (fps == -1) { @@ -151,7 +152,7 @@ namespace maix::camera #endif if (open) { - e = this->open(_width, _height, _format, _buff_num); + e = this->open(_width, _height, _format, _fps, _buff_num); err::check_raise(e, "camera open failed"); } } @@ -320,6 +321,12 @@ namespace maix::camera { image::Image *img = _impl->read(buff, buff_size); err::check_null_raise(img, "camera read failed"); + + uint64_t wait_ms = 1000 / _fps; + while (time::ticks_ms() - _last_read_ms <= wait_ms) { + time::sleep_ms(1); + } + _last_read_ms = time::ticks_ms(); return img; } else @@ -328,6 +335,12 @@ namespace maix::camera image::Image *img2 = img->to_format(_format, buff, buff_size); delete img; err::check_null_raise(img2, "camera read failed"); + + uint64_t wait_ms = 1000 / _fps; + while (time::ticks_ms() - _last_read_ms <= wait_ms) { + time::sleep_ms(1); + } + _last_read_ms = time::ticks_ms(); return img2; } }