mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Change descriptor functions to accept file pointer.
This commit is contained in:
parent
acafef5207
commit
154cb5be69
@ -388,21 +388,15 @@ int freak_match_keypoints(array_t *kpts1, array_t *kpts2, int threshold)
|
||||
return matches;
|
||||
}
|
||||
|
||||
int freak_save_descriptor(array_t *kpts, const char *path)
|
||||
int freak_save_descriptor(FIL *fp, array_t *kpts)
|
||||
{
|
||||
FIL fp;
|
||||
UINT bytes;
|
||||
FRESULT res;
|
||||
|
||||
int kpts_size = array_length(kpts);
|
||||
|
||||
res = f_open(&fp, path, FA_WRITE|FA_CREATE_ALWAYS);
|
||||
if (res != FR_OK) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Write the number of keypoints
|
||||
res = f_write(&fp, &kpts_size, sizeof(kpts_size), &bytes);
|
||||
res = f_write(fp, &kpts_size, sizeof(kpts_size), &bytes);
|
||||
if (res != FR_OK || bytes != sizeof(kpts_size)) {
|
||||
goto error;
|
||||
}
|
||||
@ -412,44 +406,37 @@ int freak_save_descriptor(array_t *kpts, const char *path)
|
||||
kp_t *kp = array_at(kpts, i);
|
||||
|
||||
// Write X
|
||||
res = f_write(&fp, &kp->x, sizeof(kp->x), &bytes);
|
||||
res = f_write(fp, &kp->x, sizeof(kp->x), &bytes);
|
||||
if (res != FR_OK || bytes != sizeof(kp->x)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Write Y
|
||||
res = f_write(&fp, &kp->y, sizeof(kp->y), &bytes);
|
||||
res = f_write(fp, &kp->y, sizeof(kp->y), &bytes);
|
||||
if (res != FR_OK || bytes != sizeof(kp->y)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Write descriptor
|
||||
res = f_write(&fp, kp->desc, KPT_DESC_SIZE, &bytes);
|
||||
res = f_write(fp, kp->desc, KPT_DESC_SIZE, &bytes);
|
||||
if (res != FR_OK || bytes != KPT_DESC_SIZE) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
error:
|
||||
f_close(&fp);
|
||||
return res;
|
||||
}
|
||||
|
||||
int freak_load_descriptor(array_t *kpts, const char *path)
|
||||
int freak_load_descriptor(FIL *fp, array_t *kpts)
|
||||
{
|
||||
FIL fp;
|
||||
UINT bytes;
|
||||
FRESULT res=FR_OK;
|
||||
|
||||
int kpts_size=0;
|
||||
|
||||
res = f_open(&fp, path, FA_READ|FA_OPEN_EXISTING);
|
||||
if (res != FR_OK) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Read number of keypoints
|
||||
res = f_read(&fp, &kpts_size, sizeof(kpts_size), &bytes);
|
||||
res = f_read(fp, &kpts_size, sizeof(kpts_size), &bytes);
|
||||
if (res != FR_OK || bytes != sizeof(kpts_size)) {
|
||||
goto error;
|
||||
}
|
||||
@ -459,19 +446,19 @@ int freak_load_descriptor(array_t *kpts, const char *path)
|
||||
kp_t *kp = xalloc(sizeof(*kp));
|
||||
|
||||
// Read X
|
||||
res = f_read(&fp, &kp->x, sizeof(kp->x), &bytes);
|
||||
res = f_read(fp, &kp->x, sizeof(kp->x), &bytes);
|
||||
if (res != FR_OK || bytes != sizeof(kp->x)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Read Y
|
||||
res = f_read(&fp, &kp->y, sizeof(kp->y), &bytes);
|
||||
res = f_read(fp, &kp->y, sizeof(kp->y), &bytes);
|
||||
if (res != FR_OK || bytes != sizeof(kp->y)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Read descriptor
|
||||
res = f_read(&fp, kp->desc, KPT_DESC_SIZE, &bytes);
|
||||
res = f_read(fp, kp->desc, KPT_DESC_SIZE, &bytes);
|
||||
if (res != FR_OK || bytes != KPT_DESC_SIZE) {
|
||||
goto error;
|
||||
}
|
||||
@ -481,6 +468,5 @@ int freak_load_descriptor(array_t *kpts, const char *path)
|
||||
}
|
||||
|
||||
error:
|
||||
f_close(&fp);
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -428,15 +428,15 @@ array_t *imlib_detect_objects(struct image *image, struct cascade *cascade, stru
|
||||
void fast_detect(image_t *image, array_t *keypoints, int threshold, rectangle_t *roi);
|
||||
array_t *freak_find_keypoints(image_t *image, bool normalized, int threshold, rectangle_t *roi);
|
||||
int freak_match_keypoints(array_t *kpts1, array_t *kpts2, int threshold);
|
||||
int freak_save_descriptor(array_t *kpts, const char *path);
|
||||
int freak_load_descriptor(array_t *kpts, const char *path);
|
||||
int freak_save_descriptor(FIL *fp, array_t *kpts);
|
||||
int freak_load_descriptor(FIL *fp, array_t *kpts);
|
||||
|
||||
/* LBP Operator */
|
||||
void imlib_lbp_desc(image_t *image, int div, uint8_t *desc, rectangle_t *roi);
|
||||
uint8_t *imlib_lbp_cascade(image_t *image, rectangle_t *roi);
|
||||
int imlib_lbp_desc_distance(uint8_t *d0, uint8_t *d1);
|
||||
int imlib_lbp_desc_save(const char *path, uint8_t *desc);
|
||||
int imlib_lbp_desc_load(const char *path, uint8_t **desc);
|
||||
int imlib_lbp_desc_save(FIL *fp, uint8_t *desc);
|
||||
int imlib_lbp_desc_load(FIL *fp, uint8_t **desc);
|
||||
|
||||
/* Iris detector */
|
||||
void imlib_find_iris(image_t *src, point_t *iris, rectangle_t *roi);
|
||||
|
||||
@ -147,42 +147,23 @@ int imlib_lbp_desc_distance(uint8_t *d0, uint8_t *d1)
|
||||
}
|
||||
#endif
|
||||
|
||||
int imlib_lbp_desc_save(const char *path, uint8_t *desc)
|
||||
int imlib_lbp_desc_save(FIL *fp, uint8_t *desc)
|
||||
{
|
||||
FIL fp;
|
||||
UINT bytes;
|
||||
FRESULT res=FR_OK;
|
||||
|
||||
// Open LBP file
|
||||
res = f_open(&fp, path, FA_WRITE|FA_CREATE_ALWAYS);
|
||||
if (res != FR_OK) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Write descriptor
|
||||
res = f_write(&fp, desc, LBP_DESC_SIZE, &bytes);
|
||||
|
||||
f_close(&fp);
|
||||
return res;
|
||||
return f_write(fp, desc, LBP_DESC_SIZE, &bytes);
|
||||
}
|
||||
|
||||
int imlib_lbp_desc_load(const char *path, uint8_t **desc)
|
||||
int imlib_lbp_desc_load(FIL *fp, uint8_t **desc)
|
||||
{
|
||||
FIL fp;
|
||||
UINT bytes;
|
||||
FRESULT res=FR_OK;
|
||||
|
||||
*desc = NULL;
|
||||
uint8_t *hist = xalloc(LBP_DESC_SIZE);
|
||||
|
||||
// Open LBP file
|
||||
res = f_open(&fp, path, FA_READ|FA_OPEN_EXISTING);
|
||||
if (res != FR_OK) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// Read descriptor
|
||||
res = f_read(&fp, hist, LBP_DESC_SIZE, &bytes);
|
||||
res = f_read(fp, hist, LBP_DESC_SIZE, &bytes);
|
||||
if (res != FR_OK || bytes != LBP_DESC_SIZE) {
|
||||
*desc = NULL;
|
||||
xfree(hist);
|
||||
@ -190,6 +171,5 @@ int imlib_lbp_desc_load(const char *path, uint8_t **desc)
|
||||
*desc = hist;
|
||||
}
|
||||
|
||||
f_close(&fp);
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -1278,12 +1278,20 @@ mp_obj_t py_image_from_struct(image_t *image)
|
||||
|
||||
int py_image_descriptor_from_roi(image_t *image, const char *path, rectangle_t *roi)
|
||||
{
|
||||
FIL fp;
|
||||
FRESULT res = FR_OK;
|
||||
|
||||
array_t *kpts = freak_find_keypoints(image, false, 10, roi);
|
||||
printf("Save Descriptor: KPTS(%d)\n", array_length(kpts));
|
||||
printf("Save Descriptor: ROI(%d %d %d %d)\n", roi->x, roi->y, roi->w, roi->h);
|
||||
|
||||
if (array_length(kpts)) {
|
||||
int res = freak_save_descriptor(kpts, path);
|
||||
if ((res = f_open(&fp, path, FA_WRITE|FA_CREATE_ALWAYS)) == FR_OK) {
|
||||
res = freak_save_descriptor(&fp, kpts);
|
||||
f_close(&fp);
|
||||
}
|
||||
|
||||
// File open/write error
|
||||
if (res != FR_OK) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
|
||||
}
|
||||
@ -1335,84 +1343,94 @@ mp_obj_t py_image_load_cascade(uint n_args, const mp_obj_t *args, mp_map_t *kw_a
|
||||
|
||||
mp_obj_t py_image_load_descriptor(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
||||
{
|
||||
int res = FR_OK;
|
||||
FIL fp;
|
||||
FRESULT res;
|
||||
|
||||
mp_obj_t desc = mp_const_none;
|
||||
descriptor_t desc_type = mp_obj_get_int(args[0]);
|
||||
const char *path = mp_obj_str_get_str(args[1]);
|
||||
|
||||
switch (desc_type) {
|
||||
case DESC_LBP: {
|
||||
py_lbp_obj_t *lbp = m_new_obj(py_lbp_obj_t);
|
||||
lbp->base.type = &py_lbp_type;
|
||||
if ((res = f_open(&fp, path, FA_READ|FA_OPEN_EXISTING)) == FR_OK) {
|
||||
switch (desc_type) {
|
||||
case DESC_LBP: {
|
||||
py_lbp_obj_t *lbp = m_new_obj(py_lbp_obj_t);
|
||||
lbp->base.type = &py_lbp_type;
|
||||
|
||||
res = imlib_lbp_desc_load(path, &lbp->hist);
|
||||
if (res != FR_OK) {
|
||||
break;
|
||||
}
|
||||
desc = lbp;
|
||||
break;
|
||||
}
|
||||
|
||||
case DESC_FREAK: {
|
||||
array_t *kpts = NULL;
|
||||
array_alloc(&kpts, xfree);
|
||||
|
||||
res = freak_load_descriptor(kpts, path);
|
||||
if (res != FR_OK) {
|
||||
res = imlib_lbp_desc_load(&fp, &lbp->hist);
|
||||
if (res == FR_OK) {
|
||||
desc = lbp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Return keypoints MP object
|
||||
py_kp_obj_t *kp_obj = m_new_obj(py_kp_obj_t);
|
||||
kp_obj->base.type = &py_kp_type;
|
||||
kp_obj->kpts = kpts;
|
||||
kp_obj->threshold = 10;
|
||||
kp_obj->normalized = false;
|
||||
desc = kp_obj;
|
||||
break;
|
||||
case DESC_FREAK: {
|
||||
array_t *kpts = NULL;
|
||||
array_alloc(&kpts, xfree);
|
||||
|
||||
res = freak_load_descriptor(&fp, kpts);
|
||||
if (res == FR_OK) {
|
||||
// Return keypoints MP object
|
||||
py_kp_obj_t *kp_obj = m_new_obj(py_kp_obj_t);
|
||||
kp_obj->base.type = &py_kp_type;
|
||||
kp_obj->kpts = kpts;
|
||||
kp_obj->threshold = 10;
|
||||
kp_obj->normalized = false;
|
||||
desc = kp_obj;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
default: {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Descriptor type is not supported"));
|
||||
}
|
||||
f_close(&fp);
|
||||
}
|
||||
|
||||
// File open or write error
|
||||
if (res != FR_OK) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
|
||||
}
|
||||
|
||||
// If no file error and descriptor is still none, then it's not supported.
|
||||
if (desc == mp_const_none) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Descriptor type is not supported"));
|
||||
}
|
||||
return desc;
|
||||
}
|
||||
|
||||
mp_obj_t py_image_save_descriptor(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
||||
{
|
||||
int res = FR_OK;
|
||||
FIL fp;
|
||||
FRESULT res;
|
||||
|
||||
descriptor_t desc_type = mp_obj_get_int(args[0]);
|
||||
const char *path = mp_obj_str_get_str(args[1]);
|
||||
|
||||
switch (desc_type) {
|
||||
case DESC_LBP: {
|
||||
py_lbp_obj_t *lbp = ((py_lbp_obj_t*)args[2]);
|
||||
res = imlib_lbp_desc_save(path, lbp->hist);
|
||||
break;
|
||||
}
|
||||
if ((res = f_open(&fp, path, FA_WRITE|FA_CREATE_ALWAYS)) == FR_OK) {
|
||||
switch (desc_type) {
|
||||
case DESC_LBP: {
|
||||
py_lbp_obj_t *lbp = ((py_lbp_obj_t*)args[2]);
|
||||
res = imlib_lbp_desc_save(&fp, lbp->hist);
|
||||
break;
|
||||
}
|
||||
|
||||
case DESC_FREAK: {
|
||||
py_kp_obj_t *kpts = ((py_kp_obj_t*)args[2]);
|
||||
res = freak_save_descriptor(kpts->kpts, path);
|
||||
break;
|
||||
case DESC_FREAK: {
|
||||
py_kp_obj_t *kpts = ((py_kp_obj_t*)args[2]);
|
||||
res = freak_save_descriptor(&fp, kpts->kpts);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// ignore unsupported descriptors when saving
|
||||
|
||||
default: {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Descriptor type is not supported"));
|
||||
}
|
||||
f_close(&fp);
|
||||
}
|
||||
|
||||
// File open or read error
|
||||
if (res != FR_OK) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
|
||||
}
|
||||
return mp_const_true;
|
||||
}
|
||||
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_load_image_obj, py_image_load_image);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_load_cascade_obj, 1, py_image_load_cascade);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_load_descriptor_obj, 2, py_image_load_descriptor);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user