Merge branch 'bugfix/read_from_sdcard' into 'master'

bugfix: Read model data from SD card

See merge request speech-recognition-framework/esp-sr!84
This commit is contained in:
Sun Xiang Yu 2024-01-08 15:47:15 +08:00
commit 6d33cb3bc2
14 changed files with 46 additions and 69 deletions

View File

@ -2,14 +2,14 @@ menu "ESP Speech Recognition"
choice MODEL_DATA_PATH
prompt "model data path"
default MODEL_IN_SPIFFS
default MODEL_IN_FLASH
depends on IDF_TARGET_ESP32S3
config MODEL_IN_SPIFFS
bool "spiffs partition"
config MODEL_IN_FLASH
bool "Read model data from flash"
config MODEL_IN_SDCARD
bool "SD Card"
bool "Read model data from SD Card"
endchoice

View File

@ -15,7 +15,7 @@ To use our models in your project, you need to flash these models. Currently, ES
ESP32-S3:
- Load directly from SIP Flash File System (SPIFFS)
- Load directly from SIP Flash File System (flash)
- Load from external SD card
So that on ESP32-S3 you can:
@ -42,10 +42,10 @@ Run ``idf.py menuconfig`` to navigate to ``ESP Speech Recognition``:
Model Data Path
~~~~~~~~~~~~~~~
This option indicates the storage location of the model data: ``spiffs partition`` or ``SD Card``.
This option indicates the storage location of the model data: ``Read model data from flash`` or ``Read model data from SD card``.
- ``spiffs partition`` means that the model data is stored in the SPIFFS partition, and the model data will be loaded from the SPIFFS partition
- ``SD Card`` means that the model data is stored in the SD card, and the model data will be loaded from the SD card
- ``Read model data from flash`` means that the model data is stored in the flash, and the model data will be loaded from the flash partition
- ``Read model data from SD card`` means that the model data is stored in the SD card, and the model data will be loaded from the SD card
Use AFE
~~~~~~~
@ -135,9 +135,9 @@ Here, we only introduce the code implementation, which can also be found in `mod
.. only:: esp32s3
ESP32-S3 can load model data from SPIFFS or SD card.
ESP32-S3 can load model data from flash or SD card.
Load Model Data from SPIFFS
Load Model Data from flash
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#. Write a partition table:
@ -148,46 +148,29 @@ Load Model Data from SPIFFS
Among them, ``SIZE`` can refer to the recommended size when the user uses ``idf.py build`` to compile, for example: ``Recommended model partition size: 500K``
#. Initialize the SPIFFS partition: User can use ``esp_srmodel_init()`` API to initialize SPIFFS and return all loaded models.
#. Initialize the flash partition: User can use ``esp_srmodel_init(partition_label)`` API to initialize flash and return all loaded models.
- base_path: The model storage ``base_path`` is ``srmodel`` and cannot be changed
- partition_label: The partition label of the model is ``model``, which needs to be consistent with the ``Name`` in the above partition table
After completing the above configuration, the project will automatically generate ``model.bin`` after the project is compiled, and flash it to the SPIFFS partition.
After completing the above configuration, the project will automatically generate ``model.bin`` after the project is compiled, and flash it to the flash partition.
.. only:: esp32s3
Load Model Data from SD Card
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When configured to load model data from ``SD Card``, users need to:
When configured to load model data from ``Read model data from SD card``, users need to:
- Manually load model data from SD card
After the above-mentioned configuration, users can compile the code, and copy the files in ``model/target`` to the root directory of the SD card.
- Customized path
Users can also use customized path by configuring the :cpp:func:`get_model_base_path()` of ``model/model_path.c``.
.. only:: html
For example, users can configure the customized path to the ``espmodel`` in the SD card:
::
char *get_model_base_path(void)
{
#if defined CONFIG_MODEL_IN_SDCARD
return "sdcard/espmodel";
#elif defined CONFIG_MODEL_IN_SPIFFS
return "srmodel";
#else
return NULL;
#endif
}
- Initialize SD card
Users must initialize SD card so the chip can load SD card. Users of `ESP-Skainet <https://github.com/espressif/esp-skainet>`_ can call ``esp_sdcard_init("/sdcard", num);`` to initialize any board supported SD cards. Otherwise, users need to write the initialization code themselves.
After the above-mentioned steps, users can flash the project.
- Read models
User use ``esp_srmodel_init(model_path)`` to read models in ``model_path`` of SD card.
.. |select wake wake| image:: ../../_static/wn_menu1.png
@ -202,10 +185,11 @@ After completing the above configuration, the project will automatically generat
::
//
// step1: initialize SPIFFS and return models in SPIFFS
//
srmodel_list_t *models = esp_srmodel_init("model");
//
// step1: return models in flash or in sdcard
//
char *model_path = your_model_path: // partition_label or model_path in sdcard;
models = esp_srmodel_init(model_path);
//
// step2: select the specific model by keywords

View File

