8#include <RobotsIO/Camera/YarpCamera.h>
9#include <RobotsIO/Utils/ParametersYarpPort.h>
14#include <yarp/cv/Cv.h>
15#include <yarp/eigen/Eigen.h>
16#include <yarp/os/LogStream.h>
17#include <yarp/os/Stamp.h>
18#include <yarp/os/Value.h>
21using namespace RobotsIO::Camera;
22using namespace RobotsIO::Utils;
23using namespace yarp::cv;
24using namespace yarp::eigen;
25using namespace yarp::os;
26using namespace yarp::sig;
29YarpCamera::YarpCamera(
const std::string& port_prefix,
const bool& network_bootstrap)
32 if (!yarp_.checkNetwork())
34 throw(std::runtime_error(log_name_ +
"::ctor. Error: YARP network is not available."));
38 if (!(port_rgb_.open(
"/" + port_prefix +
"/rgb:i")))
40 std::string err = log_name_ +
"::ctor. Error: cannot open rgb input port.";
41 throw(std::runtime_error(err));
45 if (!(port_depth_.open(
"/" + port_prefix +
"/depth:i")))
47 std::string err = log_name_ +
"::ctor. Error: cannot open depth input port.";
48 throw(std::runtime_error(err));
51 if (network_bootstrap)
55 while (!(network_parameters.receive_parameters()))
57 std::this_thread::sleep_for(std::chrono::seconds(1));
59 yInfo() << log_name_ +
"::ctor. Waiting for camera parameters on port " +
"/" + port_prefix +
"/dataset/camera_parameters:i";
66 std::cout << log_name_ +
"::ctor. Camera parameters:" << std::endl;
67 std::cout << log_name_ +
" - width: " << parameters_.width() << std::endl;
68 std::cout << log_name_ +
" - height: " << parameters_.height() << std::endl;
69 std::cout << log_name_ +
" - fx: " << parameters_.fx() << std::endl;
70 std::cout << log_name_ +
" - fy: " << parameters_.fy() << std::endl;
71 std::cout << log_name_ +
" - cx: " << parameters_.cx() << std::endl;
72 std::cout << log_name_ +
" - cy: " << parameters_.cy() << std::endl;
79 const std::size_t& width,
80 const std::size_t& height,
85 const std::string& port_prefix,
86 const bool& enable_camera_pose
88 enable_camera_pose_(enable_camera_pose)
91 if (!yarp_.checkNetwork())
93 throw(std::runtime_error(log_name_ +
"::ctor. Error: YARP network is not available."));
97 parameters_.width(width);
98 parameters_.height(height);
103 parameters_.initialized(
true);
106 if (!(port_rgb_.open(
"/" + port_prefix +
"/rgb:i")))
108 std::string err = log_name_ +
"::ctor. Error: cannot open rgb input port.";
109 throw(std::runtime_error(err));
113 if (!(port_depth_.open(
"/" + port_prefix +
"/depth:i")))
115 std::string err = log_name_ +
"::ctor. Error: cannot open depth input port.";
116 throw(std::runtime_error(err));
120 if (enable_camera_pose_)
122 if (!(port_pose_.open(
"/" + port_prefix +
"/pose:i")))
124 std::string err = log_name_ +
"::ctor. Error: cannot open pose input port.";
125 throw(std::runtime_error(err));
129 Camera::initialize();
132 std::cout << log_name_ +
"::ctor. Camera parameters:" << std::endl;
133 std::cout << log_name_ +
" - width: " << parameters_.width() << std::endl;
134 std::cout << log_name_ +
" - height: " << parameters_.height() << std::endl;
135 std::cout << log_name_ +
" - fx: " << parameters_.fx() << std::endl;
136 std::cout << log_name_ +
" - fy: " << parameters_.fy() << std::endl;
137 std::cout << log_name_ +
" - cx: " << parameters_.cx() << std::endl;
138 std::cout << log_name_ +
" - cy: " << parameters_.cy() << std::endl;
141YarpCamera::YarpCamera
143 const std::string& data_path,
144 const std::size_t& width,
145 const double& height,
151 Camera(data_path, width, height, fx, cx, fy, cy)
153 Camera::initialize();
157YarpCamera::~YarpCamera()
164 if (enable_camera_pose_)
171 ImageOf<PixelFloat>* image_in;
172 image_in = port_depth_.read(blocking);
174 if (image_in ==
nullptr)
175 return std::make_pair(
false, MatrixXf());
178 port_depth_.getEnvelope(stamp);
179 time_stamp_depth_ = stamp.getTime();
180 is_time_stamp_depth_ =
true;
182 cv::Mat image = yarp::cv::toCvMat(*image_in);
183 Map<Eigen::Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>> float_image(image.ptr<
float>(), image.rows, image.cols);
185 return std::make_pair(
true, float_image);
191 if (enable_camera_pose_)
193 yarp::sig::Vector* pose_in = port_pose_.read(blocking);
195 if (pose_in !=
nullptr)
197 last_camera_pose_ = *pose_in;
200 if (last_camera_pose_.size() == 7)
204 pose = Translation<double, 3>(Vector3d(last_camera_pose_[0], last_camera_pose_[1], last_camera_pose_[2]));
205 pose.rotate(AngleAxisd(last_camera_pose_[6], Vector3d(last_camera_pose_[3], last_camera_pose_[4], last_camera_pose_[5])));
207 return std::make_pair(
true,
pose);
223std::pair<bool, cv::Mat> YarpCamera::rgb(
const bool& blocking)
225 ImageOf<PixelRgb>* image_in;
226 image_in = port_rgb_.read(blocking);
228 if (image_in ==
nullptr)
229 return std::make_pair(
false, cv::Mat());
232 port_rgb_.getEnvelope(stamp);
233 time_stamp_rgb_ = stamp.getTime();
234 is_time_stamp_rgb_ =
true;
236 cv::Mat image = yarp::cv::toCvMat(*image_in);
237 cv::resize(image, image, cv::Size(parameters_.width(), parameters_.height()));
239 return std::make_pair(
true, image);
243std::pair<bool, double> YarpCamera::time_stamp_rgb()
const
245 return std::make_pair(is_time_stamp_rgb_, time_stamp_rgb_);
249std::pair<bool, double> YarpCamera::time_stamp_depth()
const
251 return std::make_pair(is_time_stamp_depth_, time_stamp_depth_);
std::pair< bool, Eigen::MatrixXf > depth(const bool &blocking) override
std::pair< bool, Eigen::Transform< double, 3, Eigen::Affine > > pose(const bool &blocking) override