Last active
February 6, 2024 09:13
-
-
Save code-boxx/06c042db6e5c6eedb10fc402371f3878 to your computer and use it in GitHub Desktop.
Revisions
-
code-boxx revised this gist
Aug 11, 2023 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,7 @@ ## JAVASCRIPT IMAGE TO TEXT WITH OCR * https://code-boxx.com/javascript-ocr-image-to-text/ * https://youtu.be/znsqLxIp3Gk * https://github.com/naptha/tesseract.js ## TEST IMAGE  -
code-boxx revised this gist
Aug 11, 2023 . 1 changed file with 15 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ window.addEventListener("load", () => { // (A) FETCH IMAGE fetch("text.png") .then(res => res.blob()) .then(async (blob) => { // (B) CREATE ENGLISH TESSERACT WORKER const worker = await Tesseract.createWorker(); await worker.loadLanguage("eng"); await worker.initialize("eng"); // (C) RESULT const res = await worker.recognize(blob); document.getElementById("result").value = res.data.text; }) .catch(err => console.error(err)); }); -
code-boxx revised this gist
Aug 10, 2023 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ /* NOT IMPORTANT - COSMETICS */ * { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; } body { max-width: 600px; margin: 0 auto; padding: 10px; } #select, #result, #vid, #go { width: 100%; padding: 10px; margin: 5px 0; } #select { border: 1px solid #ffb6b6; background: #ffe7e7; } #result { resize: none; height: 100px; border: 1px solid #c0bfff; background: #dceaff; } #vid { height: 400px; } #go { color: #fff; background: #5145b5; border: 0; cursor: pointer; } -
code-boxx revised this gist
Aug 10, 2023 . 6 changed files with 66 additions and 73 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,34 +3,17 @@ <head> <title>Image To Text - Select From File</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="x-dummy.css"> <!-- (A) LOAD TESSERACT & JS --> <!-- https://cdnjs.com/libraries/tesseract.js --> <script defer src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.1.1/tesseract.min.js"></script> <script defer src="1-select.js"></script> </head> <body> <!-- (B) FILE SELECTOR & RESULT --> <input type="file" id="select" accept="image/png, image/gif, image/webp, image/jpeg"> <textarea id="result"></textarea> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ window.addEventListener("load", async () => { // (A) GET HTML ELEMENTS const hSel = document.getElementById("select"), hRes = document.getElementById("result"); // (B) CREATE ENGLISH TESSERACT WORKER const worker = await Tesseract.createWorker(); await worker.loadLanguage("eng"); await worker.initialize("eng"); // (C) ON FILE SELECT - IMAGE TO TEXT hSel.onchange = async () => { const res = await worker.recognize(hSel.files[0]); hRes.value = res.data.text; }; }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,32 +3,16 @@ <head> <title>Image To Text - Fetch</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="x-dummy.css"> <!-- (A) LOAD TESSERACT & JS --> <!-- https://cdnjs.com/libraries/tesseract.js --> <script defer src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.1.1/tesseract.min.js"></script> <script defer src="2-fetch.js"></script> </head> <body> <!-- (B) RESULT --> <textarea id="result"></textarea> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ window.addEventListener("load", () => { // (A) FETCH IMAGE fetch("text.png") .then(res => res.blob()) .then(async (blob) => { // (B) CREATE ENGLISH TESSERACT WORKER const worker = await Tesseract.createWorker(); await worker.loadLanguage("eng"); await worker.initialize("eng"); // (C) RESULT const res = await worker.recognize(blob); document.getElementById("result").value = res.data.text; }) .catch(err => console.error(err)); }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,24 +3,18 @@ <head> <title>Image To Text - Webcam</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="x-dummy.css"> <!-- (A) LOAD TESSERACT & JS --> <!-- https://cdnjs.com/libraries/tesseract.js --> <script defer src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.1.1/tesseract.min.js"></script> <script defer src="3-cam.js"></script> </head> <body> <!-- (B) WEBCAM & RESULT --> <video id="vid" autoplay></video> <button id="go">Go!</button> <textarea id="result"></textarea> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,14 +4,14 @@ var webkam = { hVid : null, hGo :null, hRes : null, // html elements init : () => { // (A1) GET HTML ELEMENTS webkam.hVid = document.getElementById("vid"), webkam.hGo = document.getElementById("go"), webkam.hRes = document.getElementById("result"); // (A2) GET USER PERMISSION TO ACCESS CAMERA navigator.mediaDevices.getUserMedia({ video: true }) .then(async (stream) => { // (A2-1) CREATE ENGLISH TESSERACT WORKER webkam.worker = await Tesseract.createWorker(); await webkam.worker.loadLanguage("eng"); await webkam.worker.initialize("eng"); @@ -38,7 +38,7 @@ var webkam = { // (B3) CANVAS TO IMAGE, IMAGE TO TEXT const res = await webkam.worker.recognize(canvas.toDataURL("image/png")); webkam.hRes.value = res.data.text; }, }; window.addEventListener("load", webkam.init); -
code-boxx revised this gist
May 26, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ## JAVASCRIPT IMAGE TO TEXT WITH OCR https://code-boxx.com/javascript-ocr-image-to-text/ ## TESSERACT.JS -
code-boxx revised this gist
May 18, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ ## FULL TUTORIAL https://code-boxx.com/javascript-ocr-image-to-text/ ## TESSERACT.JS https://github.com/naptha/tesseract.js -
code-boxx created this gist
May 18, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ ## FULL TUTORIAL https://code-boxx.com/?p=20038&preview=true ## TESSERACT.JS https://github.com/naptha/tesseract.js ## TEST IMAGE  ## LICENSE Copyright by Code Boxx Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ <!DOCTYPE html> <html> <head> <title>Image To Text - Select From File</title> <meta charset="utf-8"> </head> <body> <!-- (A) FILE SELECTOR & RESULT --> <input type="file" id="select" accept="image/png, image/gif, image/webp, image/jpeg"> <div id="result"></div> <!-- (B) LOAD TESSERACT --> <!-- https://cdnjs.com/libraries/tesseract.js --> <script src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.0.6/tesseract.min.js"></script> <!-- (C) INIT --> <script> window.addEventListener("load", async () => { // (C1) GET HTML ELEMENTS const hSel = document.getElementById("select"), hRes = document.getElementById("result"); // (C2) CREATE ENGLISH WORKER const worker = await Tesseract.createWorker(); await worker.loadLanguage("eng"); await worker.initialize("eng"); // (C3) ON FILE SELECT - IMAGE TO TEXT hSel.onchange = async () => { const res = await worker.recognize(hSel.files[0]); hRes.innerHTML = res.data.text; }; }); </script> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ <!DOCTYPE html> <html> <head> <title>Image To Text - Fetch</title> <meta charset="utf-8"> </head> <body> <!-- (A) RESULT --> <div id="result"></div> <!-- (B) LOAD TESSERACT --> <!-- https://cdnjs.com/libraries/tesseract.js --> <script src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.0.6/tesseract.min.js"></script> <!-- (C) FETCH IMAGE & SHOW TEXT --> <script> window.addEventListener("load", () => { // (C1) FETCH IMAGE fetch("text.png") .then(res => res.blob()) .then(async (blob) => { // (C2) CREATE ENGLISH WORKER const worker = await Tesseract.createWorker(); await worker.loadLanguage("eng"); await worker.initialize("eng"); // (C3) RESULT const res = await worker.recognize(blob); document.getElementById("result").innerHTML = res.data.text; }); }); </script> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ <!DOCTYPE html> <html> <head> <title>Image To Text - Webcam</title> <meta charset="utf-8"> <style> * { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; } body { max-width: 600px; margin: 0 auto; } video, button, div { width: 100%; } video { height: 400px; } button, div { padding: 10px; } button { color: #fff; background: #5145b5; border: 0; cursor: pointer; } </style> </head> <body> <!-- (A) WEBCAM & RESULT --> <video id="camVid" autoplay></video> <button id="camGo">Go!</button> <div id="camRes"></div> <!-- (B) LOAD TESSERACT & JS --> <!-- https://cdnjs.com/libraries/tesseract.js --> <script src="https://cdnjs.cloudflare.com/ajax/libs/tesseract.js/4.0.6/tesseract.min.js"></script> <script src="3-cam.js"></script> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ var webkam = { // (A) INITIALIZE worker : null, // tesseract worker hVid : null, hGo :null, hRes : null, // html elements init : () => { // (A1) GET HTML ELEMENTS webkam.hVid = document.getElementById("camVid"), webkam.hGo = document.getElementById("camGo"), webkam.hRes = document.getElementById("camRes"); // (A2) GET USER PERMISSION TO ACCESS CAMERA navigator.mediaDevices.getUserMedia({ video: true }) .then(async (stream) => { // (A2-1) CREATE ENGLISH WORKER webkam.worker = await Tesseract.createWorker(); await webkam.worker.loadLanguage("eng"); await webkam.worker.initialize("eng"); // (A2-2) WEBCAM LIVE STREAM webkam.hVid.srcObject = stream; webkam.hGo.onclick = webkam.snap; }) .catch(err => console.error(err)); }, // (B) SNAP VIDEO FRAME TO TEXT snap : async () => { // (B1) CREATE NEW CANVAS let canvas = document.createElement("canvas"), ctx = canvas.getContext("2d"), vWidth = webkam.hVid.videoWidth, vHeight = webkam.hVid.videoHeight; // (B2) CAPTURE VIDEO FRAME TO CANVAS canvas.width = vWidth; canvas.height = vHeight; ctx.drawImage(webkam.hVid, 0, 0, vWidth, vHeight); // (B3) CANVAS TO IMAGE, IMAGE TO TEXT const res = await webkam.worker.recognize(canvas.toDataURL("image/png")); webkam.hRes.innerHTML = res.data.text; }, }; window.addEventListener("load", webkam.init);