Skip to content

Commit

Permalink
* camera blocks when reading too fast
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Jul 26, 2024
1 parent a32a907 commit a1c8e65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions components/vision/include/maix_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
15 changes: 14 additions & 1 deletion components/vision/src/maix_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
Expand Down

0 comments on commit a1c8e65

Please sign in to comment.