简体   繁体   中英

Add url as parameter for query using Tesseract OCR

I am using tesseract ocr and it is working perfectly. But my question is can I run tesseract with a url as parameter.

I am looking to do the following

localhost/test.html/?othersite.com/image/image2.jpg 

Some Image url for demo:

 1. https://i.imgur.com/leBXjxq.png
 2. https://i.imgur.com/7u9LyF6.png

when the results are processed it would then come to a text-area box.

Here's a code :

<html>
  <head>
     <title>Tesseract-JS Demo</title>
  </head>
  <body>
     <input type="text" id="url" placeholder="Image URL" />

     <!--<div id="ocr_results"> </div>-->
     <div id="ocr_status"> </div>
     <div>
        <label>Filed1
        <label>
           <textarea id="txt" ></textarea>
     </div>


  </body>

 <script src='https://cdn.rawgit.com/naptha/tesseract.js
 /1.0.10/dist/tesseract.js'></script>
  <script 

  src="https://cdnjs.cloudflare.com/ajax/
  libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
  function runOCR(url) {
  Tesseract.recognize(url)
  .then(function(result) {
  document.getElementById("txt")
  .innerHTML = result.text;
  document.getElementById('txt').focus();

  }).progress(function(result) {
  document.getElementById("ocr_status")
  .innerText = result["status"] + " (" +
  (result["progress"] * 100) + "%)";
  });
  }
  document.getElementById("url")
  .addEventListener("change", function(e) {
  var url = document.getElementById("url").value;
  runOCR(url);

  });


 </script>

You can do localhost/test.html?image=https://i.imgur.com/leBXjxq.png

And you can get the image from the URL in JavaScript like so:

const urlParams = new URLSearchParams(window.location.search);
const myImage = urlParams.get('image');

myImage variable will be: " https://i.imgur.com/leBXjxq.png " and then you can pass it to your OCR method.

A sample code will be:

const urlParams = new URLSearchParams(window.location.search);
const myImage = urlParams.get('image');
if (myImage) {
    runOCR(myImage);
}

Here is a link with updated code: https://gist.github.com/kolarski/0bc2a3feb02adb1b63016d0d78b3653c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM