24 #include <boost/filesystem.hpp> 25 #include <opencv2/opencv.hpp> 37 SensorPlugin(), width_(0), height_(0), depth_(0), imageCounter_(0) {}
39 GeotaggedImagesPlugin::~GeotaggedImagesPlugin()
41 this->parentSensor_.reset();
42 this->camera_.reset();
45 void GeotaggedImagesPlugin::Load(sensors::SensorPtr sensor, sdf::ElementPtr sdf)
48 gzdbg << __FUNCTION__ <<
"() called." << std::endl;
52 gzerr <<
"Invalid sensor pointer.\n";
55 std::dynamic_pointer_cast<sensors::CameraSensor>(sensor);
57 if (!this->parentSensor_)
59 gzerr <<
"GeotaggedImagesPlugin requires a CameraSensor.\n";
62 this->camera_ = this->parentSensor_->Camera();
64 if (!this->parentSensor_)
66 gzerr <<
"GeotaggedImagesPlugin not attached to a camera sensor\n";
69 scene_ = camera_->GetScene();
70 lastImageTime_ = scene_->SimTime();
72 this->width_ = this->camera_->ImageWidth();
73 this->height_ = this->camera_->ImageHeight();
74 this->depth_ = this->camera_->ImageDepth();
75 this->format_ = this->camera_->ImageFormat();
77 if (sdf->HasElement(
"robotNamespace")) {
78 namespace_ = sdf->GetElement(
"robotNamespace")->Get<std::string>();
80 gzwarn <<
"[gazebo_geotagging_images_camera_plugin] Please specify a robotNamespace.\n";
83 this->storeIntervalSec_ = 1;
84 if (sdf->HasElement(
"interval")) {
85 this->storeIntervalSec_ = sdf->GetElement(
"interval")->Get<
float>();
89 if (sdf->HasElement(
"width")) {
90 destWidth_ = sdf->GetElement(
"width")->Get<
int>();
92 destHeight_ = height_;
93 if (sdf->HasElement(
"height")) {
94 destHeight_ = sdf->GetElement(
"height")->Get<
int>();
99 if (system(
"exiftool -ver &>/dev/null") != 0) {
100 gzerr <<
"exiftool not found. geotagging_images plugin will be disabled" << endl;
101 gzerr <<
"On Ubuntu, use 'sudo apt-get install libimage-exiftool-perl' to install" << endl;
105 node_handle_ = transport::NodePtr(
new transport::Node());
106 node_handle_->Init(namespace_);
108 this->parentSensor_->SetActive(
true);
110 this->newFrameConnection_ = this->camera_->ConnectNewImageFrame(
111 boost::bind(&GeotaggedImagesPlugin::OnNewFrame,
this, _1));
115 gpsSub_ = node_handle_->Subscribe(
"~/gps_position", &GeotaggedImagesPlugin::OnNewGpsPosition,
this);
117 storageDir_ =
"frames";
118 boost::filesystem::remove_all(storageDir_);
122 void GeotaggedImagesPlugin::OnNewGpsPosition(ConstVector3dPtr& v)
124 lastGpsPosition_ = *v;
128 void GeotaggedImagesPlugin::OnNewFrame(
const unsigned char * image)
131 image = this->camera_->ImageData(0);
133 common::Time currentTime = scene_->SimTime();
134 if (currentTime.Double() - lastImageTime_.Double() < storeIntervalSec_) {
138 Mat frame = Mat(height_, width_, CV_8UC3);
139 Mat frameBGR = Mat(height_, width_, CV_8UC3);
140 frame.data = (uchar*)image;
141 cvtColor(frame, frameBGR, cv::COLOR_RGB2BGR);
144 snprintf(file_name,
sizeof(file_name),
"%s/DSC%05i.jpg", storageDir_.c_str(), imageCounter_);
146 if (destWidth_ != width_ || destHeight_ != height_) {
148 cv::Size
size(destWidth_, destHeight_);
149 cv::resize(frameBGR, frameResized, size);
150 imwrite(file_name, frameResized);
152 imwrite(file_name, frameBGR);
155 char gps_tag_command[1024];
156 double lat = lastGpsPosition_.x();
157 char north_south =
'N', east_west =
'E';
158 double lon = lastGpsPosition_.y();
167 snprintf(gps_tag_command,
sizeof(gps_tag_command),
168 "exiftool -gpslatituderef=%c -gpslongituderef=%c -gpsaltituderef=above -gpslatitude=%.9lf -gpslongitude=%.9lf" 170 " -datetimeoriginal=now -gpsdop=0.8" 171 " -gpsmeasuremode=3-d -gpssatellites=13 -gpsaltitude=%.3lf -overwrite_original %s &>/dev/null",
172 north_south, east_west, lat, lon, lastGpsPosition_.z(), file_name);
174 system(gps_tag_command);
177 lastImageTime_ = currentTime;
GZ_REGISTER_SENSOR_PLUGIN(GazeboGpsPlugin)
Gazebo plugin that saves geotagged camera images to disk.
CvImagePtr cvtColor(const CvImageConstPtr &source, const std::string &encoding)
static const bool kPrintOnPluginLoad
void create_directory(std::string &path)