porcupine_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021, Rein Appeldoorn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <picovoice_msgs/GetKeywordAction.h>
19 #include <ros/init.h>
20 
21 #include "./porcupine_recognizer.h"
22 #include "./recognizer_node.h"
23 
25 {
26 using namespace picovoice_msgs;
27 class PorcupineNode : public RecognizerNode<PorcupineRecognizerData, PorcupineRecognizer, GetKeywordAction>
28 {
29 public:
30  PorcupineNode(const PorcupineRecognizerData::Parameters& parameters, const std::string& keywords_directory)
31  : RecognizerNode("porcupine", "get_keyword", parameters), keywords_directory_(keywords_directory)
32  {
33  }
34 
35 private:
36  void updateParameters(const GetKeywordGoal& goal, PorcupineRecognizerData::Parameters& parameters) override
37  {
38  if (goal.keywords.empty())
39  {
40  throw std::runtime_error("No keywords specified");
41  }
42 
43  parameters.keywords_.clear();
44  for (const auto& keyword : goal.keywords)
45  {
46  parameters.keywords_[keyword.name] = pathFromUrl(keyword.url, ".ppn", keywords_directory_);
47  }
48  }
49 
50  void updateResult(const PorcupineRecognizerData::Result& result, GetKeywordResult& action_result) override
51  {
52  action_result.is_understood = result.is_understood_;
53  action_result.keyword_name = result.keyword_name_;
54  }
55 
56  std::string keywords_directory_;
57 };
58 } // namespace picovoice_driver
59 
60 int main(int argc, char** argv)
61 {
62  using namespace picovoice_driver;
63 
64  ros::init(argc, argv, "porcupine");
65 
66  ros::NodeHandle local_nh("~");
67  auto model_url = local_nh.param("model_url", defaultResourceUrl() + "/models/porcupine_params.pv");
68  auto keywords_directory_url = local_nh.param("keywords_directory_url", defaultResourceUrl() + "/keywords");
69 
70  try
71  {
73  parameters.model_path_ = pathFromUrl(model_url, ".pv");
74 
75  PorcupineNode node(parameters, pathFromUrl(keywords_directory_url));
76  ros::spin();
77  }
78  catch (const std::exception& e)
79  {
80  ROS_FATAL("PorcupineNode exception: %s", e.what());
81  return 1;
82  }
83  return 0;
84 }
picovoice_driver::RecognizerNode
Definition: recognizer_node.h:33
picovoice_driver::pathFromUrl
std::string pathFromUrl(const std::string &url, const std::string &extension, const std::string &directory)
pathFromUrl Get a file path from an url
Definition: ros_util.cpp:49
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
init.h
recognizer_node.h
picovoice_driver::RecognizerData::Parameters::model_path_
std::string model_path_
model_path_ Path to the Picovoice model parameters
Definition: recognizer.h:38
picovoice_driver::PorcupineNode::PorcupineNode
PorcupineNode(const PorcupineRecognizerData::Parameters &parameters, const std::string &keywords_directory)
Definition: porcupine_node.cpp:30
picovoice_driver::PorcupineNode::updateParameters
void updateParameters(const GetKeywordGoal &goal, PorcupineRecognizerData::Parameters &parameters) override
Definition: porcupine_node.cpp:36
picovoice_driver::PorcupineNode::keywords_directory_
std::string keywords_directory_
Definition: porcupine_node.cpp:56
picovoice_driver
Definition: porcupine_node.cpp:24
picovoice_driver::PorcupineNode
Definition: porcupine_node.cpp:27
picovoice_driver::PorcupineRecognizerData::Parameters
Definition: porcupine_recognizer.h:33
main
int main(int argc, char **argv)
Definition: porcupine_node.cpp:60
ROS_FATAL
#define ROS_FATAL(...)
porcupine_recognizer.h
picovoice_driver::PorcupineRecognizerData::Parameters::keywords_
std::map< std::string, std::string > keywords_
keywords_ Map from a keyword names to a keyword.ppn paths
Definition: porcupine_recognizer.h:38
picovoice_driver::defaultResourceUrl
std::string defaultResourceUrl()
defaultResourcePath Get the default resource path
Definition: ros_util.cpp:27
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
ros::spin
ROSCPP_DECL void spin()
picovoice_driver::PorcupineNode::updateResult
void updateResult(const PorcupineRecognizerData::Result &result, GetKeywordResult &action_result) override
Definition: porcupine_node.cpp:50
picovoice_driver::PorcupineRecognizerData::Result::is_understood_
bool is_understood_
is_understood_ Whether the recognizer understood a keyword
Definition: porcupine_recognizer.h:51
picovoice_driver::PorcupineRecognizerData::Result::keyword_name_
std::string keyword_name_
keyword_name_ Detected keyword name
Definition: porcupine_recognizer.h:56
ros::NodeHandle
picovoice_driver::PorcupineRecognizerData::Result
Definition: porcupine_recognizer.h:46


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50