巧用ifstream判断文件是否存在
2016-03-25 21:46
281 查看
最近在写手写数字的识别软件,训练样例数量巨大而且数字个数不唯一,有可能在中途粘出一部分做测试样例。因此写下面的脚本来获取文件名,之后丢到训练函数里。
#include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> #include <opencv.hpp> #include <opencv2/opencv.hpp> #include <opencv2/ml/ml.hpp> #include <opencv2/core/core.hpp> #include <opencv_modules.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #define cvQueryHistValue_1D( hist, idx0 ) \ ((float)cvGetReal1D( (hist)->bins, (idx0))) using namespace std; using namespace cv; const string rootDir("./sample/"); const string jpg(".jpg"); const string dirNames[11] = { rootDir + "0/", rootDir + "1/", rootDir + "2/", rootDir + "3/", rootDir + "4/", rootDir + "5/", rootDir + "6/", rootDir + "7/", rootDir + "8/", rootDir + "9/" }; inline void i2s(string& str, int i, int len = 5) { stringstream ss; ss << setw(len) << setfill('0') << i; str = ss.str(); } void train(string fileName) { } void getFile(string dirName, int num) { static int sum = 0; string tmp; string fileName; string snum; stringstream ss; int cur = 1; tmp.clear(); ss << num; ss >> snum; ifstream fileRead; for (; ; cur++) { i2s(tmp, cur); fileName = dirNames[num] + snum + "_" + tmp + jpg; if (cur == 1) cout << "start file name : " << fileName << endl; fileRead.open(fileName); if (!fileRead) { cout << "end file name : " << fileName << endl; break; } fileRead.close(); train(fileName); } sum += cur - 1; cout << "current number of samples : " << sum << endl << endl; } int main() { int i = 0; for (int i = 0; i != 10; i++) { getFile(dirNames[i], i); } return 0; }
![](https://images2015.cnblogs.com/blog/758450/201603/758450-20160325214545433-2021413290.jpg)
相关文章推荐
- 探讨Nodejs中的作用域问题。
- linux(centos)搭建SVN服务器
- WPF学习——依赖项属性(3)
- Java设计模式之“工厂方法模式”
- MapReduce处理多个不同的出入文件
- Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
- ExtJS获取父子、兄弟容器元素方法
- 20145214 《Java程序设计》第4周学习总结
- objective-C学习笔记(九)ARC
- 设置一个虚拟域名使内网其他用户也可以通过虚拟域名访问
- 用shell脚本监控进程是否存在 不存在则启动的实例
- poj3904 Sky Code【容斥原理】
- Ubuntu下编译linux内核,报"mkimage" command not found错的解决
- 无人机矩阵操作系统
- PHP include 和 require 语句
- 在Java中使用Json
- 群发微信图文消息,但是正文中的图片却不显示
- KMP训练五题
- linker command failed with exit code 1 (use -v to see invocation)
- Python reverse order