Skip to content

Commit

Permalink
* support set_windowing
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Jul 25, 2024
1 parent 622c398 commit 1e778d3
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 10 deletions.
4 changes: 4 additions & 0 deletions components/maixcam_lib/include/sophgo_middleware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ int mmf_get_vi_unused_channel(void);
int mmf_vi_init(void);
int mmf_vi_init2(mmf_vi_cfg_t *vi_info);
int mmf_vi_deinit(void);
int mmf_vi_get_max_size(int *width, int *height);
int mmf_vi_chn_caculate_ldc(int ch, int x_ratio, int y_ratio, int center_x_oft, int center_y_oft, int distortion_ratio);
int mmf_vi_chn_ldc_enable(int ch, bool en);
int mmf_add_vi_channel_with_enc(int ch, int width, int height, int format);
int mmf_add_vi_channel(int ch, int width, int height, int format);
int mmf_del_vi_channel(int ch);
int mmf_del_vi_channel_all(void);
int mmf_vi_channel_set_windowing(int ch, int x, int y, int w, int h);
int mmf_reset_vi_channel(int ch, int width, int height, int format);
bool mmf_vi_chn_is_open(int ch);
int mmf_vi_aligned_width(int ch);
Expand Down
8 changes: 8 additions & 0 deletions components/vision/include/base/maix_camera_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ namespace maix::camera
* @return returns exposure mode
*/
virtual int exp_mode(int value = -1) = 0;

/**
* Set window size of camera
* @param roi Support two input formats, [x,y,w,h] set the coordinates and size of the window;
* [w,h] set the size of the window, when the window is centred.
* @return error code
*/
virtual int set_windowing(std::vector<int> roi) = 0;
};

}
Expand Down
9 changes: 9 additions & 0 deletions components/vision/include/maix_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,15 @@ namespace maix::camera
* @maixpy maix.camera.Camera.exp_mode
*/
int exp_mode(int value = -1);

/**
* Set window size of camera
* @param roi Support two input formats, [x,y,w,h] set the coordinates and size of the window;
* [w,h] set the size of the window, when the window is centred.
* @return error code
* @maixpy maix.camera.Camera.set_windowing
*/
err::Err set_windowing(std::vector<int> roi);
private:
std::string _device;
int _ch;
Expand Down
5 changes: 5 additions & 0 deletions components/vision/port/linux/maix_camera_v4l2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,11 @@ namespace maix::camera
{
return -1;
}

int set_windowing(std::vector<int> roi)
{
return -1;
}
private:
std::string device;
image::Format format;
Expand Down
36 changes: 36 additions & 0 deletions components/vision/port/maixcam/maix_camera_mmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,42 @@ namespace maix::camera
err::check_bool_raise(out >= 0, "set exposure failed");
return out;
}

int set_windowing(std::vector<int> roi)
{
int max_height, max_width;
char log_msg[100];
int x = 0, y = 0, w = 0, h = 0;

err::check_bool_raise(!mmf_vi_get_max_size(&max_width, &max_height), "get max size of camera failed");

if (roi.size() == 4) {
x = roi[0], y = roi[1], w = roi[2], h = roi[3];
} else if (roi.size() == 2) {
w = roi[0], h = roi[1];
x = (max_width - w) / 2;
y = (max_height - h) / 2;
} else {
err::check_raise(err::ERR_RUNTIME, "roi size must be 4 or 2");
}

snprintf(log_msg, sizeof(log_msg), "Width must be a multiple of 64.");
err::check_bool_raise(w % 64 == 0, std::string(log_msg));
snprintf(log_msg, sizeof(log_msg), "the coordinate x range needs to be [0,%d].", max_width - 1);
err::check_bool_raise(x >= 0 || x < max_width, std::string(log_msg));
snprintf(log_msg, sizeof(log_msg), "the coordinate y range needs to be [0,%d].", max_height - 1);
err::check_bool_raise(y >= 0 || y < max_height, std::string(log_msg));
snprintf(log_msg, sizeof(log_msg), "the row of the window is larger than the maximum, try x=%d, w=%d.", x, max_width - x);
err::check_bool_raise(x + w <= max_width, std::string(log_msg));
snprintf(log_msg, sizeof(log_msg), "the column of the window is larger than the maximum, try y=%d, h=%d.", y, max_height - y);
err::check_bool_raise(y + h <= max_height, std::string(log_msg));

this->width = w;
this->height = h;
err::check_bool_raise(!mmf_vi_channel_set_windowing(ch, x, y, w, h), "set windowing failed.");

return 0;
}
private:
std::string device;
image::Format format;
Expand Down
61 changes: 54 additions & 7 deletions components/vision/port/maixcam/maix_display_mmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,63 @@
#include "maix_thread.hpp"
#include "maix_image.hpp"
#include "maix_time.hpp"
#include <unistd.h>
#include "sophgo_middleware.hpp"
#include "maix_pwm.hpp"

