【深度学习】【caffe实用工具5】笔记27 windows下SSD网络中的get_image_size工具的使用

来源:互联网 发布:简单的单片机设计作品 编辑:程序博客网 时间:2024/06/07 23:08

/**************************************************************************************************************************文件说明:         【1】This program retrieves the sizes of a set of images. 【2】这个程序用于检索一组图像的尺寸使用方法:         【1】get_image_size [FLAGS] ROOTFOLDER/ LISTFILE OUTFILE       【1】ROOTFOLDER/:图片集和检测任务中的注释文件所在文件夹的根目录   【2】LISTFILE:标签和图片的文件列表,例如:train.txt val.txt   【3】OUTFILE:生成的数据库存放的路径以及生成的数据库的名称,不用带后缀名   【4】对于【分类任务】,文件列表的格式是:imgfolder1/img1.JPEG 7        对于【检测任务】,文件列表的格式是:imgfolder1/img1.JPEG annofolder1/anno1.xml运行环境:        Win10+vs2013+cuda7.5+cuDnnv5.0+OpenCv时间地点:        陕西师范大学 文津楼 2017.8.10作    者:        九月***************************************************************************************************************************/#include <fstream>  #include <map>#include <string>#include <utility>#include <vector>#include "gflags/gflags.h"#include "glog/logging.h"#include "caffe/util/io.hpp"using namespace caffe;  DEFINE_string(name_id_file, "","A file which maps image_name to image_id.");int main(int argc, char** argv) {#ifdef USE_OPENCV  ::google::InitGoogleLogging(argv[0]);  // Print output to stderr (while still logging)  FLAGS_alsologtostderr = 1;#ifndef GFLAGS_GFLAGS_H_  namespace gflags = google;#endif  gflags::SetUsageMessage("Get sizes of a set of images.\n"  "Usage:\n"  "get_image_size ROOTFOLDER/ LISTFILE OUTFILE\n");    argv[1] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//";  argv[2] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//test.txt";  argv[3] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//test_name_size.txt";  std::ifstream infile(argv[2]);  if (!infile.good())   {    LOG(FATAL) << "Failed to open file: " << argv[2];  }  std::vector<std::pair<std::string, std::string> > lines;  std::string filename, label;  while (infile >> filename >> label)   {    lines.push_back(std::make_pair(filename, label));  }  infile.close();  LOG(INFO) << "A total of " << lines.size() << " images.";  const string name_id_file = FLAGS_name_id_file;  std::map<string, int> map_name_id;  if (!name_id_file.empty())   {    std::ifstream nameidfile(name_id_file.c_str());    if (!nameidfile.good()) {      LOG(FATAL) << "Failed to open name_id_file: " << name_id_file;    }    std::string name;    int         id;    while (nameidfile >> name >> id) {      CHECK(map_name_id.find(name) == map_name_id.end());      map_name_id[name] = id;    }    CHECK_EQ(map_name_id.size(), lines.size());  }  // Storing to outfile  boost::filesystem::path root_folder(argv[1]);  std::ofstream           outfile(argv[3]);  if (!outfile.good())   {    LOG(FATAL) << "Failed to open file: " << argv[3];  }  int    height;  int    width;  int    count = 0;  for (int line_id = 0; line_id < lines.size(); ++line_id)   {    boost::filesystem::path img_file = root_folder / lines[line_id].first;    GetImageSize(img_file.string(), &height, &width);    std::string img_name = img_file.stem().string();    if (map_name_id.size() == 0) {      outfile << img_name << " " << height << " " << width << std::endl;    } else {      CHECK(map_name_id.find(img_name) != map_name_id.end());      int img_id = map_name_id.find(img_name)->second;      outfile << img_id << " " << height << " " << width << std::endl;    }    if (++count % 1000 == 0) {      LOG(INFO) << "Processed " << count << " files.";    }  }  // write the last batch  if (count % 1000 != 0)   {    LOG(INFO) << "Processed " << count << " files.";  }  outfile.flush();  outfile.close();  std::system("pause");#else  LOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";#endif  // USE_OPENCV  return 0;}


阅读全文
0 0
原创粉丝点击