巧用ifstream判断文件是否存在

  最近在写手写数字的识别软件,训练样例数量巨大而且数字个数不唯一,有可能在中途粘出一部分做测试样例。因此写下面的脚本来获取文件名,之后丢到训练函数里。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 #include <opencv.hpp>
21 #include <opencv2/opencv.hpp>
22 #include <opencv2/ml/ml.hpp>
23 #include <opencv2/core/core.hpp>
24 #include <opencv_modules.hpp>
25 #include <opencv2/highgui/highgui.hpp>
26 #include <opencv2/imgproc/imgproc.hpp>
27 
28 #define cvQueryHistValue_1D( hist, idx0 ) \
29     ((float)cvGetReal1D( (hist)->bins, (idx0)))
30 
31 
32 using namespace std;
33 using namespace cv;
34 
35 const string rootDir("./sample/");
36 const string jpg(".jpg");
37 const string dirNames[11] = {
38     rootDir + "0/", rootDir + "1/", rootDir + "2/", 
39     rootDir + "3/", rootDir + "4/", rootDir + "5/", 
40     rootDir + "6/", rootDir + "7/", rootDir + "8/", 
41     rootDir + "9/"
42 };
43 
44 inline void i2s(string& str, int i, int len = 5) {
45     stringstream ss;
46     ss << setw(len) << setfill('0') << i;
47     str = ss.str();
48 }
49 
50 void train(string fileName) {
51 
52 }
53 
54 void getFile(string dirName, int num) {
55     static int sum = 0;
56     string tmp;
57     string fileName;
58     string snum;
59     stringstream ss;
60     int cur = 1;
61 
62     tmp.clear();
63     ss << num; ss >> snum;
64     ifstream fileRead;
65     for (; ; cur++) {
66         i2s(tmp, cur);
67         fileName = dirNames[num] + snum + "_" + tmp + jpg;
68         if (cur == 1) cout << "start file name : " << fileName << endl;
69         fileRead.open(fileName);
70         if (!fileRead) {
71             cout << "end file name : " << fileName << endl;
72             break;
73         }
74         fileRead.close();
75         train(fileName);
76     }
77     sum += cur - 1;
78     cout << "current number of samples : " << sum << endl << endl;
79 }
80 
81 
82 int main() {
83     int i = 0;
84     for (int i = 0; i != 10; i++) {
85         getFile(dirNames[i], i);
86     }
87     return 0;
88 }

 

 

转载于:https://www.cnblogs.com/kirai/p/5321298.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值