{"id":19545164,"url":"https://github.com/scscms/ocr","last_synced_at":"2026-06-07T21:02:01.189Z","repository":{"id":93555063,"uuid":"131367809","full_name":"scscms/OCR","owner":"scscms","description":"利用tesseract-ocr,GraphicsMagick进行图片文本识别","archived":false,"fork":false,"pushed_at":"2018-05-02T05:53:04.000Z","size":361,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-08T19:23:56.395Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scscms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-28T03:13:54.000Z","updated_at":"2018-05-02T05:53:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"e7b40d44-3b4a-4b8e-b69c-175460a6a15a","html_url":"https://github.com/scscms/OCR","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2FOCR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2FOCR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2FOCR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2FOCR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scscms","download_url":"https://codeload.github.com/scscms/OCR/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240801040,"owners_count":19859727,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T03:35:52.680Z","updated_at":"2025-02-26T05:42:55.085Z","avatar_url":"https://github.com/scscms.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tsseract文字识别\u003csup\u003eshine\u003c/sup\u003e\n\n前言\n\n\u003eTesseract的OCR引擎最先由HP实验室于1985年开始研发，至1995年时已经成为OCR业内最准确的三款识别引擎之一。然而，HP不久便决定放弃OCR业务，Tesseract也从此尘封。 数年以后，HP意识到，与其将Tesseract束之高阁，不如贡献给开源软件业，让其重焕新生–2005年，Tesseract由美国内华达州信息技术研究所获得，并求诸于Google对 Tesseract进行改进、消除Bug、优化工作。\n\n### Windows环境安装tesseract-ocr 4.00并配置环境变量\n\n- 下载地址：[tesseract-ocr-setup-4.00.00dev安装文件](http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe \"点击下载\")\n\n- 安装：直接运行`tesseract-ocr-setup-4.00.00dev.exe`文件，下一步、同意协议下一步、所有用户使用下一步、安装。\n    \n- 选择语言：默认只有英文字体。选择所有字体安装有1G多，也没必要。所以我们只新增`简体`和`繁体`中文。\n\n![image](img/1.png)\n![image](img/2.png)\n\n安装过程中会自动下载字体，可能会有点慢（视网络情况）。所以特别注意不要安装一些无需的字体！\n\n- 配置环境变量：因为我们需要命令式执行文件，所以安装好后需要配置环境变量。在我的电脑上右键选择“属性--\u003e高级属性设置--\u003e环境变量”，给系统变量Path追加`;C:\\Program Files (x86)\\Tesseract-OCR`(根据你的安装目录相应变化)。同时在系统变量下面新建一个变量`TESSDATA_PREFIX`，其值一样为`C:\\Program Files (x86)\\Tesseract-OCR`配置好了点击保存，并重启电脑。\n\n\t\u003e`TESSDATA_PREFIX`变量是解决跨盘读取字库问题。如果没有新建将不能在其他盘调用字库。\n\n- 测试安装：打开CMD命令终端，输入：`tesseract -v` 可以看到版本信息说明安装成功。\n![image](img/3.png)\n\n- 识别图片：识别图片命令`tesseract 图片名称 生成结果的文件名称 -l 字体库`，如：\n```\ntesseract img/txt.png result -l chi_sim --psm 6\n```\n不出意外的话就会得到一个含有结果的`result.txt`文件。\n\n### 文字识别应用:破解验证码\n\n废话少说，让程序自动识别验证码可用于自动登录。在此我准备了一张真实网站上的验证码\n\n![image](img/code.jpg)\n\n此验证码可以变色，所以我截图了好几个。让我们尝试一下：\n```\ntesseract img/code.jpg result -l chi_sim --psm 6\n```\n破解为`少 85237581 40`可见这结果并不理想。\n\n\t在此我们故意仍使用中文字体破解，如果使用英文字体破解效果会稍好。\n\n其实也不难理解，因为彩色图片识别度不高。如果我们对图片先`去色`，并调整`阈值`为130再去识别效果就会好很多。\n\n![image](img/4.jpg)\n\n\u003e分别为：原图、去色、阈值130的效果。\n\n\n测试代码[test.js](test.js)\n\n当然，你肯定会说：“那总不能让程序自己搞PS软件去处理阈值呀！”。是的，所以我们要再安装一个自动处理图片的工具`GraphicsMagick`。\n\n#### GraphicsMagick安装\n\n\u003eGraphicsMagick号称图像处理领域的瑞士军刀。 短小精悍的代码却提供了一个鲁棒、高效的工具和库集合，来处理图像的读取、写入和操作，支持超过88种图像格式，包括重要的DPX、GIF、JPEG、JPEG-2000、PNG、PDF、PNM和TIFF。\n\n[GraphicsMagick](http://www.graphicsmagick.org/download.html \"下载页面\")里面含有各种系统的下载文件，而我使用的是[window64位版本](ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/windows/ \"windows各版本下载FTP\")其文件只有4.26M。安装过程很简单，直接下一步、下一步即可。安装完后同样设置一下系统变量为新增`;C:\\Program Files\\GraphicsMagick-1.3.28-Q8`(根据你的安装目录相应变化)，并重启电脑。\n\n- 测试安装\n\n在CMD命令里输入`gm`回车，如果能看到以下信息表示安装成功：\n\n```\nGraphicsMagick 1.3.28 2018-01-20 Q8 http://www.GraphicsMagick.org/\nCopyright (C) 2002-2018 GraphicsMagick Group.\nAdditional copyrights and licenses apply to this software.\nSee http://www.GraphicsMagick.org/www/Copyright.html for details.\nUsage: gm command [options ...]\n\nWhere commands include:\n      batch - issue multiple commands in interactive or batch mode\n  benchmark - benchmark one of the other commands\n    compare - compare two images\n  composite - composite images together\n    conjure - execute a Magick Scripting Language (MSL) XML script\n    convert - convert an image or sequence of images\n       help - obtain usage message for named command\n   identify - describe an image or image sequence\n    mogrify - transform an image or sequence of images\n    montage - create a composite image (in a grid) from separate images\n       time - time one of the other commands\n    version - obtain release version\n   register - register this application as the source of messages\n```\n\n扩展：更多[gm](https://github.com/aheckmann/gm \"github gm官网\")介绍看官方文档。\n\n- 编写执行脚本\n\n参见：[index.js](index.js)\n\n- 查看效果\n\n```\nnode index\n```\n\n查看控制台是否正常把验证码打印出来？\n\n### 验证码安全对策\n\n- 增加验证码复杂度，比如镂空字体，复杂的干扰线条等；\n- 语音验证码：随机生成的验证码使用语音读出。缺点：应用阻力比较大，不是所有电脑都有声音，和兼容性问题大。\n```\nlet speech = new SpeechSynthesisUtterance();\nspeech.text = '验证码：23abc';\nspeech.lang = 'zh-CN';\nspeechSynthesis.speak(speech);\n```\n- 滑块验证：目前比较流行的拖动向右滑动滑块填充拼图。缺点：就是过分依赖第三方网站，同时也不是完全安全可靠。\n\n- 手机验证码：把验证码发到手机里。缺点：成本比较高，容易被刷爆。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscscms%2Focr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscscms%2Focr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscscms%2Focr/lists"}