blob: 044c957582726db67e7d6130ba89f0e019c180e3 [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
51void main_loop(hal_platform& platform)
52{
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
Isabella Gottardi8df12f32021-04-07 17:15:31 +010064 arm::app::Profiler profiler{&platform, "kws"};
65 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
66
alexander3c798932021-03-26 21:42:19 +000067 caseContext.Set<hal_platform&>("platform", platform);
68 caseContext.Set<arm::app::Model&>("model", model);
69 caseContext.Set<uint32_t>("clipIndex", 0);
70 caseContext.Set<int>("frameLength", g_FrameLength);
71 caseContext.Set<int>("frameStride", g_FrameStride);
72 caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); /* Normalised score threshold. */
73
74 KwsClassifier classifier; /* classifier wrapper object. */
75 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
76
77 std::vector <std::string> labels;
78 GetLabelsVector(labels);
79
80 caseContext.Set<const std::vector <std::string>&>("labels", labels);
81
82 bool executionSuccessful = true;
83 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
84
85 /* Loop. */
86 do {
87 int menuOption = MENU_OPT_RUN_INF_NEXT;
88 if (bUseMenu) {
89 DisplayMenu();
90 menuOption = arm::app::ReadUserInputAsInt(platform);
91 printf("\n");
92 }
93 switch (menuOption) {
94 case MENU_OPT_RUN_INF_NEXT:
95 executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false);
96 break;
97 case MENU_OPT_RUN_INF_CHOSEN: {
98 printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010099 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000100 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform));
101 executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false);
102 break;
103 }
104 case MENU_OPT_RUN_INF_ALL:
105 executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), true);
106 break;
107 case MENU_OPT_SHOW_MODEL_INFO:
108 executionSuccessful = model.ShowModelInfoHandler();
109 break;
110 case MENU_OPT_LIST_AUDIO_CLIPS:
111 executionSuccessful = ListFilesHandler(caseContext);
112 break;
113 default:
114 printf("Incorrect choice, try again.");
115 break;
116 }
117 } while (executionSuccessful && bUseMenu);
118 info("Main loop terminated.\n");
119}