首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用tesseract.js识别彩票上的日期和数字?

如何使用tesseract.js识别彩票上的日期和数字?
EN

Stack Overflow用户
提问于 2019-07-18 16:46:18
回答 1查看 2K关注 0票数 3

我的应用程序试图识别抽奖日期,并在彩票上播放号码。但是,由于票证背景上的图像,我无法检测日期和号码。如何修改我的代码以实现我的目标?

最初,我试图找到一个API,该API将接受彩票的条形码,如果彩票是中奖票,则返回。在对网络进行了广泛的研究之后,我意识到这样的方法是不可能的,所以现在我尝试使用字符识别方法来检测数字和绘制日期。与此信息,我将交叉参照它与获奖的数字和抽签日期。这里的好处是,想要的字符都是黑色的,其他的都有不同的颜色。我试图使用this逻辑,但我很难操纵代码来实现我的目的。

所需的代码将输出“第一张抽签:”日期和播放的6个数字(在A06:的右边)。

我真正得到的是:

“no”lo0“Wm”{3153:-.:,.4,LDTTU PLUS,;7N9"??ms: 10 20 24 25 32 3.7总计:R5‘00.7’hc?‘e: IWHW 753:“15/0/19 FE:4;1-071094555258an94

代码语言:javascript
复制
//function I use to run OCR
function runOCR(url) {
  Tesseract.recognize(url)
    .then(function(result) {
      console.log(result.text);
    }).progress(function(result) {
      console.log('Status: ' + result['status']);
    });
}

提前感谢您的有效解决方案。我只是需要有人帮助我在像素出红和白的背景,以便前景变得容易识别。我对这里的两行感兴趣:抽签日期,上面写着第一次抽签: Saterday 20/07/19A06: 10 20 24 25 32 37

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-20 08:17:03

哇哦..。我试过了。

我首先将图像转换为灰度图像,然后检查该值是否高于或低于阈值。只需上传图像并移动滑块以更改阈值即可。

(您可能需要在整页lol中打开它)

有一个好的:)

代码语言:javascript
复制
const fileReader      = document.getElementById('fileReader');
const sliderThreshold = document.getElementById('sliderThreshold');
const inputCanvas     = document.getElementById('inputCanvas');
const outputCanvas    = document.getElementById('outputCanvas');
const inputCtx        = inputCanvas.getContext('2d');
const outputCtx       = outputCanvas.getContext('2d');


sliderThreshold.addEventListener('input', e => displayResult(e.target.value));


fileReader.addEventListener('change', inputEvent => {
  let reader = new FileReader();
  reader.addEventListener('load', readerEvent => {
    let img = new Image();
    img.addEventListener('load', () => {
      inputCanvas.width  = img.width;
      inputCanvas.height = img.height;
      inputCtx.drawImage(img, 0, 0);

      displayResult(50);
    });
    img.src = readerEvent.target.result;
  });
  reader.readAsDataURL(inputEvent.target.files[0]);
});



function displayResult(threshold) {
  let imageData = inputCtx.getImageData(0,0, inputCanvas.width, inputCanvas.height);
  let data = imageData.data;

  for(let i = 0; i < data.length; i += 4) {
    // Convert RGB values to grayscale (you can look that up)
    let grayscale = data[i] * 0.3 + data[i + 1] * 0.59 + data[i + 2] * 0.11;

    // Check if the value is obove or below the threshold value and return white or black
    let finalColor = grayscale < threshold ? 0 : 255;

    // Asign the color
    data[i]     = finalColor;
    data[i + 1] = finalColor;
    data[i + 2] = finalColor;
  }

  // Put the data into another canvas so we 
  outputCanvas.width = imageData.width;
  outputCanvas.height = imageData.height;
  outputCtx.putImageData(imageData, 0, 0);
}
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .canvasContainer {
      overflow-y: scroll;
      display: inline-block;
    }
  </style>
</head>
<body>
  <input type="file" id="fileReader">
  Threshold<input type="range" min="0" max="255" id="sliderThreshold">


  <div class="canvasContainer">
    <canvas id="outputCanvas"></canvas>
  </div>
  <div class="canvasContainer">
    <canvas id="inputCanvas"></canvas>
  </div>



  <script src="./index.js"></script>
</body>
</html>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57099268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档