static mp_obj_t py_image_find_features(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { struct image *image = NULL; struct cascade *cascade = NULL; struct array *objects_array=NULL; mp_obj_t objects_list = mp_const_none; /* sanity checks */ PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QCIF); PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_GRAYSCALE); /* read arguments */ image = py_image_cobj(args[0]); cascade = py_cascade_cobj(args[1]); /* set some defaults */ cascade->threshold = 0.65f; cascade->scale_factor = 1.65f; /* read kw args */ mp_map_elem_t *kw_thresh = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("threshold")), MP_MAP_LOOKUP); if (kw_thresh != NULL) { cascade->threshold = mp_obj_get_float(kw_thresh->value); } mp_map_elem_t *kw_scalef = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("scale")), MP_MAP_LOOKUP); if (kw_scalef != NULL) { cascade->scale_factor = mp_obj_get_float(kw_scalef->value); } /* Detect objects */ objects_array = imlib_detect_objects(image, cascade); /* Create empty Python list */ objects_list = mp_obj_new_list(0, NULL); /* Add detected objects to the list */ for (int i=0; i<array_length(objects_array); i++) { struct rectangle *r = array_at(objects_array, 0); mp_obj_t rec_obj[4] = { mp_obj_new_int(r->x), mp_obj_new_int(r->y), mp_obj_new_int(r->w), mp_obj_new_int(r->h), }; mp_obj_list_append(objects_list, mp_obj_new_tuple(4, rec_obj)); } /* Free the objects array */ array_free(objects_array); return objects_list; }
mp_obj_t py_imlib_detect_color(mp_obj_t image_obj, mp_obj_t color_obj, mp_obj_t threshold) { /* C stuff */ struct color color; struct rectangle rectangle; struct image *image; /* sanity checks */ PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_RGB565); mp_obj_t *col_obj; col_obj = mp_obj_get_array_fixed_n(color_obj, 3); color.h = mp_obj_get_int(col_obj[0]); color.s = mp_obj_get_int(col_obj[1]); color.v = mp_obj_get_int(col_obj[2]); /* get image pointer */ image = py_image_cobj(image_obj); imlib_detect_color(image, &color, &rectangle, mp_obj_get_int(threshold)); mp_obj_t rec_obj[4]; rec_obj[0] = mp_obj_new_int(rectangle.x); rec_obj[1] = mp_obj_new_int(rectangle.y); rec_obj[2] = mp_obj_new_int(rectangle.w); rec_obj[3] = mp_obj_new_int(rectangle.h); return rt_build_tuple(4, rec_obj); }
static mp_obj_t py_image_binary(mp_obj_t image_obj, mp_obj_t threshold) { image_t *image; /* sanity checks */ PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QCIF); PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_GRAYSCALE); /* read arguments */ image = py_image_cobj(image_obj); int thresh = mp_obj_get_int(threshold); /* Threshold image */ imlib_binary(image, thresh); return mp_const_none; }
static mp_obj_t py_image_draw_keypoints(mp_obj_t image_obj, mp_obj_t kpts_obj) { image_t *image = NULL; py_kp_obj_t *kpts=NULL; /* get pointer */ image = py_image_cobj(image_obj); kpts = (py_kp_obj_t*)kpts_obj; PY_ASSERT_TRUE(image->bpp == 1); PY_ASSERT_TYPE(kpts_obj, &py_kp_type); color_t cl = {.r=0xFF, .g=0xFF, .b=0xFF}; for (int i=0; i<kpts->size; i++) { kp_t *kp = &kpts->kpts[i]; float co = arm_cos_f32(kp->angle); float si = arm_sin_f32(kp->angle); imlib_draw_line(image, kp->x, kp->y, kp->x+(co*10), kp->y+(si*10)); imlib_draw_circle(image, kp->x, kp->y, 4, &cl); } return mp_const_none; } static mp_obj_t py_image_find_blobs(mp_obj_t image_obj) { /* C stuff */ array_t *blobs; struct image *image; mp_obj_t blob_obj[6]; /* MP List */ mp_obj_t objects_list = mp_const_none; /* get image pointer */ image = py_image_cobj(image_obj); /* run color dector */ blobs = imlib_count_blobs(image); /* Create empty Python list */ objects_list = mp_obj_new_list(0, NULL); if (array_length(blobs)) { for (int j=0; j<array_length(blobs); j++) { blob_t *r = array_at(blobs, j); blob_obj[0] = mp_obj_new_int(r->x); blob_obj[1] = mp_obj_new_int(r->y); blob_obj[2] = mp_obj_new_int(r->w); blob_obj[3] = mp_obj_new_int(r->h); blob_obj[4] = mp_obj_new_int(r->c); blob_obj[5] = mp_obj_new_int(r->id); mp_obj_list_append(objects_list, mp_obj_new_tuple(6, blob_obj)); } } array_free(blobs); return objects_list; }
static mp_obj_t py_image_dilate(mp_obj_t image_obj, mp_obj_t ksize_obj) { image_t *image = NULL; image = py_image_cobj(image_obj); /* sanity checks */ PY_ASSERT_TRUE(image->bpp==1); imlib_dilate(image, mp_obj_get_int(ksize_obj)); return mp_const_none; }
static mp_obj_t py_sensor_set_quality(mp_obj_t qs) { int q = mp_obj_get_int(qs); PY_ASSERT_TRUE((q >= 0 && q <= 100)); q = 100-q; //invert quality q = 255*q/100; //map to 0->255 if (sensor_set_quality(q) != 0) { return mp_const_false; } return mp_const_true; }
static mp_obj_t py_image_invert(mp_obj_t image_obj) { image_t *image; image = py_image_cobj(image_obj); /* Sanity checks */ PY_ASSERT_TRUE(image->bpp == 1); /* Threshold image */ imlib_invert(image); return mp_const_none; }
static mp_obj_t py_image_histeq(mp_obj_t image_obj) { struct image *image; /* get image pointer */ image = (struct image*) py_image_cobj(image_obj); /* sanity checks */ PY_ASSERT_TRUE(image->bpp == 1); imlib_histeq(image); return mp_const_none; }
mp_obj_t py_imlib_histeq(mp_obj_t image_obj) { struct image *image; /* get image pointer */ image = (struct image*) py_image_cobj(image_obj); /* sanity checks */ PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_GRAYSCALE); imlib_histeq(image); return mp_const_none; }
mp_obj_t py_imlib_detect_objects(mp_obj_t image_obj, mp_obj_t cascade_obj) { struct image *image = NULL; struct cascade *cascade = NULL; struct array *objects_array=NULL; mp_obj_t objects_list = mp_const_none; /* sanity checks */ PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QQVGA); PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_GRAYSCALE); /* get C image pointer */ image = py_image_cobj(image_obj); /* get C cascade pointer */ cascade = py_cascade_cobj(cascade_obj); /* detect objects */ objects_array = imlib_detect_objects(image, cascade); int size = array_length(objects_array); if (size) { int i; objects_list = rt_build_list(0, NULL); for (i=0; i<size; i++) { struct rectangle *r = array_at(objects_array, 0); mp_obj_t rec_obj[4]; rec_obj[0] = mp_obj_new_int(r->x); rec_obj[1] = mp_obj_new_int(r->y); rec_obj[2] = mp_obj_new_int(r->w); rec_obj[3] = mp_obj_new_int(r->h); rt_list_append(objects_list, rt_build_tuple(4, rec_obj)); } } /* free objects array */ array_free(objects_array); return objects_list; }
static mp_obj_t py_image_threshold(mp_obj_t image_obj, mp_obj_t color_list_obj, mp_obj_t threshold) { color_t *color; image_t *image; /* sanity checks */ PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_RGB565); PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QCIF); /* read arguments */ image = py_image_cobj(image_obj); int thresh = mp_obj_get_int(threshold); /* returned image */ image_t bimage = { .w=image->w, .h=image->h, .bpp=1, .pixels=image->data+(image->w*image->h*image->bpp) }; /* copy color list */ uint len; mp_obj_t *color_arr; mp_obj_get_array(color_list_obj, &len, &color_arr); color = xalloc(len*sizeof*color); for (int i=0; i<len; i++) { mp_obj_t *color_obj; mp_obj_get_array_fixed_n(color_arr[i], 3, &color_obj); color[i].r = mp_obj_get_int(color_obj[0]); color[i].g = mp_obj_get_int(color_obj[1]); color[i].b = mp_obj_get_int(color_obj[2]); } /* Threshold image using reference color */ imlib_threshold(image, &bimage, color, len, thresh); return py_image_from_struct(&bimage); } static mp_obj_t py_image_rainbow(mp_obj_t src_image_obj) { image_t *src_image = NULL; /* get C image pointer */ src_image = py_image_cobj(src_image_obj); /* sanity checks */ PY_ASSERT_TRUE(src_image->bpp==1); image_t dst_image = { .w=src_image->w, .h=src_image->h, .bpp=2, .pixels=xalloc(src_image->w*src_image->h*2) }; imlib_rainbow(src_image, &dst_image); *src_image = dst_image; return src_image_obj; } static mp_obj_t py_image_compress(mp_obj_t image_obj, mp_obj_t quality) { image_t *image = py_image_cobj(image_obj); image_t cimage = { .w=image->w, .h=image->h, .bpp=0, .pixels= NULL }; jpeg_compress(image, &cimage, mp_obj_get_int(quality)); return py_image_from_struct(&cimage); } static mp_obj_t py_image_draw_line(mp_obj_t image_obj, mp_obj_t line_obj) { /* get image pointer */ struct image *image; image = py_image_cobj(image_obj); mp_obj_t *array; mp_obj_get_array_fixed_n(line_obj, 4, &array); int x0 = mp_obj_get_int(array[0]); int y0 = mp_obj_get_int(array[1]); int x1 = mp_obj_get_int(array[2]); int y1 = mp_obj_get_int(array[3]); imlib_draw_line(image, x0, y0, x1, y1); return mp_const_none; } static mp_obj_t py_image_draw_circle(mp_obj_t image_obj, mp_obj_t c_obj, mp_obj_t r_obj) { int cx, cy, r; mp_obj_t *array; struct image *image; color_t c = {.r=0xFF, .g=0xFF, .b=0xFF}; /* get image pointer */ image = py_image_cobj(image_obj); /* center */ mp_obj_get_array_fixed_n(c_obj, 2, &array); cx = mp_obj_get_int(array[0]); cy = mp_obj_get_int(array[1]); /* radius */ r = mp_obj_get_int(r_obj); imlib_draw_circle(image, cx, cy, r, &c); return mp_const_none; } static mp_obj_t py_image_draw_string(uint n_args, const mp_obj_t *args) { int x = mp_obj_get_int(args[1]); int y = mp_obj_get_int(args[2]); image_t *image =py_image_cobj(args[0]); const char *str = mp_obj_str_get_str(args[3]); color_t c = {.r=0xFF, .g=0xFF, .b=0xFF}; if (n_args == 5) { // get color mp_obj_t *array; mp_obj_get_array_fixed_n(args[4], 3, &array); c.r = mp_obj_get_int(array[0]); c.g = mp_obj_get_int(array[1]); c.b = mp_obj_get_int(array[2]); } imlib_draw_string(image, x, y, str, &c); return mp_const_none; } static mp_obj_t py_image_erode(mp_obj_t image_obj, mp_obj_t ksize_obj) { image_t *image = NULL; image = py_image_cobj(image_obj); /* sanity checks */ PY_ASSERT_TRUE(image->bpp==1); imlib_erode(image, mp_obj_get_int(ksize_obj)); return mp_const_none; }