Advertisement
gachiemchiep

buggy c++

Nov 8th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. /*
  2. * File: display.cpp
  3. * Author: gachiemchiep
  4. *
  5. * Created on October 25, 2013, 11:29 AM
  6. */
  7.  
  8. #include <cstdlib>
  9. #include "boost/filesystem.hpp"
  10. #include "boost/algorithm/string.hpp"
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <string>
  14. #include <sstream>
  15. #include <fstream>
  16. #include "opencv2/opencv.hpp"
  17. #include "opencv2/core/core.hpp"
  18. #include "opencv2/highgui/highgui.hpp"
  19. #include "boost/lexical_cast.hpp"
  20. #include "boost/filesystem.hpp"
  21.  
  22. using namespace std;
  23.  
  24.  
  25. struct video_cluster {
  26. boost::filesystem::path vid_path;
  27. boost::filesystem::path img_path;
  28. int cluster_num;
  29. };
  30.  
  31. struct by_cluster_num{
  32. bool operator()(const video_cluster &a,const video_cluster &b) {
  33. return a.cluster_num < b.cluster_num;
  34. }
  35. };
  36.  
  37. /*
  38. * This program convert result: vid,cluster into vid,img,cluster
  39. * It also make a img folder that contains all vid's snapshot
  40. *
  41. */
  42. int main(int argc, char** argv) {
  43.  
  44. std::vector<boost::filesystem::path> input_files;
  45. for(int i=1;i<argc;i++){
  46. boost::filesystem::path tmp(argv[i]);
  47. input_files.push_back(tmp);
  48. std::cout << tmp << std::endl;
  49. }
  50.  
  51. for(int i=0;i<input_files.size();i++){
  52. std::vector<video_cluster> result_new;
  53.  
  54. std::ifstream infile(input_files[i].string().c_str());
  55. std::string line;
  56.  
  57. std::vector<boost::filesystem::path> vids;
  58. std::vector<int> clusters;
  59.  
  60. while(std::getline(infile, line)){
  61. std::vector<std::string> strs;
  62. boost::split(strs, line, boost::is_any_of(","));
  63. boost::filesystem::path p(strs[0]);
  64. vids.push_back(p);
  65. clusters.push_back(std::atoi(strs[1].c_str()));
  66. }
  67. // make img directory
  68. boost::filesystem::path dir("img");
  69. if(!boost::filesystem::create_directories(dir)){
  70. std::cout << "Can not create img dir \n";
  71. } else {
  72. std::cout << "Make img , move all image file into img directory \n";
  73. }
  74.  
  75. cv::VideoWriter write;
  76. for(int j=0;j<vids.size();j++){
  77. std::cout << vids[j]<<" "<<clusters[j]<<std::endl;
  78. cv::VideoCapture cap(vids[j].string());
  79. cv::Mat frame;
  80. if(!cap.isOpened()){
  81. std::cout << vids[j] <<" can not be opened \n";
  82. } else {
  83. cap.set(CV_CAP_PROP_POS_FRAMES,120);
  84. cap >> frame;
  85. std::string name =vids[j].stem().string()+".png";
  86. std::cout<<"make " << name <<"\n";
  87. cv::imwrite(name,frame);
  88. // move to img
  89. boost::filesystem::path from(name);
  90. boost::filesystem::path to("img/"+name);
  91.  
  92. boost::filesystem::rename(from ,to);
  93.  
  94. video_cluster tmp;
  95. // tmp.vid_path = vids[j];
  96. tmp.img_path =to;
  97. tmp.cluster_num = clusters[j];
  98. std::cout << vids[j] <<" "<<to<<" "<<clusters[j]<<"\n";
  99. getchar();
  100. result_new.push_back(tmp);
  101. std::cout << result_new.size()<<"\n";
  102. if(!result_new.empty()){
  103. std::cout << result_new.back().img_path<<" "<<result_new.back().vid_path<<" "<<result_new.back().cluster_num<<"\n";
  104. std::cout<<"----------------------> " << result_new.back().img_path<<" "<<result_new.back().cluster_num<<"\n";
  105. getchar();
  106. }else {
  107. getchar();
  108. }
  109. }
  110. }
  111.  
  112. // sort result
  113. std::sort(result_new.begin(), result_new.end(), by_cluster_num());
  114. std::string result_img ="img_"+ input_files[i].stem().string()+".txt";
  115. std::ofstream fs(result_img.c_str());
  116. if(!fs)
  117. {
  118. std::cerr<<"Cannot open the "<< result_img <<" file ."<<std::endl;
  119. return 1;
  120. }
  121. for(int k=0;k<result_new.size();k++){
  122. fs<<result_new[k].img_path<<","<<result_new[k].cluster_num<<"\n";
  123. }
  124. fs.close();
  125. }
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement