Python文字识别OCR案例:百度AI实现OCR识别(源代码) !

最近研究OCR技术实现较多,各种OCR实现方式做了一些测试和对比,针对不同的业务需要采用不同的实现方式,今天写的这个案例是利用百度AI来实现的通用OCR文字识别。实现方式非常简单,十几行代码就能搞定,但识别率如何就要看图片质量和所用字体了。

从根本上来说,OCR的技术实现有两种方式:

第一种较为简单,就是调用各个云平台提供的OCR服务来识别图片上的文字。这种实现方式的优点是简单易用,识别率较高,灵活性较弱,定制化不容易实现,但是需要上传图片信息,如果图片上包含敏感信息就有数据安全性和隐私性方面可能的问题,而正是这最后一点让很多客户对这种OCR实现方式敬谢不敏。

第二种实现起来较为复杂,就是在本地实现OCR文字识别,需要在本机安装OCR识别软件和类库——这些软件和类库可以是第三方提供的,例如Google Tesseract OCR软件和类库,也可以是自开发的机器学习基础上的OCR文字识别程序。这种实现方式的优点是数据安全性和隐私性较强,不需要上传图片信息,OCR识别完全在本地完成,效率较高,而且可定制化高(图片处理,指定字体等),灵活性较强,但是缺点就是需要在本地安装和配置OCR识别的软件或程序。识别率要看本地安装的OCR识别软件和程序的算法实现,在性能上依赖于本地硬件配置。从数据安全性和隐私性角度,客户更多倾向于这一种实现方式。

第二种OCR实现方式的案例已经写了,就是利用Google Tesseract OCR本地软件和类库来实现的OCR文字识别,具体可参考我写的一个案例(Python文字识别OCR案例:搞定PDF扫描件和图片文字内容(源代码))。

今天这个OCR文字识别的案例是采用第一种实现方式,调用百度AI的OCR服务来实现图片上的文字识别。在百度AI平台上注册一个账号,然后就可以调用OCR服务了,每天5000次内调用免费。

从百度图片下载了一个新闻图片:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557560226444″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《Python文字识别OCR案例:百度AI实现OCR识别(源代码) !》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

Python学习交流群:1004391443,这里是python学习者聚集地,有大牛答疑,有资源共享!小编也准备了一份python学习资料,有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。

运行写的Python程序调用百度AI的OCR服务来识别这个新闻图片:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557560226455 ql-align-center” data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《Python文字识别OCR案例:百度AI实现OCR识别(源代码) !》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

百度AI的OCR服务返回内容写入了一个文本文件:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557560226464 ql-align-center” data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《Python文字识别OCR案例:百度AI实现OCR识别(源代码) !》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

这个案例的Python程序的源代码已经上传到github,需要的可以直接点击下载(下载前请点个赞,加个星star):

<bi style=”box-sizing: border-box; display: block;”>https://github.com/hanshiqiang365/baidu_demo</bi>

百度AI平台提供的各种人工智能服务还是非常好用的,从学习和研究人工智能的实现效果,或者用来实际处理不是敏感的信息的应用角度而言,实现起来非常简单,测试效果也非常不错。最重要的,很多服务是有免费使用次数的。

故有此文,与君分享。

    原文作者:不谈风月_0eb8
    原文地址: https://www.jianshu.com/p/ae85c44d5053
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