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

如何构建识别图像中字符的自动程序?一文解读OCR与HTR

选自 | Medium    作者 | Ajinkya Khalwadekar

转自 | 机器之心


前言


在机器学习和计算机视觉领域,光学字符识别(OCR)和手写文本识别(HTR)长期以来都是人们研究的重要主题。本文将帮助计算机视觉爱好者大致了解如何对文档图像中的文本进行识别。


光学字符识别和手写文本识别是人工智能领域里非常经典的问题。OCR 很简单,就是将文档照片或场景照片转换为机器编码的文本;而 HTR 就是对手写文本进行同样的操作。作者在文章中将这个问题分解成了一组更小型的问题,并制作了如下的流程图。


如何构建识别图像中字符的自动程序?一文解读OCR与HTR

图 1.1:应用流程图

 

   按文档边框裁剪图像

 

在图像处理中,通常需要对图像进行预先编辑,以便获得更好的表征。裁剪是图像编辑中最常用的操作之一,这可以移除图像中不需要的部分,也可以向图像添加所需的特征。


你可以使用 OpenCV 来轻松地找到图像中文档的边缘,查找图像中文档边缘的最佳方法是使用阈值图像。OpenCV 提供了不同的阈值样式,这是由其函数的第 4 个参数决定的。在这个函数中,第一个参数是源图像,这应该是一张灰度图像;第二个参数是用于分类像素值的阈值;第三个参数是 maxVal,这是当像素值超过(有时是低于)阈值时所要给出的值。


下面的代码将能帮助你找到阈值图像,然后确定文档边缘的轮廓,你可以将这些轮廓点与图像边缘进行比较,然后确定文档的边缘。


<p style="box-sizing: border-box;padding: 0.5em;font-size: 14px;color: rgb(169, 183, 198);line-height: 18px;border-radius: 0px;background: rgb(40, 43, 46);display: block;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;letter-spacing: 0px;margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;"><span style="box-sizing: border-box;font-size: inherit;color: rgb(128, 128, 128);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"># threshold image</span><br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"  />ret, thresh = cv2.threshold(imgray, 150, 255, 0)<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"  />cv2.imwrite(<span style="box-sizing: border-box;font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">'thresh.jpg'</span>, thresh)<br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"  /><span style="box-sizing: border-box;font-size: inherit;color: rgb(128, 128, 128);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"># edge contours</span><br style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"  />contours, hierarchy = cv2.findContours(thresh, 1, 2)</p>


   检测和裁剪/分割文档中的所有词

 

在有约束的受控环境中进行词检测通常可以使用启发式方法实现,比如利用梯度信息或者这样的事实:文本通常会被分组成段落以及排列成直线的字符。但是,使用启发式方法是存在缺陷的,图像中很多不需要的区域也会被检测为词,所以我们可以使用 OpenCV 的 EAST(Efficient and Accurate Scene Text)检测器。


可以参考 Adrian Rosebrock 写的 EAST 检测器相关文章:https://www.pyimagesearch.com/2018/08/20/opencv-text-detection-east-text-detector/ 


然后再根据 Tom Hoag 分享的方法对其进行改进:https://medium.com/@tomhoag/opencv-text-detection-548950e3494c


这种方法能以很高的准确度检测出手写文本以及机器打印的文本。检测出图像中的词之后,再将它们裁剪出来并将它们全部保存下来。

 

   预处理词图像

 

应该怎么样对图像进行预处理?这完全取决于你接下来要做什么。如果想要分类手写的和机器打印的词,需要所有图像都处于灰度模式。为了将图像转换为灰度图像,还需要使用 OpenCV:


<p style="box-sizing: border-box;padding: 0.5em;font-size: 14px;color: rgb(169, 183, 198);line-height: 18px;border-radius: 0px;background: rgb(40, 43, 46);display: block;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;letter-spacing: 0px;margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;"><span style="box-sizing: border-box;font-size: inherit;color: rgb(165, 218, 45);line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">imgray</span> = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)</p>


   这是手写词吗?

 

这是一个分类问题:确定一张特定图像中的词是「手写词」还是「机打词」。作者浏览了多篇文章和研究论文,发现支持向量机(SVM)是解决这一问题的最佳方案,然后使用了来自 sklearn 软件包的 SVM 分类器来完成这一任务。


对于用于分类的数据集,作者提到了一个很好的手写词图像有标注数据集 IAM:http://www.fki.inf.unibe.ch/databases/iam-handwriting-database


对于机器打印的词图像,作者收集了大约 2000 张词图像。下面是用于预测的特征:

 

1. 平均像素强度
2. 像素强度的标准差
3. Otsu 阈值
4. 像素强度直方图中局部最大值的数量
5. 属于像素强度上面的四分之一的像素的百分比
6. 属于像素强度下面的四分之一的像素的百分比
 
