javascript - 添加 url 作为使用 Tesseract OCR 查询的参数

标签 javascript php jquery ajax

我正在使用tesseract ocr,它工作得很好。但我的问题是我可以使用 url 作为参数运行 tesseract 吗?

我希望执行以下操作

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

演示的一些图像网址:

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

处理结果后,结果将出现在文本区域框中。

这是一个代码:

<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>

最佳答案

您可以执行localhost/test.html?image=/image/mrOsS.png

你可以从 JavaScript 中的 URL 获取图像,如下所示:

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

myImage 变量将为:“/image/mrOsS.png ”,然后您可以将其传递给 OCR 方法。

示例代码为:

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

这是包含更新代码的链接:https://gist.github.com/kolarski/0bc2a3feb02adb1b63016d0d78b3653c

关于javascript - 添加 url 作为使用 Tesseract OCR 查询的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55979718/

相关文章:

javascript - 有没有办法等待/延迟渲染,直到 #Each-Helper 使用提供的模型完成?

javascript - 将 php 变量从 Controller 传递到 javascript 函数

javascript - Prestashop:修改mailalert模块,使产品数量更加可见

javascript - Android 上的 React Native - 带图片的 ScrollView 性能糟糕

php - laravel 在验证 FormRequest 后返回错误而不是重定向的 json

php - 奇怪的数组语法

php - 在 mysql 提取之前或之后添加值?

jquery - 单击导航图标停止/重置 &lt;iframe&gt; 视频

javascript - 将鼠标悬停在任何段落上,附加带有小消息的 div,将鼠标悬停,它会淡出,对吗?

jquery - 使用循环从 localStorage 中删除键