-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.js
194 lines (151 loc) · 5.8 KB
/
ocr.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
function recognizeText(){
//var image_data = canvasContext.getImageData(0, 0,canvasContext.canvas.width,canvasContext.canvas.height);
// var image_data = canvasContext.getImageData(0, 0, canvas.width, canvas.height);
// document.getElementById("debug-image").src=canvas.toDataURL();
var parsedText=extractTextByCell(LAST_UPDATED_POSITION.x,LAST_UPDATED_POSITION.y)
// if(parsedText=="vs"){
// alert("Computational Paper\nVERSION: "+SETTINGS.version);
// return "";
// }
return parsedText;
}
function extractTextByCanvas(){
var image_data = canvasContext.getImageData(0,0,canvas.width,canvas.height);
return OCRAD(image_data);
}
function coordinatesToGrid(x,y){
var i=Math.floor(x/SETTINGS.gridSize);
var j=Math.floor(y/SETTINGS.gridSize);
return {x:i,y:j};
}
function extractTextByCell(x,y){
var gridSize=SETTINGS.gridSize;
var coor=coordinatesToGrid(x,y);
var i=coor.x;
var j=coor.y;
LAST_GRID_UPDATED={x:i,y:j}
var image_data = canvasContext.getImageData(gridSize*i,gridSize*j,gridSize,gridSize);
var ocrText=OCRAD(image_data);
var result=parseText(ocrText);
if(result==""&&SETTINGS.eraseErrors){
clearGrid(i,j);
}
drawTextGrid(result,i,j+1);
GRID[j][i]=result;
return result;
}
function drawGridSquare(i,j){
var gridSize=SETTINGS.gridSize;
canvasContext.strokeStyle = "#ff0000";
canvasContext.strokeRect(gridSize*i,gridSize*j,gridSize,gridSize);
}
function extractTextByCells(){
var arr=[];
for(var i=0;i<canvas.width/SETTINGS.gridSize;i++){
var temp=[];
for(var j=0;j<canvas.height/SETTINGS.gridSize;j++){
var image_data = canvasContext.getImageData(i*SETTINGS.gridSize,j*SETTINGS.gridSize, (i+1)*SETTINGS.gridSize,(j+1)*SETTINGS.gridSize);
temp.push(OCRAD(image_data));
}
arr.push(temp);
}
return arr;
}
function extractTextByLines(){
var arr=[];
for(var i=0;i<canvas.width/SETTINGS.gridSize;i++){
var image_data = canvasContext.getImageData(i*SETTINGS.gridSize+1, 0+1, (i+1)*SETTINGS.gridSize-1,canvas.width-1);
arr.push(OCRAD(image_data));
}
return arr;
}
function finishedDrawingLine(){
recognizeGesture();
finishedDrawingEquation();
}
function recognizeGesture(){
//Long Full Erase Swipe
if(LAST_UPDATED_POSITION.x<2*SETTINGS.gridSize&&
LAST_UPDATED_POSITION.y<2*SETTINGS.gridSize&&
FINAL_UPDATED_POSITION.x>(SETTINGS.gridDimensions.x-1)*SETTINGS.gridSize&&
FINAL_UPDATED_POSITION.y>(SETTINGS.gridDimensions.y-1)*SETTINGS.gridSize
){
clearCanvas();
}else
if(Math.sqrt(Math.pow(LAST_UPDATED_POSITION.x-FINAL_UPDATED_POSITION.x,2)+
Math.pow(LAST_UPDATED_POSITION.y-FINAL_UPDATED_POSITION.y,2))>1.2*Math.sqrt(2*Math.pow(SETTINGS.gridSize,2))){//Drawing
//Let it Draw
}else{//Single Character
if(Math.abs(LAST_UPDATED_POSITION.x-FINAL_UPDATED_POSITION.x)>=0.8*SETTINGS.gridSize&&
Math.abs(LAST_UPDATED_POSITION.y-FINAL_UPDATED_POSITION.y)>=0.8*SETTINGS.gridSize){//Erase Grid
//Calcualte average position to find grid
var avgX=(LAST_UPDATED_POSITION.x+FINAL_UPDATED_POSITION.x)/2;
var avgY=(LAST_UPDATED_POSITION.y+FINAL_UPDATED_POSITION.y)/2;
var coor=coordinatesToGrid(avgX,avgY)
clearGrid(coor.x,coor.y);
}else
if(Math.abs(LAST_UPDATED_POSITION.y-FINAL_UPDATED_POSITION.y)<SETTINGS.gridSize/10){
var coor=coordinatesToGrid(LAST_UPDATED_POSITION.x,LAST_UPDATED_POSITION.y);
if(Math.abs(LAST_UPDATED_POSITION.x-FINAL_UPDATED_POSITION.x)>=SETTINGS.gridSize){//Erase Grid
//Calcualte average position to find grid
var maxP=Math.max(LAST_UPDATED_POSITION.x,FINAL_UPDATED_POSITION.x);
var maxP=Math.max(LAST_UPDATED_POSITION.x,FINAL_UPDATED_POSITION.x);
var coor=coordinatesToGrid(LAST_UPDATED_POSITION.x,LAST_UPDATED_POSITION.y);
clearGrid(coor.x,coor.y);
}else{//Special Symbols
if(LAST_GRID_UPDATED.x==coor.x&&LAST_GRID_UPDATED.y==coor.y){//Two horizontal lines
overWriteCell("=",coor.x,coor.y);
}else{//Single horizontal line
overWriteCell("-",coor.x,coor.y);
}
LAST_GRID_UPDATED={x:coor.x,y:coor.y};
}
}else{
recognizeTextDebounced();
}
}
}
function overWriteCell(txt,i,j){
GRID[j][i]=txt;
drawTextGrid(txt,i,j+1);
LAST_GRID_UPDATED={x:0,y:0};
}
var recognizeTextDebounced = debounce(function() {
recognizeText();
},0);
var finishedDrawingEquation = debounce(function() {
computeAndWrite();
},3000);
function computeAndWrite(){
var statements=generateStatements();
console.log(statements);
var evaled=evaluate(statements);
console.log(evaled);
evaled.map(function(e){
for(var i=0;i<e.location.length;i++){
clearGrid(i+e.location.x,e.location.y+1);
}
var statementOutput=getExprAsText(e.statement);
if(statementOutput.indexOf("y=")==0){//GRAPHING TIME !!!
drawGraphResize(e);
}else{
drawTextGrid(statementOutput,e.location.x,e.location.y+1);
}
});
}
//https://davidwalsh.name/javascript-debounce-function
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
//document.getElementById("ocr-button").onclick=recognizeText;