blob: e590c4a1ee2e13d000ef72265f484811f0a103c6 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "InputFiles.hpp" /* For input audio clips. */
18#include "Classifier.hpp" /* Classifier. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000019#include "MicroNetKwsModel.hpp" /* Model class for running inference. */
alexander3c798932021-03-26 21:42:19 +000020#include "hal.h" /* Brings in platform definitions. */
21#include "Labels.hpp" /* For label strings. */
22#include "UseCaseHandler.hpp" /* Handlers for different user options. */
23#include "UseCaseCommonUtils.hpp" /* Utils functions. */
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000025
26using KwsClassifier = arm::app::Classifier;
27
28enum opcodes
29{
30 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
31 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
32 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
33 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
34 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */
35};
36
37static void DisplayMenu()
38{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010039 printf("\n\n");
40 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000041 printf("Enter option number from:\n\n");
42 printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT);
43 printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
44 printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL);
45 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
46 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
47 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010048 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000049}
50
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010051void main_loop()
alexander3c798932021-03-26 21:42:19 +000052{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000053 arm::app::MicroNetKwsModel model; /* Model wrapper object. */
alexander3c798932021-03-26 21:42:19 +000054
55 /* Load the model. */
56 if (!model.Init()) {
57 printf_err("Failed to initialise model\n");
58 return;
59 }
60
61 /* Instantiate application context. */
62 arm::app::ApplicationContext caseContext;
63
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010064 arm::app::Profiler profiler{"kws"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010065 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000066 caseContext.Set<arm::app::Model&>("model", model);
67 caseContext.Set<uint32_t>("clipIndex", 0);
68 caseContext.Set<int>("frameLength", g_FrameLength);
69 caseContext.Set<int>("frameStride", g_FrameStride);
70 caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); /* Normalised score threshold. */
71
72 KwsClassifier classifier; /* classifier wrapper object. */
73 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
74
75 std::vector <std::string> labels;
76 GetLabelsVector(labels);
77
78 caseContext.Set<const std::vector <std::string>&>("labels", labels);
79
80 bool executionSuccessful = true;
81 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
82
83 /* Loop. */
84 do {
85 int menuOption = MENU_OPT_RUN_INF_NEXT;
86 if (bUseMenu) {
87 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010088 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000089 printf("\n");
90 }
91 switch (menuOption) {
92 case MENU_OPT_RUN_INF_NEXT:
93 executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false);
94 break;
95 case MENU_OPT_RUN_INF_CHOSEN: {
96 printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010097 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010098 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +000099 executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false);
100 break;
101 }
102 case MENU_OPT_RUN_INF_ALL:
103 executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), true);
104 break;
105 case MENU_OPT_SHOW_MODEL_INFO:
106 executionSuccessful = model.ShowModelInfoHandler();
107 break;
108 case MENU_OPT_LIST_AUDIO_CLIPS:
109 executionSuccessful = ListFilesHandler(caseContext);
110 break;
111 default:
112 printf("Incorrect choice, try again.");
113 break;
114 }
115 } while (executionSuccessful && bUseMenu);
116 info("Main loop terminated.\n");
117}