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/Person.aron.generated.h>
20
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
37 Info::setLogError(bool enabled)
38 {
39 this->_logError = enabled;
40 }
41
42 std::string
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 if (not fs::is_directory(directory.toSystemPath()))
100 {
101 ARMARX_VERBOSE << "Face image directory does not exist: " << directory.toSystemPath();
102 return {};
103 }
104
105 std::vector<PackagePath> imageFiles;
106 bool local = false;
107 for (const fs::path& path : simox::fs::list_directory(directory.toSystemPath(), local))
108 {
109 for (const std::string& ext : extensions)
110 {
111 if (fs::is_regular_file(path) and simox::alg::ends_with(path.string(), ext))
112 {
113 PackagePath imageFile(_packageName,
114 _relativeProfileDirectory() / "faces-images" /
115 path.filename());
116 imageFiles.push_back(imageFile);
117 }
118 }
119 }
120 return imageFiles;
121 }
122
123 std::optional<Info::Person>
124 Info::loadProfile(const bool loadFaceImages) const
125 {
126 nlohmann::json j;
127 try
128 {
129 ARMARX_INFO << "Loading profile: " << QUOTED(profileJson().toSystemPath());
130 j = nlohmann::read_json(profileJson().toSystemPath());
131 }
132 catch (const std::exception& e)
133 {
134 if (_logError)
135 {
136 ARMARX_ERROR << e.what();
137 }
138 return std::nullopt;
139 }
140
141 Person profile = j.get<Person>();
142 if (loadFaceImages)
143 {
144 for (const PackagePath& imagePath : this->faceImages())
145 {
146 cv::Mat image =
147 cv::imread(imagePath.toSystemPath().string(), cv::ImreadModes::IMREAD_COLOR);
148 cv::cvtColor(image, image, cv::ColorConversionCodes::COLOR_BGR2RGB);
149
150 armarx::arondto::PackagePath packagePath;
151 toAron(packagePath, imagePath);
152
153 armarx::human::arondto::FaceImage faceImage;
154 faceImage.filepath = packagePath;
155 faceImage.image = image;
156 toAron(profile.faceImages.emplace_back(), faceImage);
157 }
158 }
159 return profile;
160 }
161
162 bool
164 {
165 namespace fs = std::filesystem;
166 bool result = true;
167
168 if (!fs::is_regular_file(profileJson().toSystemPath()))
169 {
170 if (_logError)
171 {
172 ARMARX_WARNING << "Expected simox object file for object " << *this << ": "
174 }
175 result = false;
176 }
177
178 return result;
179 }
180
181} // namespace armarx::armem::human
182
183std::ostream&
184armarx::armem::human::operator<<(std::ostream& os, const Info& rhs)
185{
186 return os << rhs.id();
187}
#define QUOTED(x)
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Accessor for human profile files.
Definition Info.h:20
std::vector< PackagePath > faceImages(const std::vector< std::string > &extensions={".png", ".jpg"}) const
Definition Info.cpp:95
virtual bool checkPaths() const
Checks the existence of expected files.
Definition Info.cpp:163
std::string id() const
Definition Info.cpp:49
std::string package() const
Definition Info.cpp:43
void setLogError(bool enabled)
Definition Info.cpp:37
std::optional< Person > loadProfile(const bool loadFaceImages=true) const
Definition Info.cpp:124
PackagePath file(const std::string &extension, const std::string &suffix="") const
Definition Info.cpp:61
std::filesystem::path path
Definition Info.h:22
PackagePath sub_directory(const std::string &suffix) const
Definition Info.cpp:75
armarx::human::arondto::Person Person
Definition Info.h:23
PackagePath faceImageDir() const
Definition Info.cpp:88
PackagePath profileJson() const
Definition Info.cpp:82
Info(const std::string &packageName, const path &absPackageDataDir, const path &relativePath, const std::string &id)
Info.
Definition Info.cpp:25
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
void toAron(armarx::human::arondto::HumanPose &dto, const HumanPose &bo)
std::ostream & operator<<(std::ostream &os, const Info &rhs)
Definition Info.cpp:184