Skip to content

Commit

Permalink
* optimize camera and app_camera
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Oct 8, 2024
1 parent 6848032 commit 42a2680
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 10 deletions.
6 changes: 6 additions & 0 deletions components/vision/include/maix_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ namespace maix::camera
* @maixpy maix.camera.Camera.set_windowing
*/
err::Err set_windowing(std::vector<int> roi);

/**
* Get sensor size
* @return sensor size
*/
std::vector<int> get_sensor_size();
private:
std::string _device;
int _ch;
Expand Down
5 changes: 5 additions & 0 deletions components/vision/port/linux/maix_camera_v4l2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,5 +1396,10 @@ namespace maix::camera
err::Err Camera::set_windowing(std::vector<int> roi) {
return err::ERR_NOT_IMPL;
}


std::vector<int> Camera::get_sensor_size() {
return {0, 0};
}
}

64 changes: 61 additions & 3 deletions components/vision/port/maixcam/maix_camera_mmf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,9 +961,11 @@ namespace maix::camera

uint32_t out;
if (value == -1) {
mmf_get_again(_ch, &out);
mmf_get_iso_num(_ch, &out);
out = (double)out * 1024 / 100;
} else {
mmf_set_again(_ch, value);
int iso_num = value * 100 / 1024;
mmf_set_iso_num(_ch, iso_num);
out = value;
}
return out;
Expand Down Expand Up @@ -1099,6 +1101,52 @@ namespace maix::camera
return this->awb_mode(value) == 0 ? 1 : 0;
}

static int _mmf_set_exp_mode(int ch, int mode)
{
CVI_U32 ret;
ISP_EXPOSURE_ATTR_S stExpAttr;

memset(&stExpAttr, 0, sizeof(ISP_EXPOSURE_ATTR_S));

ret = CVI_ISP_GetExposureAttr(ch, &stExpAttr);
if (ret != 0) {
printf("CVI_ISP_GetExposureAttr failed, ret: %#x.\r\n", ret);
return -1;
}

if (stExpAttr.enOpType == mode) {
return 0;
}

stExpAttr.u8DebugMode = 0;
if (mode == 0) {
stExpAttr.bByPass = 0;
stExpAttr.enOpType = OP_TYPE_AUTO;
stExpAttr.stManual.enExpTimeOpType = OP_TYPE_AUTO;
stExpAttr.stManual.enISONumOpType = OP_TYPE_AUTO;
stExpAttr.stManual.enAGainOpType = OP_TYPE_AUTO;
stExpAttr.stManual.enDGainOpType = OP_TYPE_AUTO;
stExpAttr.stManual.enISPDGainOpType = OP_TYPE_AUTO;
} else if (mode == 1) {
stExpAttr.bByPass = 0;
stExpAttr.enOpType = OP_TYPE_MANUAL;
stExpAttr.stManual.enExpTimeOpType = OP_TYPE_MANUAL;
stExpAttr.stManual.enISONumOpType = OP_TYPE_MANUAL;
stExpAttr.stManual.enAGainOpType = OP_TYPE_MANUAL;
stExpAttr.stManual.enDGainOpType = OP_TYPE_MANUAL;
stExpAttr.stManual.enISPDGainOpType = OP_TYPE_MANUAL;
stExpAttr.stManual.enGainType = AE_TYPE_ISO;
}

ret = CVI_ISP_SetExposureAttr(ch, &stExpAttr);
if (ret != 0) {
printf("CVI_ISP_SetExposureAttr failed, ret: %#x.\r\n", ret);
return -1;
}

return 0;
}

int Camera::exp_mode(int value) {
if (!this->is_opened()) {
return err::ERR_NOT_OPEN;
Expand All @@ -1108,7 +1156,7 @@ namespace maix::camera
if (value == -1) {
out = mmf_get_exp_mode(_ch);
} else {
mmf_set_exp_mode(_ch, value);
_mmf_set_exp_mode(_ch, value);
out = value;
}

Expand Down Expand Up @@ -1160,5 +1208,15 @@ namespace maix::camera
err::check_bool_raise(!mmf_vi_channel_set_windowing(_ch, x, y, w, h), "set windowing failed.");
return err::ERR_NONE;
}

std::vector<int> Camera::get_sensor_size() {
camera_priv_t *priv = (camera_priv_t *)_param;
PIC_SIZE_E enPicSize;
SIZE_S stSize;
SAMPLE_COMM_VI_GetSizeBySensor(priv->sns_type, &enPicSize);
SAMPLE_COMM_SYS_GetPicSize(enPicSize, &stSize);

return {(int)stSize.u32Width, (int)stSize.u32Height};
}
}

