知行编程网知行编程网  2022-04-19 03:00 知行编程网 隐藏边栏 |   抢沙发  24 
文章评分 0 次,平均分 0.0

10几行代码,用python打造实时截图识别OCR


你一定用过那种“OCR神器”,可以把图片中的文字提取出来,极大的提高工作效率。


今天,我们就来做一款实时截图识别的小工具。顾名思义,运行程序时,可以实时的把你截出来的图片中的文字识别出来。


下次,当你想要复制“百度文库”中的内容时,不妨试试这个程序。


效果预览


10几行代码,用python打造实时截图识别OCR


10几行代码,用python打造实时截图识别OCR

源码解析

1)等待用户截图

此处需要借助贴图神器(Snipaste)
其中“f1”是截图的快捷键,“ctrl+c”是把截图保存到剪贴板的快捷键。

如果使用qq截图的话,需要把快捷键改为对应的“ctrl+alt+c”和“enter”

顺便安利一波Snipaste,

必备效率神器


<section style="line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;margin-left: 16px;margin-right: 16px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> keyboard<br  /><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 利用截图软件(Snipaste)截图到剪贴板</span><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 输入键盘的触发事件</span><br  />keyboard.wait(hotkey=<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"f1"</span>)  <br  />keyboard.wait(hotkey=<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"ctrl+c"</span>)<br  />time.sleep(<span style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">0.1</span>)<br  /></section>

上面这段代码执行之后,现在已经有一张图片等待在剪贴板里了。

2)保存截图

利用PIL模块的ImageGrab,可以把剪贴板里的那张图片,保存到当前的目录下,并命名为“screen.png”

<section style="line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;margin-left: 16px;margin-right: 16px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">from</span> PIL <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> ImageGrab<br  /><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 把图片从剪切板保存到当前路径</span><br  />image = ImageGrab.grabclipboard()  <br  />image.save(<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"screen.png"</span>)<br  /></section>

3)识别截图中的文本

法一
pytesseract模块

优点:免费,易用
缺点:识别效果很一般,准确率不高

使用方法介绍:

1)pip install pytesseract

2)安装 tesseract-ocr.exe 配置环境变量

3)修改pytesseract.py文件,将tesseract_cmd指向Tesseract-OCR的tesseract.exe的绝对路径。

参考文章

见评论第一条


<section style="line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;margin-left: 16px;margin-right: 16px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pytesseract<br  /><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">from</span> PIL <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> Image<br  /><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 法一:利用pytesseract模块</span><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 参数一:图片 </span><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 参数二:简体中文</span><br  />text = pytesseract.image_to_string(Image.open(<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"screen.png"</span>), lang=<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">'chi_sim'</span>)<br  />print(text)<br  /></section>

来看看效果:

10几行代码,用python打造实时截图识别OCR

low的不行
果然,要想精度高,还得用百度API

法二
百度API接口

AI开放平台文档中心
https://ai.baidu.com/ai-doc

10几行代码,用python打造实时截图识别OCR

查看python语言的SDK文档

10几行代码,用python打造实时截图识别OCR
 
点击右上角(控制台),登录自己的百度账号,创建“文字识别”的应用

10几行代码,用python打造实时截图识别OCR

<section style="line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;text-align: justify;margin-left: 16px;margin-right: 16px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pytesseract<br  /><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">from</span> aip <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> AipOcr<br  /><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">from</span> PIL <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> ImageGrab<br  /><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 法二:利用百度API</span><br  />APP_ID = <span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">'你的 App ID'</span><br  />API_KEY = <span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">'你的 Api Key'</span><br  />SECRET_KEY = <span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">'你的 Secret Key'</span><br  /><br  />client = AipOcr(APP_ID, API_KEY, SECRET_KEY)<br  /><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 读取图片</span><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">with</span> open(<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"screen.png"</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">'rb'</span>) <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">as</span> f:<br  />    image = f.read()<br  /><br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># 调用百度API通用文字识别(高精度版),提取图片中的内容</span><br  />    text = client.basicAccurate(image)<br  />    result = text[<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"words_result"</span>]<br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i <span style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">in</span> result:<br  />        print(i[<span style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"words"</span>])<br  /></section>

结果
如文章首图:

10几行代码,用python打造实时截图识别OCR

我是总结


1)等待用户截图
2保存截图到当前目录
3)识别截图中的文本

其中识别截图文本,有两种方法:

1)利用 pytesseract 模块

2)利用百度API接口


回复“OCR”
可获取完整代码的Github地址


<section style="margin-right: 16px;margin-left: 16px;white-space: normal;letter-spacing: 0.5px;line-height: 1.5em;text-align: center;"><br  /></section><section style="margin-right: 8px;margin-left: 8px;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 41, 65);font-size: 15px;"><strong>近期热门:</strong></span></section><section style="margin-right: 8px;margin-left: 8px;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);">高手心得|菜鸟学Python从入门到进阶</section><section style="margin-right: 8px;margin-left: 8px;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.75em;">干货来了!菜鸟入门最经典的机器学习项目,面试必考!<br  /></section><section style="margin-right: 8px;margin-left: 8px;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.75em;">400多人做过的8道Python极速入门题<br  /></section><section style="margin-right: 8px;margin-left: 8px;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 1.75em;"><br  /></section><p style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;"><img class="rich_pages" data-ratio="0.125" data-s="300,640" data-type="png" data-w="800"  style="visibility: visible !important;width: 677px !important;" src="https://www.zkxjob.com/wp-content/uploads/2022/04/wxsync-2022-04-99ac2eb9e0e48efb4acefe9398a4b418.png"  /></p><p style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;"><br  /></p><p style="font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: left;"><span style="letter-spacing: 0.544px;text-size-adjust: auto;font-size: 16px;"> </span><span style="letter-spacing: 0.544px;text-size-adjust: auto;color: rgb(217, 33, 66);font-size: 15px;"><strong mpa-from-tpl="t">点击阅读原文,阅读菜鸟学Python 400篇干货!</strong></span></p>

本篇文章来源于: 菜鸟学Python

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享