使用Python,Tesseract更正文本方向

news/2024/9/21 20:46:13

这篇博客将介绍如何使用 Tesseract 的方向和脚本检测 (OSD) 模式执行自动文本方向检测和更正。

orientation and script detection (OSD) 方向和脚本检测
text orientation 文本定向

OSD模式检测并提供了图像中文本的方向,估计的旋转校正角度和脚本/书写系统。文本方向是指图像中文本的角度(以度为单位)。执行 OCR 时可以通过校正文本方向来获得更高的准确性。另一方面,脚本检测是指文本的书写系统,可以是拉丁语,汉字,阿拉伯语,希伯来语等。

任何 OCR 系统的一个基本组件都是图像预处理 ——呈现给OCR引擎的输入图像质量越高,OCR 输出就越好。要在 OCR 中取得成功,最重要的预处理步骤是文本方向。

1. 效果图

原图 VS 旋转校正后图如下:
可以看到文本正确识别,并且当前文本旋转了90°,需要旋转270°进行校正,文本的语言类型是Latin文。下图也可以看到文本方向被正常校正。
在这里插入图片描述

在这里插入图片描述

效果图2如下:
可以看到文本正确识别,当前文本旋转了180°,需要旋转180°进行校正,文本的语言类型是Latin文。下图也可以看到文本方向被正常校正。
在这里插入图片描述

效果图3如下:对于旋转非90°或者90°倍数的识别有问题,如下图所示;

在这里插入图片描述

2. 原理

文本方向是指图像中一段文本的旋转角度。如果文本明显旋转,则给定的单词、句子或段落在 OCR 引擎中看起来就像胡言乱语。OCR引擎是智能的,但像人类一样,它们没有被训练成倒置阅读!

因此为 OCR 准备图像数据的关键第一步是检测文本方向(如果有),然后更正文本方向。 将校正后的图像呈现到 OCR 引擎(理想情况下可以获得更高的 OCR 准确性)。

2.1 步骤

  1. 方向和脚本检测 (OSD) 的概念
  2. 如何使用 Tesseract 检测文本脚本(即书写系统)
  3. 如何使用 Tesseract 检测文本方向
  4. 如何使用 OpenCV 自动更正文本方向
pip install opencv-contrib-python

2.2 什么是方向和脚本检测?

Tesseract 有几种不同的模式,可以在自动检测和 OCR 文本时使用。其中一些模式对输入图像执行完整的OCR,而其他模式则输出元数据,例如文本信息,方向等(即orientation and script detection OSD模式)。
Tesseract的OSD模式的输出返回两个值:

  • 文本方向:输入图像中文本的估计旋转角度(以度为单位)。
  • 脚本:图像中文本的预测“书写系统”。

书写系统是一种传达信息的视觉方法,但与语音不同,书写系统还包括“存储”和“知识转移”的概念。

3. 源码

# 使用 Tesseract 检测和更正文本方向(检测到文本角度,旋转以转回去的角度,文本系统语言)
# 识别字母
# tesseract E:\mat\py-demo-22\extreme_points\images\normal.jpg E:\mat\py-demo-22\extreme_points\images\normal txt
# 识别osd
# tesseract E:\mat\py-demo-22\extreme_points\images\normal.jpg E:\mat\py-demo-22\extreme_points\images\normal_1 -l osd --psm 0
# USAGE
# python detect_orientation.py --image images/normal.jpg
# python detect_orientation.py --image images/rotated60.jpg
# python detect_orientation.py --image images/rotated180.jpg
# python detect_orientation.py --image images/rotated90.jpg
# python detect_orientation.py --image images/rotated120.jpg# 导入必要的包
# coding = utf -8
import argparse
import osimport cv2
import imutils
import pytesseract
from PIL import Image
import numpy as np
from pytesseract import Output  # PyTesseract 的输出类 (https://github.com/madmaze/pytesseract)。这个类只是指定了四种数据类型,包括将利用的DICT。# 语言文件.traindata
os.environ['TESSDATA_PREFIX'] = 'D:\\tesseract\\tessdata'  # 在PATH中配置tesseract环境变量或者此处指定# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\\tesseract\\tesseract.exe'  # 手动设置另一个版本的pytesseract# 构建命令行参数及解析
# --image 要ocr的图像
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=False, default="images/normal.jpg",help="path to input image to be OCR'd")
args = vars(ap.parse_args())# 加载输入图像,转换BGR为RGB通道使得其与tesseract兼容
# 调用Tesseract执行文本定向
image = cv2.imread(args["image"])
if image is None:raise Exception(args["image"] + (" image not exists"))
img = image.copy()# 打印pytesseract支持的所有语言
print('langs: ', pytesseract.get_languages(config=''))print(pytesseract.image_to_string(img))
# OR explicit beforehand converting
print(pytesseract.image_to_string(Image.fromarray(img)))# 简单的图像串
print(pytesseract.image_to_string(Image.open(args["image"])))
print(pytesseract.image_to_string(args["image"]))
# 法语文本图像串
print(pytesseract.image_to_string(Image.open(args["image"]),lang='fra'))  # 需要D:/Tesseract-OCR/tessdata/fra.traineddata# 获取详细的数据,包括盒,置信线和页码
print(pytesseract.image_to_data(Image.open(args["image"])))
# 获取有关方向和脚本检测信息
# print(pytesseract.image_to_osd(Image.open(args["image"])))
print('image_to_osd: ', pytesseract.image_to_osd(args["image"]))
# 获取包围盒估计
# print(pytesseract.image_to_boxes(Image.open(args["image"])))
print('image_to_boxes: ', pytesseract.image_to_boxes(args["image"]))rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = pytesseract.image_to_string(rgb)
print(results)
results = pytesseract.image_to_osd(args["image"], output_type=Output.DICT, config="--dpi 70")# 展示定向信息
# 当前方向
# 旋转图像以校正其方向的度数
# 检测到的脚本类型,如拉丁语或阿拉伯语
print("[INFO] detected orientation: {}".format(results["orientation"]))
print("[INFO] rotate by {} degrees to correct".format(results["rotate"]))
print("[INFO] detected script: {}".format(results["script"]))def rotate_bound(image, angle):# grab the dimensions of the image and then determine the# center(h, w) = image.shape[:2](cX, cY) = (w / 2, h / 2)# grab the rotation matrix (applying the negative of the# angle to rotate clockwise), then grab the sine and cosine# (i.e., the rotation components of the matrix)M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)cos = np.abs(M[0, 0])sin = np.abs(M[0, 1])# compute the new bounding dimensions of the imagenW = int((h * sin) + (w * cos))nH = int((h * cos) + (w * sin))# adjust the rotation matrix to take into account translationM[0, 2] += (nW / 2) - cXM[1, 2] += (nH / 2) - cY# perform the actual rotation and return the imagereturn cv2.warpAffine(image, M, (nW, nH), borderValue=(255, 255, 255))def getImg(img, angel,ls):print(angel+ls)rotated = rotate_bound(img, angle=angel+ls)return imutils.resize(rotated, width=300)# 旋转图像以校正方向
# 使用OpenCV的通用cv2.rotate方法,部分图像可能会丢失
# imutils.rotate_bound 不丢失保留边界的旋转图像
rotated = rotate_bound(img, angle=results["rotate"])# 展示原始图像和校正后的图像
cv2.imshow("Original", imutils.resize(img, width=300))
cv2.imshow("Output", imutils.resize(rotated,width=300))for i in range(0,361,30):cv2.imshow("Output"+str(results["rotate"]+i), getImg(img,results["rotate"],i))
cv2.waitKey(0)

