Info.cpp
Go to the documentation of this file.
1 #include "Info.h"
2 
3 #include <opencv2/core/core.hpp>
4 #include <opencv2/imgcodecs.hpp>
5 #include <opencv2/imgproc/imgproc.hpp>
6 
7 #include <SimoxUtility/algorithm/string.h>
8 #include <SimoxUtility/filesystem/list_directory.h>
9 #include <SimoxUtility/json.h>
10 #include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
11 #include <SimoxUtility/shapes/OrientedBox.h>
12 
15 
17 
18 #include <VisionX/libraries/armem_human/aron/Profile.aron.generated.h>
20 
21 namespace armarx::armem::human
22 {
23  namespace fs = std::filesystem;
24 
25  Info::Info(const std::string& packageName,
26  const path& absPackageDataDir,
27  const path& relativePath,
28  const std::string& id) :
29  _packageName(packageName),
30  _absPackageDataDir(absPackageDataDir),
31  _relativePath(relativePath),
32  _id(id)
33  {
34  }
35 
36  void
38  {
39  this->_logError = enabled;
40  }
41 
42  std::string
43  Info::package() const
44  {
45  return _packageName;
46  }
47 
48  std::string
49  Info::id() const
50  {
51  return _id;
52  }
53 
55  Info::_relativeProfileDirectory() const
56  {
57  return _relativePath / _id;
58  }
59 
61  Info::file(const std::string& _extension, const std::string& suffix) const
62  {
63  std::string extension = _extension;
64  if (extension.at(0) != '.')
65  {
66  extension = "." + extension;
67  }
68  std::string filename = /*_id + */ suffix + extension;
69 
70  PackagePath path(_packageName, _relativeProfileDirectory() / filename);
71  return path;
72  }
73 
75  Info::sub_directory(const std::string& suffix) const
76  {
77  std::string name = /*_id +*/ suffix;
78  return PackagePath(_packageName, _relativeProfileDirectory() / name);
79  }
80 
83  {
84  return file(".json", "profile");
85  }
86 
89  {
90  // return sub_directory("_faces-images");
91  return sub_directory("faces-images");
92  }
93 
94  std::vector<PackagePath>
95  Info::faceImages(const std::vector<std::string>& extensions) const
96  {
97  const PackagePath directory = faceImageDir();
98 
99  std::vector<PackagePath> imageFiles;
100  bool local = false;
101  for (const fs::path& path : simox::fs::list_directory(directory.toSystemPath(), local))
102  {
103  for (const std::string& ext : extensions)
104  {
105  if (fs::is_regular_file(path) and simox::alg::ends_with(path.string(), ext))
106  {
107  PackagePath imageFile(_packageName,
108  _relativeProfileDirectory() / "faces-images" /
109  path.filename());
110  imageFiles.push_back(imageFile);
111  }
112  }
113  }
114  return imageFiles;
115  }
116 
117  std::optional<Info::Profile>
119  {
120  nlohmann::json j;
121  try
122  {
123  j = nlohmann::read_json(profileJson().toSystemPath());
124  }
125  catch (const std::exception& e)
126  {
127  if (_logError)
128  {
129  ARMARX_ERROR << e.what();
130  }
131  return std::nullopt;
132  }
133 
134  Profile profile = j.get<Profile>();
135  for (const PackagePath& imagePath : this->faceImages())
136  {
137  cv::Mat image =
138  cv::imread(imagePath.toSystemPath().string(), cv::ImreadModes::IMREAD_COLOR);
139  cv::cvtColor(image, image, cv::ColorConversionCodes::COLOR_BGR2RGB);
140 
141  armarx::arondto::PackagePath packagePath;
142  toAron(packagePath, imagePath);
143 
144  armarx::human::arondto::FaceImage faceImage;
145  faceImage.filepath = packagePath;
146  faceImage.image = image;
147  toAron(profile.faceImages.emplace_back(), faceImage);
148  }
149  return profile;
150  }
151 
152  bool
154  {
155  namespace fs = std::filesystem;
156  bool result = true;
157 
158  if (!fs::is_regular_file(profileJson().toSystemPath()))
159  {
160  if (_logError)
161  {
162  ARMARX_WARNING << "Expected simox object file for object " << *this << ": "
163  << profileJson().toSystemPath();
164  }
165  result = false;
166  }
167 
168  return result;
169  }
170 
171 } // namespace armarx::armem::human
172 
173 std::ostream&
174 armarx::armem::human::operator<<(std::ostream& os, const Info& rhs)
175 {
176  return os << rhs.id();
177 }
armarx::armem::human::operator<<
std::ostream & operator<<(std::ostream &os, const Info &rhs)
Definition: Info.cpp:174
armarx::armem::human::Info::file
PackagePath file(const std::string &extension, const std::string &suffix="") const
Definition: Info.cpp:61
Info.h
armarx::armem::human::toAron
void toAron(armarx::human::arondto::HumanPose &dto, const HumanPose &bo)
Definition: aron_conversions.cpp:31
armarx::armem::human::Info::faceImages
std::vector< PackagePath > faceImages(const std::vector< std::string > &extensions={".png", ".jpg"}) const
Definition: Info.cpp:95
armarx::armem::human::Info::sub_directory
PackagePath sub_directory(const std::string &suffix) const
Definition: Info.cpp:75
armarx::armem::human::Info::Profile
armarx::human::arondto::Profile Profile
Definition: Info.h:23
armarx::armem::human::Info::Info
Info(const std::string &packageName, const path &absPackageDataDir, const path &relativePath, const std::string &id)
Info.
Definition: Info.cpp:25
armarx::armem::human::Info::loadProfile
std::optional< Profile > loadProfile() const
Definition: Info.cpp:118
armarx::armem::human::Info::id
std::string id() const
Definition: Info.cpp:49
armarx::armem::human::Info::setLogError
void setLogError(bool enabled)
Definition: Info.cpp:37
armarx::PackagePath::toSystemPath
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Definition: PackagePath.cpp:47
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
filename
std::string filename
Definition: VisualizationRobot.cpp:86
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
aron_conversions.h
ExpressionException.h
armarx::armem::human::Info::checkPaths
virtual bool checkPaths() const
Checks the existence of expected files.
Definition: Info.cpp:153
armarx::armem::human::Info::package
std::string package() const
Definition: Info.cpp:43
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:53
armarx::armem::human::Info::faceImageDir
PackagePath faceImageDir() const
Definition: Info.cpp:88
armarx::armem::human
Definition: aron_conversions.cpp:16
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
json_conversions.h
armarx::PackagePath
Definition: PackagePath.h:52
armarx::armem::human::Info::path
std::filesystem::path path
Definition: Info.h:22
armarx::armem::human::Info::profileJson
PackagePath profileJson() const
Definition: Info.cpp:82
armarx::armem::human::Info
Accessor for human profile files.
Definition: Info.h:19