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

/**************************************************************************************************************************
文件说明:
         【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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值