参考

  • https://pyimagesearch.com/2022/01/31/correcting-text-orientation-with-tesseract-and-python/
  • https://digi.bib.uni-mannheim.de/tesseract/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pgtn.cn/news/17589.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

30岁前挣够500万

教你30岁前挣够500万!(不妨看完,心态会改变。) 成功源于自信!相信自己。下边每个字都是价值不菲,你认真看了吗?一艘没有航行目标的船,任何方向的风都是逆风1、你为什么是穷人&#x…

Postgresql Geometry与wkt互转及长度计算

这篇博客将介绍如何使用Postgresql 进行Geometry与wkt互转及长度计算。首先构建表和数据,然后sql计算。 1. pgadmin4下的效果图: 计算距离效果图如下 也可以查看geom渲染效果图 右侧可选择不同的底图属性: 2. 构建数据表及数据sql CRE…

opencv-mediapipe手部关键点识别

文章目录可视化辅助函数单张图片摄像头检测改变关键点数据特征可视化辅助函数 在下面的代码的注释内有大致的操作 基本操作与前面的人脸检测的操作相似,增加了可视化的辅助函数 import matplotlib.pyplot as plt # 使用ipython的魔法方法,将绘制出的图像…

使用Python,OpenCV制作不同风格的素描图(正常,漫画,写实风格)

使用Python,OpenCV制作不同风格的素描图(正常,漫画,写实风格) 这篇博客将介绍如何使用Python,OpenCV制作不同风格的素描图(正常风格,漫画风格,写实风格)。 …

使用Python,OpenCV反转视频

使用Python,OpenCV反转视频 这篇博客将介绍如何使用Python,OpenCV反转视频,可以通过先把帧都保存下来在反转但太浪费时间和内存,用本文的方法又简洁又快速。 效果图 原始视频: 反转视频: 原理 cv.CAP_PROP_FRAME_COUNT 视频文件中的帧数 cv.CAP_PROP_POS_FRAMES …

蓝图基础

蓝图基础 hello1先输出,延迟1.0秒后hello2再输出 设置变量 先输出hello world,延迟1.0秒之后再输出helloworld2, 逐帧输出: 每隔0.2秒打印一行字符串 打印倒三角形 自定义函数计算三角形面积 调用自定义函数

记录|深度学习100例-卷积神经网络(CNN)minist数字分类 | 第1天

记录|深度学习100例-卷积神经网络(CNN)minist数字分类 | 第2天 1. minist0-9数字分类效果图 数据集如下: 分类及预测图如下:预测标签值和真实标签值如下图所示,成功预测 训练Loss/Accuracy图如下: 源…

使用SharpPCap在C#下进行网络抓包

转自http://www.cnblogs.com/billmo/archive/2008/11/09/1329972.html 在做大学最后的毕业设计了,无线局域网络远程安全监控策略那么抓包是这个系统设计的基础以前一直都是知道用winpcap的,现在网上搜了一下,有用C#封装好了的,很好用下面是其中的几个用法这个类库作者的主页:ht…