-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOCR.cpp
64 lines (50 loc) · 1.44 KB
/
OCR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "OCR.hpp"
void OCR::clearTrainingVector()
{
trainData.clear();
targetData.clear();
}
void OCR::addTrainingData(Mat& img, char target)
{
//Append target value
targetData.push_back(target);
//Append train data feature
Mat c, feat;
resize(img, c, cSize);
vector<float> descriptors;
hog.compute(c, descriptors);
trainData.push_back(Mat(descriptors).t());
}
void OCR::train()
{
//Organize training data into single matrix
Mat tDataMat(Size(0, trainData.size()), CV_32F);
for (Mat& fv : trainData)
tDataMat.push_back(fv);
//Organize target data
Mat targetMat(targetData);
targetMat.convertTo(targetMat, CV_32F);
//Train SVM
cout << "Started training" << endl;
svm.train(tDataMat, targetMat, Mat(), Mat(), params);
svm.save(fPath.c_str());
cout << "Finished training" << endl;
}
char OCR::recognize(Mat& img)
{
//Resize char img
Mat chr;
resize(img, chr, cSize);
//Get HOG feature vector
vector<float> descriptors;
hog.compute(chr, descriptors);
Mat desc = Mat(descriptors).t();
//Get SVM response
int rsp = cvRound(svm.predict(desc));
return char(rsp);
}
int main()
{
cout << "hi" << endl;
return 0;
}