Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Imports the Google Cloud client library
- const vision = require('@google-cloud/vision');
- // Creates a client
- const client = new vision.ImageAnnotatorClient({
- projectId: '...',
- keyFilename: 'key.json',
- });
- var x;
- var y;
- var highestX = 0;
- var highestY = 0;
- var whitespace;
- var newline;
- var word;
- var words = [];
- var wordCoordinates = [];
- var indexedWords = [];
- // Performs label detection on the image file
- client
- .textDetection('words.jpg')
- .then(results => {
- const detections = results[0].textAnnotations;
- detections.splice(0, 1);
- //console.log(detections);
- detections.forEach(function(text) {
- if(text.locale == '') {
- x = text.boundingPoly.vertices[0].x;
- y = text.boundingPoly.vertices[0].y;
- if(x > highestX){
- highestX = x;
- }
- if(y > highestY){
- highestY = y;
- }
- //console.log(text.description + ':' + text.boundingPoly.vertices[0].x);
- }
- });
- detections.forEach(function(text) {
- whitespace = highestX/10; //Whitespace can take 10% of total amount of X
- newline = highestY/10; //Newline must be larger than 10% of Y
- x = text.boundingPoly.vertices[0].x;
- y = text.boundingPoly.vertices[0].y;
- word = text.description;
- wordCoordinates[word] = [x, y];
- if(~indexedWords.indexOf(word) == false) {
- words.push(word);
- }
- indexedWords.push(word);
- detections.forEach(function(text) {
- xNew = text.boundingPoly.vertices[0].x;
- yNew = text.boundingPoly.vertices[0].y;
- newWord = text.description;
- if (Math.abs(x - xNew) < whitespace && Math.abs(y - yNew) < newline) {
- if(x !== xNew || y !== yNew) {
- if(~words.indexOf(newWord) == false) {
- if(~indexedWords.indexOf(newWord) == false) {
- console.log('words belong to each other ' + word + ' and ' + newWord);
- words.remove(word);
- words.remove(newWord);
- words.push(word + ' ' + newWord);
- indexedWords.push(newWord);
- }
- }
- }
- }
- if(Math.abs(y - yNew) < newline){
- if(x !== xNew || y !== yNew) {
- //console.log('words on the same line ' + word + ' and ' + newWord);
- }
- }
- });
- });
- test();
- })
- .catch(err => {
- console.error('ERROR:', err);
- });
- Array.prototype.remove = function() {
- var what, a = arguments, L = a.length, ax;
- while (L && this.length) {
- what = a[--L];
- while ((ax = this.indexOf(what)) !== -1) {
- this.splice(ax, 1);
- }
- }
- return this;
- };
- function test() {
- //Add words together
- for(i = 1; i < words.length; i++){
- var n = words[i-1].split(" ");
- var lastPart = n[1];
- var t = words[i].split(" ");
- var firstPart = t[0];
- if(lastPart == firstPart){
- var word1 = words[i-1];
- var word2 = words[i];
- words.remove(word1);
- words.remove(word2);
- var newWord = n[0] + " " + lastPart + " " + t[1];
- console.log(newWord);
- words.push(newWord);
- }
- }
- console.log(words);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement