-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptchatrain.cpp
More file actions
executable file
·132 lines (100 loc) · 3.04 KB
/
Copy pathcaptchatrain.cpp
File metadata and controls
executable file
·132 lines (100 loc) · 3.04 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <highgui.h>
#include <cv.h>
#include <ml.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char * argv[]){
if(argc<3){
cout << "Usage: ./" << argv[0] << " < Outfile Net > < Train Case 1 >...< Train Case N >" << endl;
exit(-1);
}
//Red neuronal
CvANN_MLP red;
//Matrices
Mat input, output, src, dst;
//Shuffling data
int arr[argc-2];
for(int i=0;i<argc-2;i++){
arr[i]=i+2;
}
srand(time (NULL));
int swap1, swap2, temp;
for(int i=0; i<10000; i++){
swap1=rand()%(argc-2);
swap2=rand()%(argc-2);
if(swap1==swap2){
i--;
}else{
temp= arr[swap1];
arr[swap1]=arr[swap2];
arr[swap2]=temp;
}
}
/*for(int i=0; i<argc-2;i++){
cout << arr[i] << endl;
}*/
for(int i =0; i<argc-2; i++){
//Se leen las imagenes por argumentos
src = imread(argv[arr[i]],CV_LOAD_IMAGE_GRAYSCALE);
//cout << argv[i][6] << argv[i][6]-'A' << endl;
Mat out(1,26,CV_32FC1,Scalar(-1));
//cout << argv[arr[i]][18] << endl;
out.at<float>(argv[arr[i]][18]-'A')=1;
output.push_back(out);
//cout << out<<endl;
if(!src.data){
cout << "Error al leer la imagen " << argv[arr[i]] << endl;
continue;
}
//cout<<src<<endl;
src = src.reshape(0,1);
input.push_back(src);
//cout << src<<endl;
}
//The number of training samples.
int train_sample_count = input.rows;
//Get the number of samples.
printf("Found training file with %d samples...\n", train_sample_count);
//Create the matrices
//Input data samples. Matrix of order (train_sample_count x 2)
Mat trainData;
input.convertTo(trainData,CV_32FC1);
//Output data samples. Matrix of order (train_sample_count x 1)
Mat trainClasses = Mat(output);
//The weight of each training data sample. We'll later set all to equal
//weights.
Mat sampleWts = Mat::ones(train_sample_count, 1, CV_32FC1);
//The matrix representation of our ANN. We'll have 3 layers.
Mat neuralLayers(3, 1, CV_32SC1);
//Setting the number of neurons on each layer of the ANN
/*We have in Layer 1: 1600 neurons (2 inputs)
Layer 2: 150 neurons (hidden layer)
Layer 3: 26 neurons (1 output)
*/
neuralLayers.at<int>(0)=1600;
neuralLayers.at<int>(1)=150;
neuralLayers.at<int>(2)=26;
//Create our ANN.
red.create(neuralLayers);
//Train it with our data.
red.train(
trainData,
trainClasses,
sampleWts,
Mat(),
CvANN_MLP_TrainParams(
cvTermCriteria(
CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
100000,
1.0
),
CvANN_MLP_TrainParams::BACKPROP,
0.01,
0.05
)
);
red.save(argv[1]);
}