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

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?