Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active February 6, 2024 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save code-boxx/06c042db6e5c6eedb10fc402371f3878 to your computer and use it in GitHub Desktop.
Save code-boxx/06c042db6e5c6eedb10fc402371f3878 to your computer and use it in GitHub Desktop.
Javascript OCR Image To Text

JAVASCRIPT IMAGE TO TEXT WITH OCR

TEST IMAGE

text

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.

<!DOCTYPE html>
<html>
<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>
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;
};
});
<!DOCTYPE html>
<html>
<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>
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));
});
<!DOCTYPE html>
<html>
<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>
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("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");
// (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.value = res.data.text;
},
};
window.addEventListener("load", webkam.init);
/* 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; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment