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