NameRecognizedObjects.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotSkillTemplates::ScanLocationGroup
17  * @author Markus Grotz ( markus dot grotz at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "NameRecognizedObjects.h"
24 
27 
29 
30 #include <SimoxUtility/json.h>
31 #include <SimoxUtility/algorithm/string.h>
32 
33 #include <boost/regex.hpp>
34 
35 #include <filesystem>
36 
37 
38 using namespace armarx;
39 using namespace ScanLocationGroup;
40 
41 namespace fs = std::filesystem;
42 
43 // DO NOT EDIT NEXT LINE
44 NameRecognizedObjects::SubClassRegistry NameRecognizedObjects::Registry(NameRecognizedObjects::GetName(), &NameRecognizedObjects::CreateInstance);
45 
46 
47 class InterruptedException : std::exception
48 {};
49 
50 
51 void NameRecognizedObjects::waitForSpeechFinished()
52 {
53  ARMARX_INFO << "Starting to wait for speech to finish";
54  TimeUtil::SleepMS(200);
55  while (!isRunningTaskStopped())
56  {
57  ARMARX_INFO << deactivateSpam(1) << "Speech observer status: " << getSpeechObserver()->getDatafieldByName("TextToSpeech", "State")->getString();
58  if (getSpeechObserver()->getDatafieldByName("TextToSpeech", "State")->getString() == "FinishedSpeaking")
59  {
60  ARMARX_IMPORTANT << "Speech observer status: " << getSpeechObserver()->getDatafieldByName("TextToSpeech", "State")->getString();
61  break;
62  }
64  }
65  if (isRunningTaskStopped())
66  {
67  throw InterruptedException();
68  }
69 }
70 
71 
73 {
74 }
75 
76 static std::vector<std::string> getNamesFromChannels(std::vector<ChannelRefPtr> const& channels,
77  std::map<std::string, std::string> const& humanObjectNameMap)
78 {
79  boost::regex re("[^a-zA-Z0-9]+");
80 
81  auto resolveObjectName = [&](ChannelRefPtr channelRef)
82  {
83  std::string memoryName = channelRef->getDataField("className")->getString();
84  if (humanObjectNameMap.count(memoryName))
85  {
86  return humanObjectNameMap.at(memoryName);
87  }
88  else
89  {
90  return boost::regex_replace(memoryName, re, std::string(" "));
91  }
92  };
93 
94  std::vector<ChannelRefPtr> const& objectInstanceChannels = channels;
95  std::vector<std::string> objectNames;
96 
97  std::set<std::string> uniqueObjectNames;
98  for (ChannelRefPtr const& p : objectInstanceChannels)
99  {
100  auto resolvedName = resolveObjectName(p);
101  if (uniqueObjectNames.count(resolvedName))
102  {
103  continue;
104  }
105  uniqueObjectNames.insert(resolvedName);
106  objectNames.push_back(resolvedName);
107  }
108 
109  return objectNames;
110 }
111 
113 {
114  std::map<std::string, std::string> humanObjectNameMap = in.getHumanObjectNameMap();
115 
116  std::vector<std::string> recognizedObjectsVec = getNamesFromChannels(in.getObjectInstanceChannels(), humanObjectNameMap);
117 
118  if (in.getUseObjectPoseStorage())
119  {
120  // Also get the object names from the ObjectPoseStorage
121  const objpose::ObjectPoseStorageInterfacePrx objectPoseStorage = getObjectPoseStorage();
122  const std::vector<std::string> objPoseProviderNames = in.getObjectPoseProviderNames();
123  const std::set<std::string> objPoseProviderNamesSet(objPoseProviderNames.begin(), objPoseProviderNames.end());
124  const objpose::ObjectPoseSeq objectPoses = objpose::fromIce(getObjectPoseStorage()->getObjectPoses());
125  ARMARX_INFO << "Got " << objectPoses.size() << " object poses.";
126 
127  ObjectFinder finder;
128  for (objpose::ObjectPose const& objectPose : objectPoses)
129  {
130  if (objPoseProviderNamesSet.count(objectPose.providerName))
131  {
132  const static bool includeClassName = false;
133  const std::vector<std::string> names = finder.loadSpokenNames(objectPose.objectID, includeClassName);
134  ARMARX_IMPORTANT << "Loaded names for object " << objectPose.objectID << ": " << names;
135 
136  const std::string objectName = names.size() > 0 ? names.front()
137  : objectPose.objectID.className();
138  recognizedObjectsVec.push_back(objectName);
139  }
140  }
141  }
142  std::set<std::string> recognizedObjects;
143  for (const std::string& name : recognizedObjectsVec)
144  {
145  recognizedObjects.insert(simox::alg::to_lower(name));
146  }
147 
148  std::vector<std::string> display = in.getDisplay_Text();
149  if (display.size() != 2)
150  {
151  display.resize(2);
152  }
153 
154  std::vector<std::string> ttsSeq;
155  if (recognizedObjects.empty())
156  {
157  ttsSeq.push_back("I cannot see any objects that I know.");
158  display[1] = "I did not find any objects.";
159  }
160  else if (recognizedObjects.size() == 1)
161  {
162  const std::string resolvedName = *recognizedObjects.begin();
163  ttsSeq.push_back("I can see " + resolvedName);
164  display[1] = "I can see the " + resolvedName;
165  }
166  else
167  {
168  ttsSeq.push_back("I can see the following objects: ");
169  for (const std::string& objectName : recognizedObjects)
170  {
171  ttsSeq.push_back(" a " + objectName + "");
172  }
173 
174  display[1] = "I found " + std::to_string(recognizedObjects.size()) + " objects.";
175  }
176 
177  getMessageDisplay()->setMessage(display[0], display[1]);
178 
179  if (in.getReportObjectsWithSpeech())
180  {
181  for (auto tts : ttsSeq)
182  {
183  getTextToSpeech()->reportText(tts);
184  if (in.getWaitForSpeechToFinish())
185  {
186  waitForSpeechFinished();
187  }
188  else
189  {
190  ARMARX_INFO << deactivateSpam(5) << "Not waiting for speech";
191  }
192 
193  }
194  }
195 
196  emitSuccess();
197 }
198 
199 //void NameRecognizedObjects::onBreak()
200 //{
201 // // put your user code for the breaking point here
202 // // execution time should be short (<100ms)
203 //}
204 
206 {
207  // put your user code for the exit point here
208  // execution time should be short (<100ms)
209 }
210 
211 
212 // DO NOT EDIT NEXT FUNCTION
214 {
215  return XMLStateFactoryBasePtr(new NameRecognizedObjects(stateData));
216 }
217 
armarx::ObjectPoseStorageInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::objpose::ObjectPoseStorageInterface > ObjectPoseStorageInterfacePrx
Definition: ObjectPoseClientWidget.h:51
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::objpose::fromIce
void fromIce(const Box &box, simox::OrientedBox< float > &oobb)
display
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this display
Definition: license.txt:11
armarx::ScanLocationGroup::NameRecognizedObjects::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: NameRecognizedObjects.cpp:213
IceInternal::Handle< ChannelRef >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::ScanLocationGroup::NameRecognizedObjects::onEnter
void onEnter() override
Definition: NameRecognizedObjects.cpp:72
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
armarx::ScanLocationGroup::NameRecognizedObjects::Registry
static SubClassRegistry Registry
Definition: NameRecognizedObjects.h:47
NameRecognizedObjects.h
armarx::ObjectFinder::loadSpokenNames
std::vector< std::string > loadSpokenNames(const ObjectID &objectID, bool includeClassName=false) const
Load names to use when verbalizing an object name.
Definition: ObjectFinder.cpp:369
ObjectPose.h
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::TimeUtil::SleepMS
static void SleepMS(float milliseconds)
Definition: TimeUtil.h:195
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ScanLocationGroup::NameRecognizedObjects::onExit
void onExit() override
Definition: NameRecognizedObjects.cpp:205
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::ScanLocationGroup::NameRecognizedObjects::run
void run() override
Definition: NameRecognizedObjects.cpp:112
InterruptedException
Definition: NameRecognizedObjects.cpp:47
armarx::ScanLocationGroup::NameRecognizedObjects::NameRecognizedObjects
NameRecognizedObjects(const XMLStateConstructorParams &stateData)
Definition: NameRecognizedObjects.h:34
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
ObjectFinder.h