dsa111111 2019-05-04 05:22
浏览 447
已采纳

使用Tesseract OCR将url添加为查询参数

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>

  • 写回答

1条回答 默认 最新

  • dtt2012 2019-05-04 05:50
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料