PageRenderTime 365ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main.cpp

https://github.com/yianni/imagepublisher
C++ | 119 lines | 95 code | 15 blank | 9 comment | 10 complexity | d5ed71fd2feba2edc9a0817add29240f MD5 | raw file
  1. /**
  2. * @file main.cpp
  3. * @brief
  4. * @date Created on: 4 Nov 2010
  5. *
  6. * @author Yianni
  7. * [Note: deprecated practice as git will maintain author info and blame logs.]
  8. */
  9. #define BOOST_FILESYSTEM_NO_DEPRECATED
  10. #include "boost/filesystem.hpp"
  11. #include "sensor_msgs/Image.h"
  12. #include "image_transport/image_transport.h"
  13. #include "cv_bridge/CvBridge.h"
  14. #include "std_msgs/String.h"
  15. #include <iostream>
  16. #include <string>
  17. #include <vector>
  18. #include <algorithm>
  19. #include <opencv/cv.h>
  20. #include <opencv/highgui.h>
  21. #include <ros/ros.h>
  22. #include <signal.h>
  23. #include <sstream>
  24. #include <boost/program_options.hpp>
  25. #include <climits>
  26. using namespace boost::filesystem;
  27. namespace po = boost::program_options;
  28. using namespace std;
  29. void ctrlc(int s)
  30. {
  31. cout << "ctrl-c pressed" << endl;
  32. ros::shutdown();
  33. signal(SIGINT, SIG_DFL);
  34. }
  35. int main(int argc, char *argv[])
  36. {
  37. string p = "./"; //(argc == 3 ? argv[2] : ".");
  38. po::options_description desc("Allowed options");
  39. desc.add_options()
  40. ("help", "produce help message")
  41. ("delay", po::value<int>(), "set delay for image wait")
  42. ("dir", po::value< vector<string> >(), "the directory path")
  43. ("epochs", po::value<unsigned long long int>(), "number of epochs");
  44. po::variables_map vm;
  45. po::store(po::parse_command_line(argc, argv, desc), vm);
  46. po::notify(vm);
  47. if (vm.count("help")) {
  48. cout << desc << "\n";
  49. return 1;
  50. }
  51. int cvDelay = vm.count("delay") ? vm["delay"].as<int>() : 500;
  52. cout << "Delay: " << cvDelay << " ms" << endl;
  53. if (vm.count("dir"))
  54. p.assign(vm["dir"].as< vector<string> >()[0].c_str());
  55. cout << "Dir: " << p.c_str() << endl;
  56. unsigned long long int epochs = vm.count("epochs") ? vm["epochs"].as<unsigned long long int>() : ULLONG_MAX;
  57. cout << "Epochs: " << epochs << endl;
  58. ros::init(argc, argv, "Static_Image_Publisher");
  59. ros::NodeHandle n;
  60. image_transport::ImageTransport it_(n);
  61. sensor_msgs::CvBridge bridge_;
  62. image_transport::Publisher image_pub_;
  63. image_pub_ = it_.advertise("/camera_sim/image", 1);
  64. ros::Publisher strPub = n.advertise<std_msgs::String>("/camera_sim/image_filename", 1, true);
  65. std_msgs::String msg;
  66. vector<string> filenames;
  67. IplImage *img = NULL;
  68. CvFont font;
  69. cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
  70. cvNamedWindow("main", CV_WINDOW_AUTOSIZE);
  71. cvMoveWindow("main", 600, 600);
  72. signal(SIGINT, ctrlc);
  73. for(unsigned long long int i=0; i<epochs; i++) {
  74. if (is_directory(p)) {
  75. for (directory_iterator itr(p); itr!=directory_iterator(); ++itr)
  76. if (is_regular_file(itr->status())) {
  77. string str(itr->path().file_string());
  78. filenames.push_back(str);
  79. }
  80. } else
  81. ROS_ERROR("error reading directory");
  82. sort(filenames.begin(), filenames.end());
  83. for (vector<string>::iterator itr = filenames.begin(); itr != filenames.end(); ++itr) {
  84. img = cvLoadImage(itr->c_str());
  85. if(!img)
  86. cout << "Could not load image file: " << itr->c_str() << endl;
  87. else {
  88. try {
  89. image_pub_.publish(bridge_.cvToImgMsg(img, "bgr8"));
  90. msg.data = itr->c_str();
  91. strPub.publish(msg);
  92. } catch (sensor_msgs::CvBridgeException error) {
  93. ROS_ERROR("error");
  94. }
  95. cvPutText(img, itr->c_str(), cvPoint(200, 290), &font, cvScalar(0, 255, 0, 0));
  96. cvShowImage("main", img);
  97. cvWaitKey(cvDelay);
  98. cvReleaseImage(&img);
  99. }
  100. }
  101. }
  102. //ros::spin();
  103. return 0;
  104. }