-
Notifications
You must be signed in to change notification settings - Fork 1
/
coordinatesHelper.js
175 lines (150 loc) · 4.99 KB
/
coordinatesHelper.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
const inside = require('point-in-polygon');
const deepcopy = require("deepcopy");
/**
* @Method computes the maximum y coordinate from the identified text blob
* @param data
* @returns {*}
*/
function getYMax(data) {
let v = data.textAnnotations[0].boundingPoly.vertices;
let yArray = [];
for(let i=0; i <4; i++){
yArray.push(v[i]['y']);
}
return Math.max.apply(null, yArray);
}
/**
* @Method inverts the y axis coordinates for easier computation
* as the google vision starts the y axis from the bottom
* @param data
* @param yMax
* @returns {*}
*/
function invertAxis(data, yMax) {
data = fillMissingValues(data);
for(let i=1; i < data.textAnnotations.length; i++ ){
let v = data.textAnnotations[i].boundingPoly.vertices;
for(let j=0; j <4; j++){
v[j]['y'] = (yMax - v[j]['y']);
}
}
return data;
}
/**
* @Method sets zero to missing polygon coordinates. This behaviour has been observed in images where
* the text starts from the edge of the image. In such scenarios the x/y coordinates have been empty.
* @param data
* @returns {*}
*/
function fillMissingValues(data) {
for(let i=1; i < data.textAnnotations.length; i++ ){
let v = data.textAnnotations[i].boundingPoly.vertices;
v.map((ver) => {
if(ver['x'] === undefined){
ver['x'] = 0;
}
if(ver['y'] === undefined){
ver['y'] = 0;
}
});
}
return data;
}
/**
*
* @param mergedArray
*/
function getBoundingPolygon(mergedArray) {
for(let i=0; i< mergedArray.length; i++) {
let arr = [];
// calculate line height
let h1 = mergedArray[i].boundingPoly.vertices[0].y - mergedArray[i].boundingPoly.vertices[3].y;
let h2 = mergedArray[i].boundingPoly.vertices[1].y - mergedArray[i].boundingPoly.vertices[2].y;
let h = h1;
if(h2> h1) {
h = h2
}
let avgHeight = h * 0.6;
arr.push(mergedArray[i].boundingPoly.vertices[1]);
arr.push(mergedArray[i].boundingPoly.vertices[0]);
let line1 = getRectangle(deepcopy(arr), true, avgHeight, true);
arr = [];
arr.push(mergedArray[i].boundingPoly.vertices[2]);
arr.push(mergedArray[i].boundingPoly.vertices[3]);
let line2 = getRectangle(deepcopy(arr), true, avgHeight, false);
mergedArray[i]['bigbb'] = createRectCoordinates(line1, line2);
mergedArray[i]['lineNum'] = i;
mergedArray[i]['match'] = [];
mergedArray[i]['matched'] = false;
}
}
function combineBoundingPolygon(mergedArray) {
// select one word from the array
for(let i=0; i< mergedArray.length; i++) {
let bigBB = mergedArray[i]['bigbb'];
// iterate through all the array to find the match
for(let k=i; k< mergedArray.length; k++) {
// Do not compare with the own bounding box and which was not matched with a line
if(k !== i && mergedArray[k]['matched'] === false) {
let insideCount = 0;
for(let j=0; j < 4; j++) {
let coordinate = mergedArray[k].boundingPoly.vertices[j];
if(inside([coordinate.x, coordinate.y], bigBB)){
insideCount += 1;
}
}
// all four point were inside the big bb
if(insideCount === 4) {
let match = {matchCount: insideCount, matchLineNum: k};
mergedArray[i]['match'].push(match);
mergedArray[k]['matched'] = true;
}
}
}
}
}
function getRectangle(v, isRoundValues, avgHeight, isAdd) {
if(isAdd){
v[1].y = v[1].y + avgHeight;
v[0].y = v[0].y + avgHeight;
}else {
v[1].y = v[1].y - avgHeight;
v[0].y = v[0].y - avgHeight;
}
let yDiff = (v[1].y - v[0].y);
let xDiff = (v[1].x - v[0].x);
let gradient = yDiff / xDiff;
let xThreshMin = 1;
let xThreshMax = 2000;
let yMin;
let yMax;
if(gradient === 0) {
// extend the line
yMin = v[0].y;
yMax = v[0].y;
}else{
yMin = (v[0].y) - (gradient * (v[0].x - xThreshMin));
yMax = (v[0].y) + (gradient * (xThreshMax - v[0].x));
}
if(isRoundValues) {
yMin = Math.round(yMin);
yMax = Math.round(yMax);
}
return {xMin : xThreshMin, xMax : xThreshMax, yMin: yMin, yMax: yMax};
}
function createRectCoordinates(line1, line2) {
return [[line1.xMin, line1.yMin], [line1.xMax, line1.yMax], [line2.xMax, line2.yMax],[line2.xMin, line2.yMin]];
}
var exports = module.exports = {};
exports.getYMax = function (data) {
return getYMax(data);
};
exports.invertAxis = function (data, yMax) {
return invertAxis(data, yMax);
};
exports.getBoundingPolygon = function (mergedArray) {
return getBoundingPolygon(mergedArray);
};
exports.combineBoundingPolygon = function (mergedArray) {
return combineBoundingPolygon(mergedArray);
};