forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/imagimob: Adding imagimob mp integration.
Signed-off-by: NikhitaR-IFX <Nikhita.Rajasekhar@infineon.com>
- Loading branch information
1 parent
cb8a144
commit 85983a1
Showing
8 changed files
with
4,127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# MicroPython ImagiMob Integration for Infineon's PSoC6 port | ||
|
||
This directory contains the imagimob generated model files and the interface for it's enablement in micropython. To access your edge ai models developed for PSoC6 based kit from a micropython application, please follow the description below. | ||
|
||
## Folder structure | ||
|
||
**model.c** : Imagimob generated source code in C for your developed model in the studio. | ||
|
||
**model.h** : Imagimob generated source code in C for your developed model in the studio. | ||
|
||
**imai_mp_iface.c** : MicroPython interface for ImagiMob models. | ||
|
||
**micropython.mk** : Makefile configurations for firmware generation. | ||
|
||
## Installation | ||
|
||
Please check for the pre-requisites mentioned [here](../../ports/psoc6/README.md#Pre-requisites). | ||
|
||
## Usage | ||
|
||
Follow the steps below to generate micropython bindings for your imagimob generated model. | ||
|
||
1. The imagimob micropytthon integration is available in imai-mp-integration, so checkout to that branch after cloning this repository. | ||
|
||
git clone https://github.com/Infineon/micropython.git | ||
|
||
git checkout --track origin/imai-mp-integration | ||
|
||
2. Then initialize the ModusToolbox™ environment: | ||
|
||
make mtb_init BOARD=<board-name> | ||
|
||
3. Retrieve submodules: | ||
|
||
make submodules | ||
|
||
4. In the examples/imagimob/ folder, replace the **model.c** and **model.h** files by your imagimob generated model files. | ||
|
||
5. Compile it alongwith psoc6 port source code and generate the final .hex to be flashed into your device. From your root: | ||
|
||
cd ports/psoc6 | ||
make program | ||
|
||
# Run micropython | ||
|
||
Use any micropython supported IDE (like Thonny) and establish a session with your device. Now you should be able to import your model as shown below: | ||
|
||
import imai as model | ||
|
||
model.init() # Initialize the model | ||
|
||
model.enqueue(data_in) # Send the data inputs for the model | ||
|
||
model.dequeue(data_out) # Get the classifications | ||
|
||
**Note**: Above is only an abstract code to show the usage in micropython. Please take care of populating the data-in and data_out with right dimensions and values. The provided api's abide by its operation in imagimob. Please refer to API documentation [here](https://developer.imagimob.com/edge-optimization/edge-api). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Include MicroPython API. | ||
#include "py/runtime.h" | ||
|
||
#include "py/mphal.h" | ||
#include "model.h" | ||
|
||
static mp_obj_t init(void){ | ||
IMAI_init(); | ||
return mp_const_none; | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_0(init_obj, init); | ||
|
||
static mp_obj_t enqueue(const mp_obj_t data_in_obj){ | ||
float data_in[IMAI_DATA_IN_COUNT]; | ||
mp_obj_t *data_in_items; | ||
size_t len; | ||
mp_obj_get_array(data_in_obj, &len, &data_in_items); | ||
if (len != IMAI_DATA_IN_COUNT) { | ||
mp_raise_ValueError("data_in must be a list of IMAI_DATA_IN_COUNT floats"); | ||
} | ||
for (int i = 0; i < IMAI_DATA_IN_COUNT; i++) { | ||
data_in[i] = mp_obj_float_get(data_in_items[i]); | ||
} | ||
int result = IMAI_enqueue(data_in); | ||
return MP_OBJ_NEW_SMALL_INT(result); | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_1(enqueue_obj, enqueue); | ||
|
||
static mp_obj_t dequeue(mp_obj_t data_out_obj) { | ||
mp_buffer_info_t buf_info; | ||
mp_get_buffer(data_out_obj, &buf_info, MP_BUFFER_WRITE); | ||
float *data_out = (float *)buf_info.buf; | ||
int result = IMAI_dequeue(data_out); | ||
if (result == 0) { | ||
return MP_OBJ_NEW_SMALL_INT(result); | ||
} else if (result == -1) { | ||
mp_printf(&mp_plat_print, "No data is currently available\n"); | ||
return MP_OBJ_NEW_SMALL_INT(result); | ||
} else if (result == -2) { | ||
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Internal memory allocation error"), result); | ||
} | ||
return MP_OBJ_NEW_SMALL_INT(result); | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_1(dequeue_obj, dequeue); | ||
|
||
static const mp_rom_map_elem_t example_module_globals_table[] = { | ||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_imai) }, | ||
|
||
// imagimob fns | ||
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&init_obj) }, | ||
{ MP_ROM_QSTR(MP_QSTR_enqueue), MP_ROM_PTR(&enqueue_obj) }, | ||
{ MP_ROM_QSTR(MP_QSTR_dequeue), MP_ROM_PTR(&dequeue_obj) }, | ||
|
||
// constants | ||
{ MP_ROM_QSTR(MP_QSTR_DATA_IN), MP_ROM_INT(IMAI_DATA_IN_COUNT) }, | ||
{ MP_ROM_QSTR(MP_QSTR_DATA_OUT), MP_ROM_INT(IMAI_DATA_OUT_COUNT) }, | ||
}; | ||
static MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table); | ||
|
||
// Define module object. | ||
const mp_obj_module_t imai_module = { | ||
.base = { &mp_type_module }, | ||
.globals = (mp_obj_dict_t *)&example_module_globals, | ||
}; | ||
|
||
// Register the module to make it available in Python. | ||
#if MICROPY_PY_IMAI | ||
MP_REGISTER_MODULE(MP_QSTR_imai, imai_module); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
IMAI_MOD_DIR := $(USERMOD_DIR) | ||
|
||
# Add all C files to SRC_USERMOD. | ||
SRC_USERMOD += $(IMAI_MOD_DIR)/imai_mp_iface.c $(IMAI_MOD_DIR)/model.c | ||
|
||
# We can add our module folder to include paths if needed | ||
# This is not actually needed in this example. | ||
CFLAGS_USERMOD += -I$(IMAI_MOD_DIR) |
Oops, something went wrong.