PersonMemoryDebugger.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 VisionX::ArmarXObjects::person_memory_debugger
17  * @author Peter Albrecht ( usnlf at student dot kit dot edu )
18  * @date 2024
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #include "PersonMemoryDebugger.h"
25 #include <optional>
26 #include <utility>
27 #include <Eigen/src/Geometry/AngleAxis.h>
28 #include <Eigen/src/Geometry/Quaternion.h>
29 #include <SimoxUtility/algorithm/get_map_keys_values.h>
30 #include <SimoxUtility/algorithm/string/string_tools.h>
31 
33 
42 
44 #include <VisionX/libraries/armem_human/aron/FaceRecognition.aron.generated.h>
45 #include <VisionX/libraries/armem_human/aron/HumanPose.aron.generated.h>
46 #include <VisionX/libraries/armem_human/aron/Profile.aron.generated.h>
53 
55 {
56 
57  const std::string PersonMemoryDebugger::pose_provider_name = "person_memory_debugger";
58  const std::string PersonMemoryDebugger::profile_provider_name = "person_memory_debugger";
59  const std::string PersonMemoryDebugger::recognition_provider_name = "person_memory_debugger";
60 
63  {
66  return def;
67  }
68 
69  void
71  {
72  }
73 
74  void
76  {
77  idCounter = 0;
78  running = true;
79  mns = memoryNameSystem();
80  while (running)
81  {
82  this->run();
83  }
84  }
85 
86  void
88  {
89  }
90 
91  void
93  {
94  }
95 
96  void
97  PersonMemoryDebugger::run()
98  {
99  ARMARX_INFO << "Use 'pose [id] [x] [y] [z]' to commit a pose at location";
100  ARMARX_INFO << "Use 'face [profile entityName] [x] [y] [z]' to commit a face at location";
101  ARMARX_INFO << "Use 'profile [profile entityName]' to commit a profile";
102  ARMARX_INFO << "Use 'exit' to exit";
103  std::string command;
104  std::cin >> command;
105  if (command == "pose")
106  {
107  poseCommand();
108  }
109  else if (command == "face")
110  {
111  faceRecognitionCommand();
112  }
113  else if (command == "profile")
114  {
115  profileCommand();
116  }
117  else if (command == "exit")
118  {
119  running = false;
120  }
121  else
122  {
123  ARMARX_WARNING << "Unrecognized command. Ignoring";
124  }
125 
126  std::cin.clear();
127  }
128 
129  void
130  PersonMemoryDebugger::poseCommand()
131  {
132  std::string id, x_s, y_s, z_s;
133  std::cin >> id >> x_s >> y_s >> z_s;
134  float x = std::stof(x_s);
135  float y = std::stof(y_s);
136  float z = std::stof(z_s);
137  Eigen::Vector3f pos(x, y, z);
138  commitPose(pos, id);
139  }
140 
141  void
142  PersonMemoryDebugger::faceRecognitionCommand()
143  {
144  std::string id, x_s, y_s, z_s;
145  std::cin >> id >> x_s >> y_s >> z_s;
146 
147  auto profileId = findProfileId(id);
148  if (not profileId.has_value())
149  {
150  ARMARX_WARNING << "Could not find profile. Continuing..";
151  }
152 
153  float x = std::stof(x_s);
154  float y = std::stof(y_s);
155  float z = std::stof(z_s);
156 
157  Eigen::Vector3f pos(x, y, z);
158  commitFaceRecognition(pos, profileId);
159  }
160 
161  void
162  PersonMemoryDebugger::profileCommand()
163  {
164  std::string name;
165  std::cin >> name;
166  commitProfile(name);
167  }
168 
169  void
170  PersonMemoryDebugger::commitProfile(std::string& name)
171  {
172  armarx::armem::client::Writer poseWriter =
174 
175  // we only set the firstName. FaceImages are not required,
176  // as the user manually sets the profile in the face recognition.
177  // therefore, this will not work with a real implementation of face recognition
178  // TODO: implement option to add face images
180  dto.firstName = name;
181 
184  .withEntityName(name);
185 
187  .entityID = entityId,
188  .instancesData = {dto.toAron()},
189  .referencedTime = armarx::armem::Time::Now(),
190  .confidence = 1.0F,
191  .sentTime = armarx::armem::Time::Now(),
192  .arrivedTime = armarx::armem::Time::Now(),
193  };
194 
195  ARMARX_INFO << "Committing profile with name: " << name;
196  poseWriter.commit(update);
197  }
198 
199  std::optional<armarx::armem::MemoryID>
200  PersonMemoryDebugger::findProfileId(std::string& entityId)
201  {
202  armarx::armem::client::Reader profileReader =
204 
207  std::optional<armarx::armem::MemoryID> foundProfileId = std::nullopt;
208 
209  // memory fetch failed
210  if (not queryResult.success)
211  {
212  return std::nullopt;
213  }
214 
215  // find the first matching profile
216  queryResult.memory.forEachInstance(
217  [&entityId, &foundProfileId](armarx::armem::wm::EntityInstance& instance)
218  {
219  if (foundProfileId.has_value())
220  {
221  // we want the first match, and ignore all further matches here.
222  return;
223  }
224 
225  if (instance.id().entityName == entityId)
226  {
227  // match found
228  foundProfileId = instance.id();
229  }
230  }
231  );
232 
233  return foundProfileId;
234  }
235 
236  void
237  PersonMemoryDebugger::commitFaceRecognition(Eigen::Vector3f& facePosition, std::optional<armarx::armem::MemoryID> profileId)
238  {
239  armarx::armem::client::Writer poseWriter =
241 
242  // get the id counter
243  std::stringstream ss;
244  ss << idCounter++;
245 
247  bo.framedPosition3D = armarx::FramedPosition(facePosition, "global", "Armar7");
248  // doesnt make sense, but this isn't set in prod anyways
249  bo.position2D = Eigen::Vector2i::Identity();
250  bo.position3DGlobal = facePosition;
251  // profile id may be nullopt, but this case is communicated to the user
252  bo.profileID = std::move(profileId);
253 
254  armarx::human::arondto::FaceRecognition dto;
256 
259  .withEntityName(ss.str());
260 
262  .entityID = entityId,
263  .instancesData = {dto.toAron()},
264  .referencedTime = armarx::armem::Time::Now(),
265  .confidence = 1.0F,
266  .sentTime = armarx::armem::Time::Now(),
267  .arrivedTime = armarx::armem::Time::Now(),
268  };
269 
270  // hack
271  ARMARX_INFO << "Committing face recognition for profile: " << profileId.value_or(armarx::armem::MemoryID().withEntityName("INVALID")).entityName;
272  poseWriter.commit(update);
273 
274  }
275 
276  void
277  PersonMemoryDebugger::commitPose(Eigen::Vector3f& facePosition, std::string& humanId)
278  {
279  armarx::armem::client::Writer poseWriter =
281 
283  pose.humanTrackingId = humanId;
285  pose.cameraFrameName = "global";
287  armarx::FramedOrientation defaultOrientation(Eigen::Quaternionf::UnitRandom(), "global", "Armar7");
288 
290 
291  // iterate over all joints, and set their global positions to the face pos.
292  // this is the only value required to be valid by the PersonInstanceUpdater
293  for (auto& joint : JointNames.names())
294  {
296  .label = joint,
297  .confidence = 1.0F,
298  .positionCamera = armarx::FramedPosition(),
299  .orientationCamera = armarx::FramedOrientation(),
300  .positionRobot = armarx::FramedPosition(),
301  .orientationRobot = armarx::FramedOrientation(),
302  .positionGlobal = armarx::FramedPosition(facePosition, "global", "Armar7"),
303  .orientationGlobal = defaultOrientation,
304  };
305  // set every joint to face position
306  pose.keypoints[joint] = key;
307  }
308 
311  .withEntityName(humanId);
312 
313  armarx::human::arondto::HumanPose dto;
314  armarx::armem::human::toAron(dto, pose);
315 
317  .entityID = entityId,
318  .instancesData = {dto.toAron()},
319  .referencedTime = armarx::armem::Time::Now(),
320  .confidence = 1.0F,
321  .sentTime = armarx::armem::Time::Now(),
322  .arrivedTime = armarx::armem::Time::Now(),
323  };
324 
325  ARMARX_INFO << "Committing pose with HumanID '" << humanId << "'.";
326  poseWriter.commit(update);
327 
328  }
329 
330  std::string
332  {
333  return "PersonMemoryDebugger";
334  }
335 
336  // ARMARX_REGISTER_COMPONENT_EXECUTABLE(PersonMemoryDebugger,
337  // PersonMemoryDebugger::GetDefaultName());
338 
339 } // namespace VisionX::components::person_memory_debugger
armarx::armem::detail::SuccessHeader::success
bool success
Definition: SuccessHeader.h:20
VisionX::components::person_memory_debugger
Definition: PersonMemoryDebugger.cpp:54
VisionX::components::person_memory_debugger::PersonMemoryDebugger::onInitComponent
void onInitComponent() override
Definition: PersonMemoryDebugger.cpp:70
armarx::armem::client::Reader
Reads data from a memory server.
Definition: Reader.h:24
Reader.h
armarx::armem::client::QueryResult::memory
wm::Memory memory
The slice of the memory that matched the query.
Definition: Query.h:58
armarx::armem::client::Reader::getLatestSnapshotsIn
QueryResult getLatestSnapshotsIn(const MemoryID &id, armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the latest snapshots under the given memory ID.
Definition: Reader.cpp:343
Writer.h
armarx::human::FaceRecognitionCoreSegmentID
const armem::MemoryID FaceRecognitionCoreSegmentID
Definition: memory_ids.cpp:31
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::human::toAron
void toAron(armarx::human::arondto::HumanPose &dto, const HumanPose &bo)
Definition: aron_conversions.cpp:31
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
MemoryID.h
armarx::armem::client::plugins::PluginUser::memoryNameSystem
MemoryNameSystem & memoryNameSystem()
Definition: PluginUser.cpp:22
armarx::human::pose::model::k4a_bt_body_32
This file is part of ArmarX.
Definition: k4a_bt_body_32.h:32
armarx::armem::human::FaceRecognition
Definition: types.h:59
forward_declarations.h
VisionX::components::person_memory_debugger::PersonMemoryDebugger::onConnectComponent
void onConnectComponent() override
Definition: PersonMemoryDebugger.cpp:75
armarx::armem::human::PoseKeypoint::label
std::string label
Definition: types.h:19
PersonMemoryDebugger.h
memory_ids.h
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
VisionX::components::person_memory_debugger::PersonMemoryDebugger::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PersonMemoryDebugger.cpp:62
armarx::armem::client::MemoryNameSystem::getWriter
Writer getWriter(const MemoryID &memoryID)
Get a writer to the given memory name.
Definition: MemoryNameSystem.cpp:270
k4a_bt_body_32.h
VisionX::components::person_memory_debugger::PersonMemoryDebugger::getDefaultName
std::string getDefaultName() const override
Definition: PersonMemoryDebugger.cpp:331
armarx::armem::human::HumanPose::poseModelId
std::string poseModelId
Definition: types.h:32
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::human::PoseKeypoint
Definition: types.h:17
types.h
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::human::pose::model::k4a_bt_body_32::JointNames
const simox::meta::EnumNames< Joints > JointNames
Names of the joints as defined in the body model.
Definition: k4a_bt_body_32.h:78
PoseSegment.h
if
if(!yyvaluep)
Definition: Grammar.cpp:724
FramedPose.h
VisionX::components::person_memory_debugger::PersonMemoryDebugger::recognition_provider_name
static const std::string recognition_provider_name
Definition: PersonMemoryDebugger.h:60
armarx::armem::human::HumanPose::timestamp
DateTime timestamp
Definition: types.h:37
armarx::armem::human::HumanPose::cameraFrameName
std::string cameraFrameName
Definition: types.h:38
armarx::armem::server::human::profile::Profile
armarx::human::arondto::Profile Profile
Definition: Segment.cpp:43
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::client::Writer
Helps a memory client sending data to a memory.
Definition: Writer.h:22
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
armarx::FramedOrientation
The FramedOrientation class.
Definition: FramedPose.h:199
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::human::HumanPose
Definition: types.h:30
armarx::armem::MemoryID::entityName
std::string entityName
Definition: MemoryID.h:53
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::armem::human::HumanPose::humanTrackingId
std::optional< std::string > humanTrackingId
Definition: types.h:36
memory_definitions.h
armarx::human::pose::model::k4a_bt_body_32::ModelId
const std::string ModelId
Definition: k4a_bt_body_32.h:34
VisionX::components::person_memory_debugger::PersonMemoryDebugger::onExitComponent
void onExitComponent() override
Definition: PersonMemoryDebugger.cpp:92
Commit.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
Decoupled.h
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
VisionX::components::person_memory_debugger::PersonMemoryDebugger::pose_provider_name
static const std::string pose_provider_name
Definition: PersonMemoryDebugger.h:58
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
aron_conversions.h
Segment.h
armarx::human::ProfileCoreSegmentID
const armem::MemoryID ProfileCoreSegmentID
Definition: memory_ids.cpp:34
IceUtil::Handle< class PropertyDefinitionContainer >
Segment.h
armarx::armem::base::detail::ForEachEntityInstanceMixin::forEachInstance
bool forEachInstance(InstanceFunctionT &&func)
Definition: iteration_mixins.h:146
VisionX::components::person_memory_debugger::PersonMemoryDebugger::onDisconnectComponent
void onDisconnectComponent() override
Definition: PersonMemoryDebugger.cpp:87
armarx::human::PoseCoreSegmentID
const armem::MemoryID PoseCoreSegmentID
Definition: memory_ids.cpp:40
MemoryNameSystem.h
armarx::armem::human::HumanPose::keypoints
KeyPointMap keypoints
Definition: types.h:35
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::client::MemoryNameSystem::getReader
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Definition: MemoryNameSystem.cpp:177
VisionX::components::person_memory_debugger::PersonMemoryDebugger::profile_provider_name
static const std::string profile_provider_name
Definition: PersonMemoryDebugger.h:59
armarx::aron::bo
const std::optional< BoT > & bo
Definition: aron_conversions.h:168