当前位置: 首页 > java >正文

JS-OCR-demo加载本地文件

背景:

在了解 Tesseract 的识别效果的时候,有个demo项目很好用。有个小毛病,就是没事都要从摄像头抓取图片,然后进行识别。如果可以从本地读取图,就更方便了。

实现:

  1. 下载项目代码:https://github.com/kdzwinel/JS-OCR-demo

  2. 在页面index.html增加选择文件的input组件

    51|<input type="file" id="uploadImage" accept="image/*" class="btn btn-lg btn-default" style="justify-self: center;margin-top: 10px;">
    
  3. 在main.js修改方法:

  • 增加加载本地图片的逻辑。

       // 新增:本地图片上传$('#uploadImage').on('change', function (e) {var file = e.target.files[0];if (!file) return;var reader = new FileReader();reader.onload = function (evt) {var img = document.querySelector('#step2 img');img.onload = function () {// 将图片绘制到canvasvar canvas = document.querySelector('#step2 canvas');canvas.width = pictureWidth;canvas.height = pictureHeight;var ctx = canvas.getContext('2d');ctx.drawImage(img, 0, 0, canvas.width, canvas.height);texture = fxCanvas.texture(canvas);fxCanvas.draw(texture).hueSaturation(-1, -1)//grayscale.unsharpMask(20, 2).brightnessContrast(0.2, 0.9).update();$('.jcrop-holder img').attr('src', fxCanvas.toDataURL());$(img).attr('src', fxCanvas.toDataURL());};img.src = evt.target.result;};reader.readAsDataURL(file);step2(false);changeStep(2);});
    
  • 修改step2方法:增加参数判断,区分使用的相机截图,还是本地文件;截图时,使用图片真实像素为基准。

    function step2(usevideo=true) {var canvas = document.querySelector('#step2 canvas');var img = document.querySelector('#step2 img');//setup canvascanvas.width = pictureWidth;canvas.height = pictureHeight;var ctx = canvas.getContext('2d');if(usevideo){//draw picture from video on canvasctx.drawImage(video, 0, 0);console.log('draw picture from video on canvas');}//modify the picture using glfx.js filterstexture = fxCanvas.texture(canvas);fxCanvas.draw(texture).hueSaturation(-1, -1)//grayscale.unsharpMask(20, 2).brightnessContrast(0.2, 0.9).update();window.texture = texture;window.fxCanvas = fxCanvas;$(img)//setup the crop utility.one('load', function () {if (!$(img).data().Jcrop) {$(img).Jcrop({onSelect: function () {//Enable the 'done' button$('#adjust').removeAttr('disabled');},trueSize: [img.naturalWidth || img.width, img.naturalHeight || img.height]});} else {//update crop tool (it creates copies of <img> that we have to update manually)$('.jcrop-holder img').attr('src', fxCanvas.toDataURL());}})//show output from glfx.js.attr('src', fxCanvas.toDataURL());
    }
    
  • 修改step3方法:截图区域不再使用缩放系数计算;修改识别语言为中文chi_sim

    function step3() {var canvas = document.querySelector('#step3 canvas');var step2Image = document.querySelector('#step2 img');var cropData = $(step2Image).data().Jcrop.tellSelect();// 直接使用裁剪区域的宽高canvas.width = cropData.w;canvas.height = cropData.h;var ctx = canvas.getContext('2d');ctx.drawImage(step2Image,cropData.x, cropData.y, cropData.w, cropData.h, // 源区域0, 0, cropData.w, cropData.h                    // 目标区域);var spinner = $('.spinner');spinner.show();$('blockquote p').text('');$('blockquote footer').text('');// do the OCR!// 设置参数var options = 'chi_sim+eng';Tesseract.recognize(ctx, options).then(function (result) {var resultText = result.text ? result.text.trim() : '';//show the resultspinner.hide();$('blockquote p').html('&bdquo;' + resultText + '&ldquo;');$('blockquote footer').text('(' + resultText.length + ' characters)');});}
    

在这里插入图片描述

http://www.xdnf.cn/news/2361.html

相关文章:

  • 直播预告|TinyVue 组件库高级用法:定制你的企业级UI体系
  • 仿微信上传头像,实现拍摄、相册选择、手动缩放、裁剪、蒙版、撤回、还原、上传微信本地文件功能
  • 【C++】类和对象(上)
  • 【质量管理】TRIZ(萃智)的工程系统进化法则
  • GAEA情感坐标的技术架构与系统集成
  • 机器学习基础理论 - 分类问题评估指标
  • 【AI模型学习】GPT——从v1到v3
  • JavaScript输出数据的方法
  • 高并发架构设计之缓存
  • 快速上手 MetaGPT
  • 【Flutter】Flutter + Unity 插件结构与通信接口封装
  • 继续 那个错误分析
  • 机器学习-入门-线性模型(1)
  • 量子纠缠式架构:当微服务同时存在于所有节点时,CAP定理是否依然成立?
  • R中实现数值求导的包numDeriv
  • deepSeek浅谈对vue的mixin的理解,用于什么应用场景?
  • 探索DeepWiki:GitHub源码阅读的变革性工具
  • 从零构建云原生秒杀系统——后端架构与实战
  • Win10安装 P104-100 驱动
  • 衡量矩阵数值稳定性的关键指标:矩阵的条件数
  • Golang | 搜索表达式
  • 【c++】AVL树模拟实现
  • 从基础到实战的量化交易全流程学习:1.3 数学与统计学基础——概率与统计基础 | 基础概念
  • 深入详解人工智能数学基础—概率论-KL散度在变分自编码器(VAE)中的应用
  • 驯龙日记:用Pandas驾驭数据的野性
  • 《2025全球机器学习技术大会:阿里云讲师张玉明深度剖析通义灵码AI程序员》
  • prometheus手动添加k8s集群外的node-exporter监控
  • 国家与省市县 标准地图服务网站 审图号地图下载
  • 【Redis】zset类型
  • 本安型交换机 + TSN:煤矿智能化的关键拼图