我正在使用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/