Skip to content

Instantly share code, notes, and snippets.

@rsmnarts
Created April 16, 2020 09:55
Show Gist options
  • Save rsmnarts/36a7c75c08735877d212b6daee6c83d8 to your computer and use it in GitHub Desktop.
Save rsmnarts/36a7c75c08735877d212b6daee6c83d8 to your computer and use it in GitHub Desktop.
const { createWorker } = require('tesseract.js');
const base64Img =
'iVBORw0KGgoAAAANSUhEUgAAAVcAAAB5CAYAAACN4GLqAAAElklEQVR42u3dUW7sOAxEUe9/054lPATTsljFc4H8JiqJumbbHet5AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABnef/wIyeuLcovfoCUepYz0zFjvBU7cOCjOpaTXOODA5PrV05yjQ8OTK1dOcm1IjgwsWblJNeK4MDEepWTXGuCA6dqp+UB0OSc5HpwggkWX9fQ1L+xMec6sT5P1hNEEOu0TkvO/tqKnVyCxTTh3NobW3Km1Vb0xBIsptboRLmm51wh1klXLXJF8icrOcl19KSSKyZ2VFPkumGuq+oq9YvTIFdyJVdyJVeEb/aTY9mSc11dkSvIlVzJdYFct308AemQa2m+VLkSLLm2bEqNTalXyBW61nvj8amxOBu5Aj4ut+e84hRyVZSwvuS6RGSnx/Sr3+kNX+S67fufulZyPVpA3lNLruSqa62V640C8iJwYnVLQNdKrj+Wq9MWyJVcZYqW6xfj+evvdpwNuboloGsl1x8XkbPCiFXXqmtdI9evrtD/dywESzhyLu1ap8l1youJn+fOf+IQrM0pZ9EFI+0kgi//1te3I8g1RzavnC4YCQNJOENdUZPqppeX+F5rgVynC+4tnHPcv8jKueA2x/QTJ28eJexAuL0STVwL31gZlm+63FrPaSfXHOnIqWsdO6Bpi570EA8+FssZup/e4T/Ni6B71dHJWbyXNkmVXMm1WUCb/1NwZKZtYp22EOSaV+Ny6lrj5LpRaOSaW+Ny6lpHy3X7YpBrtpDk1LWOkqsrHbk2SUhOe3lU52pBAq68JOspe8FtkJoBJiwWucL65OccP94Tk+qeq81LsHKuH+upQU4W7LYzw2CN2nJGjPOkAKcK1nhgjXJzxnTYpwdKrjYuwcq5ch2myHXz15/IlXTkLOtavxrsNMF6oAVy7ZVr1ES+H/4d7xYgV9KRs2KvTDy7ilzJlXTkjJ//qQcDkiuxko6c0U3IxtNPyRXWqk+ukRPZdkAgueZu8kbp+KpTYddKruSaWJMt0vFEvnyPTD8k8C1eLA+zsubkxguOtss1eo9M7yBvdc6uyDqn250ruYbvkYSHS+RKro33/bbkXPvJLkWub5nU3BLIuvDcfLmRrjV0f6R87/Tr79sqHHX5xVh0isXNR9J/TX35r7gNc54u7C0bndBKmw9ynfkQ7/TfUZtzxrAl57pbZomTmvqWrnfRZm2ozy3v3fBKUBN75R2zae9VaDu1dPpa+Zu61qgwpxf8y+PAvy7ghrPob2a6OX9bcq7oWsn1nGi/FHi7XE/P56212pyzvmudHOjk4r9Dfx5yjVk3OXWt0VeLU0XQKtZ2uT6LhEOs5Bop2L/8rrQibpfrQzh1OZ/WWiXXf/+upAJuF+st8cipa60MduPG/ukiV6wZ8pFT11q/Eb9+kn+qyKfMVyONUm3P6R0bCzdh6y0Va9w1L1tyYplcAQDkCgDkCgDkCgAgVwAgVwAgVwAgV3IFAHIFAHIFAHIlVwAgVwAgVwAgV3IFAHIFAHIFAAAAAAAAAAAAAOA4/wFujg1hTlObygAAAABJRU5ErkJggg==';
const worker = createWorker();
const getTextFromImage = async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data } = await worker.recognize(Buffer.from(base64Img, 'base64'));
await worker.terminate();
return data.text;
};
const getNumberOnly = (str) => str.replace(/\D/g, '');
getTextFromImage().then((v) => console.log(getNumberOnly(v)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment