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");
85  }
86 
89  {
90  return sub_directory("_faces-images");
91  }
92 
93  std::vector<PackagePath>
94  Info::faceImages(const std::vector<std::string>& extensions) const
95  {
96  const PackagePath directory = faceImageDir();
97 
98  std::vector<PackagePath> imageFiles;
99  bool local = false;
100  for (const fs::path& path : simox::fs::list_directory(directory.toSystemPath(), local))
101  {
102  for (const std::string& ext : extensions)
103  {
104  if (fs::is_regular_file(path) and simox::alg::ends_with(path.string(), ext))
105  {
106  PackagePath imageFile(_packageName,
107  _relativeProfileDirectory() / (_id + "_faces-images") /
108  path.filename());
109  imageFiles.push_back(imageFile);
110  }
111  }
112  }
113  return imageFiles;
114  }
115 
116  std::optional<Info::Profile>
118  {
119  nlohmann::json j;
120  try
121  {
122  j = nlohmann::read_json(profileJson().toSystemPath());
123  }
124  catch (const std::exception& e)
125  {
126  if (_logError)
127  {
128  ARMARX_ERROR << e.what();
129  }
130  return std::nullopt;
131  }
132 
133  Profile profile = j.get<Profile>();
134  for (const PackagePath& imagePath : this->faceImages())
135  {
136  cv::Mat image =
137  cv::imread(imagePath.toSystemPath().string(), cv::ImreadModes::IMREAD_COLOR);
138  cv::cvtColor(image, image, cv::ColorConversionCodes::COLOR_BGR2RGB);
139 
140  armarx::arondto::PackagePath packagePath;
141  toAron(packagePath, imagePath);
142 
143  armarx::human::arondto::FaceImage faceImage;
144  faceImage.filepath = packagePath;
145  faceImage.image = image;
146  toAron(profile.faceImages.emplace_back(), faceImage);
147  }
148  return profile;
149  }
150 
151  bool
153  {
154  namespace fs = std::filesystem;
155  bool result = true;
156 
157  if (!fs::is_regular_file(profileJson().toSystemPath()))
158  {
159  if (_logError)
160  {
161  ARMARX_WARNING << "Expected simox object file for object " << *this << ": "
162  << profileJson().toSystemPath();
163  }
164  result = false;
165  }
166 
167  return result;
168  }
169 
170 } // namespace armarx::armem::human
171 
172 std::ostream&
173 armarx::armem::human::operator<<(std::ostream& os, const Info& rhs)
174 {
175  return os << rhs.id();
176 }
armarx::armem::human::operator<<
std::ostream & operator<<(std::ostream &os, const Info &rhs)
Definition: Info.cpp:173
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:94
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:24
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:117
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:54
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
filename
std::string filename
Definition: VisualizationRobot.cpp:83
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
aron_conversions.h
ExpressionException.h
armarx::armem::human::Info::checkPaths
virtual bool checkPaths() const
Checks the existence of expected files.
Definition: Info.cpp:152
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:50
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:186
json_conversions.h
armarx::PackagePath
Definition: PackagePath.h:55
armarx::armem::human::Info::path
std::filesystem::path path
Definition: Info.h:23
armarx::armem::human::Info::profileJson
PackagePath profileJson() const
Definition: Info.cpp:82
armarx::armem::human::Info
Accessor for human profile files.
Definition: Info.h:20