Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: display.cpp
- * Author: gachiemchiep
- *
- * Created on October 25, 2013, 11:29 AM
- */
- #include <cstdlib>
- #include "boost/filesystem.hpp"
- #include "boost/algorithm/string.hpp"
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <sstream>
- #include <fstream>
- #include "opencv2/opencv.hpp"
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "boost/lexical_cast.hpp"
- #include "boost/filesystem.hpp"
- using namespace std;
- struct video_cluster {
- boost::filesystem::path vid_path;
- boost::filesystem::path img_path;
- int cluster_num;
- };
- struct by_cluster_num{
- bool operator()(const video_cluster &a,const video_cluster &b) {
- return a.cluster_num < b.cluster_num;
- }
- };
- /*
- * This program convert result: vid,cluster into vid,img,cluster
- * It also make a img folder that contains all vid's snapshot
- *
- */
- int main(int argc, char** argv) {
- std::vector<boost::filesystem::path> input_files;
- for(int i=1;i<argc;i++){
- boost::filesystem::path tmp(argv[i]);
- input_files.push_back(tmp);
- std::cout << tmp << std::endl;
- }
- for(int i=0;i<input_files.size();i++){
- std::vector<video_cluster> result_new;
- std::ifstream infile(input_files[i].string().c_str());
- std::string line;
- std::vector<boost::filesystem::path> vids;
- std::vector<int> clusters;
- while(std::getline(infile, line)){
- std::vector<std::string> strs;
- boost::split(strs, line, boost::is_any_of(","));
- boost::filesystem::path p(strs[0]);
- vids.push_back(p);
- clusters.push_back(std::atoi(strs[1].c_str()));
- }
- // make img directory
- boost::filesystem::path dir("img");
- if(!boost::filesystem::create_directories(dir)){
- std::cout << "Can not create img dir \n";
- } else {
- std::cout << "Make img , move all image file into img directory \n";
- }
- cv::VideoWriter write;
- for(int j=0;j<vids.size();j++){
- std::cout << vids[j]<<" "<<clusters[j]<<std::endl;
- cv::VideoCapture cap(vids[j].string());
- cv::Mat frame;
- if(!cap.isOpened()){
- std::cout << vids[j] <<" can not be opened \n";
- } else {
- cap.set(CV_CAP_PROP_POS_FRAMES,120);
- cap >> frame;
- std::string name =vids[j].stem().string()+".png";
- std::cout<<"make " << name <<"\n";
- cv::imwrite(name,frame);
- // move to img
- boost::filesystem::path from(name);
- boost::filesystem::path to("img/"+name);
- boost::filesystem::rename(from ,to);
- video_cluster tmp;
- // tmp.vid_path = vids[j];
- tmp.img_path =to;
- tmp.cluster_num = clusters[j];
- std::cout << vids[j] <<" "<<to<<" "<<clusters[j]<<"\n";
- getchar();
- result_new.push_back(tmp);
- std::cout << result_new.size()<<"\n";
- if(!result_new.empty()){
- std::cout << result_new.back().img_path<<" "<<result_new.back().vid_path<<" "<<result_new.back().cluster_num<<"\n";
- std::cout<<"----------------------> " << result_new.back().img_path<<" "<<result_new.back().cluster_num<<"\n";
- getchar();
- }else {
- getchar();
- }
- }
- }
- // sort result
- std::sort(result_new.begin(), result_new.end(), by_cluster_num());
- std::string result_img ="img_"+ input_files[i].stem().string()+".txt";
- std::ofstream fs(result_img.c_str());
- if(!fs)
- {
- std::cerr<<"Cannot open the "<< result_img <<" file ."<<std::endl;
- return 1;
- }
- for(int k=0;k<result_new.size();k++){
- fs<<result_new[k].img_path<<","<<result_new[k].cluster_num<<"\n";
- }
- fs.close();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement