blob: c683e7197d332c20ebe6e93ad4fcc348f4649fc6 [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. */
19#include "DsCnnModel.hpp" /* Model class for running inference. */
20#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. */
24
25using KwsClassifier = arm::app::Classifier;
26
27enum opcodes
28{
29 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
30 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
31 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
32 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
33 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */
34};
35
36static void DisplayMenu()
37{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010038 printf("\n\n");
39 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000040 printf("Enter option number from:\n\n");
41 printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT);
42 printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
43 printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL);
44 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
45 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
46 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010047 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000048}
49
50void main_loop(hal_platform& platform)
51{
52 arm::app::DsCnnModel model; /* Model wrapper object. */
53
54 /* Load the model. */
55 if (!model.Init()) {
56 printf_err("Failed to initialise model\n");
57 return;
58 }
59
60 /* Instantiate application context. */
61 arm::app::ApplicationContext caseContext;
62
Isabella Gottardi8df12f32021-04-07 17:15:31 +010063 arm::app::Profiler profiler{&platform, "kws"};
64 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
65
alexander3c798932021-03-26 21:42:19 +000066 caseContext.Set<hal_platform&>("platform", platform);
67 caseContext.Set<arm::app::Model&>("model", model);
68 caseContext.Set<uint32_t>("clipIndex", 0);
69 caseContext.Set<int>("frameLength", g_FrameLength);
70 caseContext.Set<int>("frameStride", g_FrameStride);
71 caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); /* Normalised score threshold. */
72
73 KwsClassifier classifier; /* classifier wrapper object. */
74 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
75
76 std::vector <std::string> labels;
77 GetLabelsVector(labels);
78
79 caseContext.Set<const std::vector <std::string>&>("labels", labels);
80
81 bool executionSuccessful = true;
82 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
83
84 /* Loop. */
85 do {
86 int menuOption = MENU_OPT_RUN_INF_NEXT;
87 if (bUseMenu) {
88 DisplayMenu();
89 menuOption = arm::app::ReadUserInputAsInt(platform);
90 printf("\n");
91 }
92 switch (menuOption) {
93 case MENU_OPT_RUN_INF_NEXT:
94 executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false);
95 break;
96 case MENU_OPT_RUN_INF_CHOSEN: {
97 printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010098 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000099 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform));
100 executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false);
101 break;
102 }
103 case MENU_OPT_RUN_INF_ALL:
104 executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), true);
105 break;
106 case MENU_OPT_SHOW_MODEL_INFO:
107 executionSuccessful = model.ShowModelInfoHandler();
108 break;
109 case MENU_OPT_LIST_AUDIO_CLIPS:
110 executionSuccessful = ListFilesHandler(caseContext);
111 break;
112 default:
113 printf("Incorrect choice, try again.");
114 break;
115 }
116 } while (executionSuccessful && bUseMenu);
117 info("Main loop terminated.\n");
118}