-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
solver.js
169 lines (128 loc) · 3.84 KB
/
solver.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//Hiding the Problem Statement
try{
let form = document.getElementById('formVerify1')
form.style.display='none'
//Changing Title and adding .gif
let status = document.getElementsByClassName('barTitle')[0];
document.title = "Solving C.A.P.T.C.H.A.";
let gif = document.createElement('img');
gif.src = 'https://raw.githubusercontent.com/ra101/kissSolver/core/assets/loading.gif';
gif.style.marginTop = '5%';
document.getElementsByClassName('barContent')[0].appendChild(gif);
//Extracting Hints
status.innerText = 'Extracting Hints'
let hints =[];
hints.push(form.getElementsByTagName('span')[0].innerText.trim().split(', '));
hints.push(form.getElementsByTagName('span')[1].innerText.trim().split(', '));
status.innerText = 'Extracting Images'
let options = form.getElementsByTagName('img');
//Match to database using variable in imageData.js
status.innerText = 'Matching Database';
let answers=[[]];
answers.push([]);
try{
for(let i=0;i<2;i++){ //i=0,1
let possiblities = []
for (let j=0;j<tagPossiblity[hints[i][0]].length;j++){
for (let k=0;k<tagPossiblity[hints[i][1]].length;k++){
if(tagPossiblity[hints[i][0]][j]==tagPossiblity[hints[i][1]][k]){
possiblities.push(tagPossiblity[hints[i][0]][j])
break;
}
}
}
for(let j=0;j<options.length;j++){
dim = options[j].naturalWidth+','+options[j].naturalHeight
for(let k=0;k<possiblities.length;k++){
if(getDimension[possiblities[k]] == dim){
answers[i].push(j);
break;
}
}
}
}
}
catch(e){
;
}
if(answers[0].length ==0 || answers[0].length==0){
location.reload()
}
//Loading Haar Cascade
status.innerText = 'Loading Number Detector'
let haarCascade = new cv.CascadeClassifier();
// there exist any link
try{
cv.FS_unlink('cascade.xml')
}
catch(e){
;
}
//cascadeData is defined in /cascade/haarcascade_kiss_no.js
cv.FS_createDataFile('/', 'cascade.xml',cascadeData , true, false, false);
/*
let utils = new Utils('errorMessage');
utils.createFileFromUrl('cascade.xml', 'https://raw.githubusercontent.com/ra101/kissSolver/core/haar_cascade/cascade.xml', () => {
});
*/
haarCascade.load('cascade.xml');
let cnn;
(async () => {
cnn = await tf.loadLayersModel('https://raw.githubusercontent.com/ra101/kissSolver/core/cnn/model.json');
//// for Best Performance:
status.innerText = 'Solving C.A.P.T.C.H.'
let msize = new cv.Size(0, 0);
let dsize = new cv.Size(45, 55);
let number = new cv.RectVector();
let undetected =[[]];
undetected.push([]);
let count = 0
let n =options.length;
for (let i=0;i<2;i++){
for(let j=0;j<answers[i].length;j++){
let cvBatch = cv.imread(options[answers[i][j]])
let grayBatch = new cv.Mat();
cv.cvtColor(cvBatch, grayBatch, cv.COLOR_RGBA2GRAY, 0);
haarCascade.detectMultiScale(grayBatch, number);
if(number.size()){
let roi = grayBatch.roi(number.get(0));
cv.resize(roi,roi,dsize);
var cnn_input = tf.tensor(roi.data).reshape([1,55,45,1]);
var ok = cnn.predict(cnn_input,{batchSize: 1});
//(ok.argMax(1).dataSync()[0]) is the no with highest probablity
if(hints[i][2]!=(ok.argMax(1).dataSync()[0])){
answers[i].splice(j,1)
j=j-1;
}
}
else{
undetected[i].push(answers[i][j]);
answers[i].splice(j,1)
j=j-1;
}
}
}
//cascadeData.delete()
haarCascade.delete()
number.delete()
let reloadCounter = 0;
//if there are more than one option, and one option is undetected and rest are detected but wrong
for(let i=0;i<2;i++){
if(answers[i].length == 0 && undetected[i].length == 1){
answers[i].push(undetected[i][0]);
}
}
for(let i=0;i<2;i++){
if(answers[i].length == 1){
options[answers[i][0]].click();
reloadCounter += i+1; //0->1 and 1->2 therfore for 1&2->3
}
}
if(reloadCounter<3){
location.reload()
}
})()
}
catch(e){
;
}