按照上面来看,所有特征都与图像的像素强度有关联。下一个问题是:如何找到像素强度?

灰度图像的像素值就是像素的强度,同样也可以使用 OpenCV 和数学运算来完成这一任务。
 
   使用 TensorFlow 的 HTR
 
这是本文所有问题中最具有挑战性的问题。在尝试了不同的解决方案之后(包括在手写字符数据集上重新训练 Tesseract),结果显示 Harald Scheidl 这篇文章的方法最佳:https://towardsdatascience.com/build-a-handwritten-text-recognition-system-using-tensorflow-2326a3487cd5

作者使用了类似的方法,不过做了一些小修改,在这里使用了神经网络,由 5 个卷积神经网络(CNN)层、2 个循环神经网络(RNN)层和 1 个连接主义时间分类(CTC)层构成。用于训练这个神经网络的数据集是 IAM 数据集,但你也可以使用任何有标注的词图像数据集。

如何构建识别图像中字符的自动程序?一文解读OCR与HTR
图 1.2:来自 Herald Scheidl 文章的示意图
 
CNN 层的输入是大小为 128×32 的灰度值图像。CNN 层的输出是一个序列,其包含 32 项,其中每一项都有 256 个特征。这些特征再进一步由 RNN 层处理,但是,某些特征已经表现出了与输入图像的特定高层面性质的高度相关性。

如何构建识别图像中字符的自动程序?一文解读OCR与HTR
图 1.3:来自 Herald Scheidl 的文章的示意图
 
图 1.3 展示了处理一张包含文本「little」的图像时,可视化的 RNN 输出矩阵。最上面的图表中的矩阵包含了字符的分数,这些字符中的最后一项(第 80 个)是一个 CTC 空白标签。其它矩阵项,从上到下分别对应于如下字符:!」#&』()*+,-./0123456789:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
 
可以看到,大多数时间里,被预测的字符都刚好出现在它们在图像中的位置处(比如,你可以比较看看图像与图表中 i 的位置。只有最后一个字符 e 没有对齐。但这其实没有问题,因为 CTC 操作是无分割的,而且不在乎绝对位置。最下面的图表展示了字符 l、i、t、e 和 CTC 空白标签的分数,该文本可以轻松地被解码:我们只需要从每个时间步骤取出最可能的字符即可,这会构成所谓的最佳路径,然后我们丢弃重复的字符,最后丢弃所有空白,得到:「l—-ii—t-t—l-…-e」→「l—-i—t-t—l-…-e」→「little」。
 
更多有关如何实现这一方法的细节信息,请参看 Herald Scheidl 的文章。

   Tesseract(OCR)
 
Tesseract 是目前最好的用于机器打印字符识别的开源 OCR 工具。Tesseract 支持 Unicode(UTF-8)字符集,可以识别超过 100 种语言,还包含多种输出支持,比如纯文本、PDF、TSV 等。但是为了得到更好的 OCR 结果,还必须提升提供给 Tesseract 的图像的质量。

注意,在执行实际的 OCR 之前,Tesseract 会在内部执行多种不同的图像处理操作(使用 Leptonica 库)。通常它表现不错,但在某些情况下的效果却不够好,导致准确度显著下降。
 
在将图像传递给 Tesseract 之前,可以尝试以下图像处理技术,但具体使用哪些技术取决于你想要读取的图像:
 
1. 反转图像
2. 重新缩放
3. 二值化
4. 移除噪声
5. 旋转/调整倾斜角度
6. 移除边缘
 
所有这些操作都可以使用 OpenCV 或通过 Python 使用 numpy 实现。
 
简单总结一下,本文介绍了与 OCR 和 HTR 相关的一些问题和可能的解决方案。如果你想要真正理解,一定要亲自动手实现它们看看。

原文链接:
https://medium.com/@ajinkya.khalwadekar/building-ocr-and-handwriting-recognition-for-document-images-f7630ee95d46

<pre style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-right: 8px;margin-left: 8px;max-width: 100%;white-space: normal;color: rgb(0, 0, 0);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;text-align: center;widows: 1;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></section><section style="max-width: 100%;white-space: normal;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;text-align: center;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-right: 8px;margin-bottom: 15px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></section><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">“12306”的架构到底有多牛逼?</span></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">中国程序员34岁生日当天在美国遭抢笔记本电脑,追击歹徒被拖行后身亡,为什么会发生此类事件?</span></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">阿里如何抗住90秒100亿?看这篇你就明白了!</span></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(87, 107, 149);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">60个Chrome神器插件大收集:助你快速成为老司机,一键分析网站技术栈</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">深度学习必懂的13种概率分布</span></p></section></section></section></section></section></section></section></section>

本篇文章来源于: 深度学习这件小事

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

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

发表评论

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