10 changes: 6 additions & 4 deletions examples/camera_display/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ static int cmd_loop(camera::Camera *cam, display::Display *disp)
{
uint64_t out = 0;
out = cam->exposure(value);
err::check_bool_raise(out == value, "set error");
log::info("set exposure: %ld\r\n", value);
log::info("set exposure: %ld\r\n", out);
out = cam->gain();
log::info("get gain: %d\r\n", out);
out = cam->exposure();
log::info("get exposure: %ld\r\n", out);
}
Expand All @@ -200,10 +201,11 @@ static int cmd_loop(camera::Camera *cam, display::Display *disp)
{
uint32_t out = 0;
out = cam->gain(value);
err::check_bool_raise(out == value, "set error");
log::info("set gain: %ld\r\n", value);
log::info("set gain: %ld\r\n", out);
out = cam->gain();
log::info("get gain: %d\r\n", out);
out = cam->exposure();
log::info("get exposure: %ld\r\n", out);
}
break;
case 2:
Expand Down
16 changes: 16 additions & 0 deletions projects/app_camera/main/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,22 @@ static int app_config_param(void)
}
}

if (ui_get_focus_btn_update_flag()) {
if (ui_get_focus_btn_touched()) {
int width = ALIGN(priv.disp->width(), 64);
int height = ALIGN(priv.disp->height(), 64);
std::vector<int> sensor_size = priv.camera->get_sensor_size();
int windowing_x = ALIGN((sensor_size[0] - width) / 2, 64);
int windowing_y = ALIGN((sensor_size[1] - height) / 2, 64);
priv.camera->set_windowing({windowing_x, windowing_y, width, height});
log::info("camera set windowing, %d, %d, %d, %d", windowing_x, windowing_y, width, height);
} else {
std::vector<int> sensor_size = priv.camera->get_sensor_size();
priv.camera->set_windowing({0, 0, sensor_size[0], sensor_size[1]});
log::info("camera set windowing, %d, %d, %d, %d", 0, 0, sensor_size[0], sensor_size[1]);
}
}

if (ui_get_ev_setting_flag()) {
if (ui_get_ev_auto_flag()) {
printf("EV setting: Auto\n");
Expand Down
27 changes: 27 additions & 0 deletions projects/app_camera/main/app/ui_event_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern lv_obj_t *g_wb_setting;
extern lv_obj_t *g_small_img;
extern lv_obj_t *g_big_img;
extern lv_obj_t *g_video_running_screen;
extern lv_obj_t *g_focus_button;

static struct {
unsigned int camera_snap_start_flag : 1;
Expand All @@ -51,6 +52,8 @@ static struct {
unsigned int wb_auto_flag : 1;
unsigned int photo_delay_anim_start_flag : 1;
unsigned int photo_delay_anim_stop_flag : 1;
unsigned int focus_btn_touched : 1;
unsigned int focus_btn_update_flag : 1;

unsigned int resolution_setting_idx;
} priv = {
Expand Down Expand Up @@ -304,6 +307,20 @@ void event_touch_wb_cb(lv_event_t * e)
}
}

void event_touch_focus_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED) {
if (lv_obj_get_state(g_focus_button) != LV_STATE_FOCUSED) {
priv.focus_btn_touched = 1;
priv.focus_btn_update_flag = 1;
} else {
priv.focus_btn_touched = 0;
priv.focus_btn_update_flag = 1;
}
}
}

void event_shutter_bar_update_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
Expand Down Expand Up @@ -1110,3 +1127,13 @@ void ui_set_record_time(uint64_t ms)
lv_obj_t *label = lv_obj_get_child(g_video_running_screen, -1);
lv_label_set_text(label, str);
}

bool ui_get_focus_btn_update_flag() {
bool flag = priv.focus_btn_update_flag ? true : false;
priv.focus_btn_update_flag = false;
return flag;
}

bool ui_get_focus_btn_touched() {
return priv.focus_btn_touched ? true : false;
}
3 changes: 3 additions & 0 deletions projects/app_camera/main/app/ui_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ void ui_anim_photo_delay_start(int delay_s);
bool ui_get_photo_delay_anim_status(void);
bool ui_get_photo_delay_anim_stop_flag(void);

bool ui_get_focus_btn_update_flag();
bool ui_get_focus_btn_touched();