using namespace maix::peripheral;

namespace maix::display
{
__attribute__((unused)) static int _get_vo_max_size(int *width, int *height, int rotate)
{
int w = 0, h = 0;
const char *fb_path = "/dev/fb0";
uint8_t need_rmmod = 0;
if (access(fb_path, F_OK) == -1) {
system("insmod /mnt/system/ko/soph_fb.ko");
need_rmmod = 1;
}

FILE *fp;
char buffer[4096];
fp = popen("fbset", "r");
if (fp == NULL) {
perror("popen failed");
return 1;
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
char *mode_line = strstr(buffer, "mode");
if (mode_line) {
sscanf(mode_line, "mode \"%dx%d", &w, &h);
break;
}
}
pclose(fp);

if (need_rmmod) {
system("rmmod soph_fb");
}

if (rotate) {
*width = h;
*height = w;
} else {
*width = w;
*height = h;
}
return 0;
}
class DisplayCviMmf final : public DisplayBase
{
public:
DisplayCviMmf(const char *device, int width, int height, image::Format format)
{
this->_width = width > 552 ? 552 : width;
this->_height = height > 368 ? 368 : height;
err::check_bool_raise(!_get_vo_max_size(&_max_width, &_max_height, 1), "get vo max size failed");
width = width <= 0 ? _max_width : width;
height = height <= 0 ? _max_height : height;
this->_width = width > _max_width ? _max_width : width;
this->_height = height > _max_height ? _max_height : height;
this->_format = format;
this->_opened = false;
this->_format = format;
Expand All @@ -43,8 +86,11 @@ namespace maix::display

DisplayCviMmf(int layer, int width, int height, image::Format format)
{
this->_width = width > 552 ? 552 : width;
this->_height = height > 368 ? 368 : height;
err::check_bool_raise(!_get_vo_max_size(&_max_width, &_max_height, 1), "get vo max size failed");
width = width <= 0 ? _max_width : width;
height = height <= 0 ? _max_height : height;
this->_width = width > _max_width ? _max_width : width;
this->_height = height > _max_height ? _max_height : height;
this->_format = format;
this->_opened = false;
this->_format = format;
Expand Down Expand Up @@ -91,9 +137,8 @@ namespace maix::display

err::Err open(int width, int height, image::Format format)
{
width = width > 552 ? 552 : width;
height = height > 368 ? 368 : height;

width = width > _max_width ? _max_width : width;
height = height > _max_height ? _max_height : height;
if(this->_opened)
{
return err::ERR_NONE;
Expand Down Expand Up @@ -322,6 +367,8 @@ namespace maix::display
private:
int _width;
int _height;
int _max_width;
int _max_height;
image::Format _format;
int _layer;
int _ch;
Expand Down
21 changes: 21 additions & 0 deletions components/vision/src/maix_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,5 +483,26 @@ namespace maix::camera

return _impl->exp_mode(value);
}

err::Err Camera::set_windowing(std::vector<int> roi) {
if (_impl == NULL)
return err::ERR_NOT_INIT;

if (!this->is_opened()) {
return err::ERR_NOT_OPEN;
}

err::check_bool_raise(!_impl->set_windowing(roi), "set_windowing failed");
if (roi.size() == 4) {
this->_width = roi[2];
this->_height = roi[3];
} else if (roi.size() == 2) {
this->_width = roi[0];
this->_height = roi[1];
} else {
err::check_raise(err::ERR_RUNTIME, "roi size must be 2 or 4");
}
return err::ERR_NONE;
}
}

3 changes: 0 additions & 3 deletions components/vision/src/maix_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ namespace maix::display

Display::Display(int width, int height, image::Format format, const char *device, bool open)
{
// TODO: Get actual device max size
width = (width == -1) ? 640 : width;
height = (height == -1) ? 480 : height;
_impl = NULL;

// Select implementation by platform
Expand Down

0 comments on commit 1e778d3

Please sign in to comment.