@ -15,7 +15,7 @@
ESP32-S3
- 从 SPI 闪存文件系统 (SPIFFS) 分区加载
- 从 SPI 闪存(flash)文件系统分区加载
- 从外部 SD 卡加载
因此具有以下优势:
@ -42,9 +42,9 @@
Model Data Path
~~~~~~~~~~~~~~~
该选项表示模型数据的存储位置,支持选择 ``spiffs partition````SD Card``
该选项表示模型数据的存储位置,支持选择 ``Read model data from flash````Read model data from SD Card``
- ``spiffs partition`` 表示模型数据存储在 SPIFFS 分区中,模型数据将会从 SPIFFS 分区中加载
- ``Read model data from flash`` 表示模型数据存储在 flash 分区中,模型数据将会从 flash 分区中加载
- ``SD Card`` 表示模型数据存储在 SD 卡中,模型数据将会从 SD 卡中加载
使用 AFE
@ -135,25 +135,24 @@
.. only:: esp32s3
ESP32-S3 支持从 Flash SPIFFS 或 SD 卡中直接加载模型数据,下方将分别介绍。
ESP32-S3 支持从 Flash 或 SD 卡中直接加载模型数据,下方将分别介绍。
模型数据存储在 Flash SPIFFS
模型数据存储在 Flash
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#. 编写分区表:
::
model, data, spiffs, , SIZE,
model, data, data, , SIZE,
其中 SIZE 可以参考在用户使用 ``idf.py build`` 编译时的推荐大小,例如: ``Recommended model partition size: 500K``
#. 初始化 SPIFFS 分区:用户可以直接调用提供的 ``esp_srmodel_init()`` API 来初始化 SPIFFS并返回 SPIFFS 中的模型。
#. 初始化 partition 分区:用户可以直接调用提供的 ``esp_srmodel_init(partition_label)`` API 来获取 partition 中的模型。
- base_path模型的存储 ``base_path````srmodel``,不可更改
- partition_label模型的分区 label 为 ``model`` ,需要和上述分区表中的 ``Name`` 保持一致
- partition_label为partition table 中定义的模型的分区,需要和上述函数的入参保持一致
完成上述配置后,模型会在工程编译完成后自动生成 ``model.bin`` ,并在用户调用 ``idf.py flash`` 时烧写到 SPIFFS 分区。
完成上述配置后,模型会在工程编译完成后自动生成 ``srmodels.bin`` ,并在用户调用 ``idf.py flash`` 时烧写到指定 分区。
.. only:: esp32s3
@ -165,30 +164,13 @@
- 手动移动模型数据至 SD 卡中
用户完成以上配置后,可以先进行编译,编译完成后将 ``model/target`` 目录下的文件拷贝至 SD 卡的根目录。
- 自定义路径
如果用户想将模型放置于指定文件夹,可以自己修改位于 ``model/model_path.c`` 中的 :cpp:func:`get_model_base_path()` 函数。
.. only:: html
比如,如需指定文件夹为 SD 卡目录中的 ``espmodel`` 则可以修改该函数为:
::
char *get_model_base_path(void)
{
#if defined CONFIG_MODEL_IN_SDCARD
return "sdcard/espmodel";
#elif defined CONFIG_MODEL_IN_SPIFFS
return "srmodel";
#else
return NULL;
#endif
}
- 初始化 SD 卡
用户需要初始化 SD 卡,来使系统能够记载 SD 卡。如果用户使用 `ESP-Skainet <https://github.com/espressif/esp-skainet>`_ ,可以直接调用 ``esp_sdcard_init("/sdcard", num);`` 来初始化其支持开发板的 SD 卡。否则,需要自己编写初始化程序。
完成以上操作后,便可以进行工程的烧录。
- 自定义路径
使用``esp_srmodel_init(model_path)``来获取sdcard指定路径``esp_srmodel_init(partition_label)``中的所有model name。
.. |select wake wake| image:: ../../_static/wn_menu1.png
.. |multi wake wake| image:: ../../_static/wn_menu2.png
@ -203,9 +185,10 @@
::
//
// step1: initialize SPIFFS and return models in SPIFFS
// step1: return models in flash
//
srmodel_list_t *models = esp_srmodel_init();
char *model_path = your_model_path: // partition_label or model_path in sdcard;
models = esp_srmodel_init(model_path);
//
// step2: select the specific model by keywords

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -404,12 +404,15 @@ srmodel_list_t *srmodel_sdcard_init(const char *base_path)
struct dirent *ret;
DIR *dir = NULL;
printf("Opening directory: %s\n", base_path);
dir = opendir(base_path);
printf("Directory opened\n");
int model_num = 0;
int idx = 0;
FILE *fp;
if (dir != NULL) {
printf("Directory opened\n");
// get the number of models
while ((ret = readdir(dir)) != NULL) {
// NULL if reach the end of directory
@ -491,6 +494,12 @@ srmodel_list_t *esp_srmodel_init(const char *partition_label)
#ifdef CONFIG_IDF_TARGET_ESP32
return srmodel_config_init();
#else
#ifdef CONFIG_MODEL_IN_SDCARD
// Read model data from SD card
return srmodel_sdcard_init(partition_label);
#else
// Read model data from flash partition
const esp_partition_t *partition = NULL;
// find spiffs partition
partition = esp_partition_find_first(
@ -506,6 +515,7 @@ srmodel_list_t *esp_srmodel_init(const char *partition_label)
return NULL;
#endif
#endif
#else
return srmodel_sdcard_init(partition_label);
#endif