typedef struct {
unsigned int exposure_time_max;
unsigned int exposure_time_min;
Expand Down
61 changes: 59 additions & 2 deletions projects/app_camera/main/app/ui_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ lv_obj_t *g_ev_setting;
lv_obj_t *g_wb_setting;
lv_obj_t *g_small_img;
lv_obj_t *g_big_img;
lv_obj_t *g_center_img;

lv_obj_t *g_video_running_screen;
lv_obj_t *g_focus_button;

LV_IMG_DECLARE(img_delay);
LV_IMG_DECLARE(img_exit);
Expand All @@ -57,6 +59,7 @@ LV_IMG_DECLARE(img_photo_clicked);
LV_IMG_DECLARE(img_video_ready);
LV_IMG_DECLARE(img_video_stop);
LV_IMG_DECLARE(img_small_recording);
LV_IMG_DECLARE(img_focus);

extern void event_touch_exit_cb(lv_event_t * e);
extern void event_touch_delay_cb(lv_event_t * e);
Expand All @@ -81,7 +84,7 @@ extern void event_touch_wb_to_auto_cb(lv_event_t * e);
extern void event_touch_select_delay_cb(lv_event_t * e);
extern void event_touch_select_resolution_cb(lv_event_t * e);
extern void event_touch_big_img_cb(lv_event_t * e);

extern void event_touch_focus_cb(lv_event_t * e);
static void priv_init(void)
{
priv.max_w = 552;
Expand Down Expand Up @@ -125,7 +128,8 @@ static void left_screen_init(void)
lv_obj_set_style_border_side(scr, LV_BORDER_SIDE_NONE, 0);
lv_obj_set_style_radius(scr, 0, 0);
lv_obj_set_style_bg_color(scr, lv_color_hex(0x000000), 0);
lv_obj_remove_flag(scr, LV_OBJ_FLAG_SCROLLABLE);
// lv_obj_remove_flag(scr, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_scrollbar_mode(scr, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_style_pad_hor(scr, 1, 0);

lv_obj_t *img;
Expand Down Expand Up @@ -201,6 +205,24 @@ static void left_screen_init(void)
lv_image_set_src(img, &img_option);
lv_obj_center(img);
}

{
lv_obj_t *obj = lv_obj_create(scr);
lv_obj_set_size(obj, lv_pct(100), lv_pct(25));
lv_obj_set_pos(obj, 0, lv_pct(100));
lv_obj_set_style_bg_color(obj, lv_color_hex(0), 0);
lv_obj_set_style_bg_color(obj, lv_color_hex(0x2e2e2e), LV_STATE_CHECKED);
lv_obj_set_style_border_side(obj, LV_BORDER_SIDE_NONE, 0);
lv_obj_set_style_radius(obj, 0, 0);
lv_obj_remove_flag(obj, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(obj, event_touch_focus_cb, LV_EVENT_CLICKED, NULL);
g_focus_button = obj;

img = lv_image_create(obj);
lv_image_set_src(img, &img_focus);
lv_obj_center(img);
}
}


Expand Down Expand Up @@ -982,6 +1004,41 @@ static void screen_wb_init(void)
}
}

void ui_show_center_image(uint8_t *data, int data_size, int width, int height)
{
if (data_size != width * height * 3) {
printf("data_size != width * height * 3\n");
return;
}

static lv_obj_t *img = NULL;
static lv_img_dsc_t img_dsc;
if (img != NULL) {
lv_obj_delete(img);
img = NULL;
if (img_dsc.data) {
free(img_dsc.data);
img_dsc.data = NULL;
}
}

img = lv_image_create(lv_layer_top());
memset(&img_dsc, 0, sizeof(lv_img_dsc_t));
img_dsc.header.cf = LV_COLOR_FORMAT_RGB888;
img_dsc.header.w = width;
img_dsc.header.h = height;
img_dsc.data_size = img_dsc.header.w * img_dsc.header.h * 3;
img_dsc.data = malloc(img_dsc.data_size);
if (!img_dsc.data) {
printf("malloc failed\n");
return;
}
memcpy(img_dsc.data, data, img_dsc.data_size);
lv_img_set_src(img, &img_dsc);
lv_obj_center(img);
}


static void screen_big_image(void)
{
lv_obj_t *scr = lv_obj_create(lv_scr_act());
Expand Down
2 changes: 1 addition & 1 deletion projects/app_camera/main/app/ui_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void ui_set_wb_value(int val);
void ui_update_small_img(void *data, int data_size);
void ui_refresh(void);
void ui_set_select_option(int idx);

void ui_show_center_image(uint8_t *data, int data_size, int width, int height);
#ifdef __cplusplus
} /*extern "C"*/
#endif
Expand Down
Loading

0 comments on commit 42a2680

Please sign in to comment.