Advertisement
Guest User

Untitled

a guest
Sep 4th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Imports the Google Cloud client library
  2. const vision = require('@google-cloud/vision');
  3.  
  4. // Creates a client
  5. const client = new vision.ImageAnnotatorClient({
  6.     projectId: '...',
  7.     keyFilename: 'key.json',
  8. });
  9. var x;
  10. var y;
  11. var highestX = 0;
  12. var highestY = 0;
  13. var whitespace;
  14. var newline;
  15. var word;
  16. var words = [];
  17. var wordCoordinates = [];
  18. var indexedWords = [];
  19. // Performs label detection on the image file
  20. client
  21.     .textDetection('words.jpg')
  22.     .then(results => {
  23.     const detections = results[0].textAnnotations;
  24.     detections.splice(0, 1);
  25.     //console.log(detections);
  26.     detections.forEach(function(text) {
  27.         if(text.locale == '') {
  28.             x = text.boundingPoly.vertices[0].x;
  29.             y = text.boundingPoly.vertices[0].y;
  30.             if(x > highestX){
  31.                 highestX = x;
  32.             }
  33.             if(y > highestY){
  34.                 highestY = y;
  35.             }
  36.             //console.log(text.description + ':' + text.boundingPoly.vertices[0].x);
  37.         }
  38.     });
  39.     detections.forEach(function(text) {
  40.         whitespace = highestX/10; //Whitespace can take 10% of total amount of X
  41.         newline = highestY/10; //Newline must be larger than 10% of Y
  42.         x = text.boundingPoly.vertices[0].x;
  43.         y = text.boundingPoly.vertices[0].y;
  44.         word = text.description;
  45.         wordCoordinates[word] = [x, y];
  46.         if(~indexedWords.indexOf(word) == false) {
  47.             words.push(word);
  48.         }
  49.         indexedWords.push(word);
  50.         detections.forEach(function(text) {
  51.             xNew = text.boundingPoly.vertices[0].x;
  52.             yNew = text.boundingPoly.vertices[0].y;
  53.             newWord = text.description;
  54.             if (Math.abs(x - xNew) < whitespace && Math.abs(y - yNew) < newline) {
  55.                 if(x !== xNew || y !== yNew) {
  56.                     if(~words.indexOf(newWord) == false) {
  57.                         if(~indexedWords.indexOf(newWord) == false) {
  58.                             console.log('words belong to each other ' + word + ' and ' + newWord);
  59.                             words.remove(word);
  60.                             words.remove(newWord);
  61.                             words.push(word + ' ' + newWord);
  62.                             indexedWords.push(newWord);
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.             if(Math.abs(y - yNew) < newline){
  68.                 if(x !== xNew || y !== yNew) {
  69.                     //console.log('words on the same line ' + word + ' and ' + newWord);
  70.                 }
  71.             }
  72.         });
  73.     });
  74.     test();
  75. })
  76. .catch(err => {
  77.     console.error('ERROR:', err);
  78. });
  79.  
  80. Array.prototype.remove = function() {
  81.     var what, a = arguments, L = a.length, ax;
  82.     while (L && this.length) {
  83.         what = a[--L];
  84.         while ((ax = this.indexOf(what)) !== -1) {
  85.             this.splice(ax, 1);
  86.         }
  87.     }
  88.     return this;
  89. };
  90.  
  91. function test() {
  92.     //Add words together
  93.     for(i = 1; i < words.length; i++){
  94.         var n = words[i-1].split(" ");
  95.         var lastPart = n[1];
  96.         var t = words[i].split(" ");
  97.         var firstPart = t[0];
  98.         if(lastPart == firstPart){
  99.             var word1 = words[i-1];
  100.             var word2 = words[i];
  101.             words.remove(word1);
  102.             words.remove(word2);
  103.  
  104.             var newWord = n[0] + " " + lastPart + " " + t[1];
  105.             console.log(newWord);
  106.             words.push(newWord);
  107.         }
  108.     }
  109.     console.log(words);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement