pytesseract image_to_osd文字方向与文字编码检测
image_to_osd中osd的全称是Orientation and script detection,image_to_osd的用途是检测图像中文字方向和字符编码类型,同时会给出相应检测结果的置信度。本文介绍pytesseract中函数image_to_osd输出内容的意义及其用法。
环境说明
本文代码运行的前提是已设置Tesseract及tessdata的环境变量,请参考pytesseract image_to_data检测并定位图片中的文字。 若未设置环境变量,则需要在python代码中添加指定Tesseract与tessdata路径的代码,具体请参考链接。
image_to_osd 能做的
Orientation in degrees & Rotate
我们从pytesseract的介绍中截取一部分作为测试图像,命名为”pytesseract.png”,如下图所示:
我们在测试图像相同路径下新建文件命名为osd_test.py,输入如下代码:
1 2 3 4 5 6 7 8 9 |
from pytesseract import Output import pytesseract import cv2 image = cv2.imread("pytesseract.png") #swap color channel ordering from BGR (OpenCV’s default) to RGB (compatible with Tesseract and pytesseract). # By default OpenCV stores images in BGR format and since pytesseract assumes RGB format, # we need to convert from BGR to RGB format/mode: rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(pytesseract.image_to_osd(rgb)) |
第1~3行导入所需库文件;
第4行读入测试图像;
第8行代码将opencv的BGR转换为tesseract所需要的RGB格式。
第9行输出image_to_osd检测当前图像的结果,使用默认输出格式Output.STRING。
测试结果如下所示:
1 2 3 4 5 6 |
Page number: 0 Orientation in degrees: 0 Rotate: 0 Orientation confidence: 11.03 Script: Latin Script confidence: 20.00 |
image_to_osd也可以参考函数image_to_data的用法设定为Output.DICT的输出格式,并用相应的方式进行取用,如下代码所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
output = pytesseract.image_to_osd(rgb,output_type=Output.DICT) print("Page number in dict:" + str(output["page_num"])) print("Orientation in degrees in dict:" + str(output["orientation"])) print("Rotate in dict:" + str(output["rotate"])) print("Orientation confidence in dict:" + str(output["orientation_conf"])) print("Script in dict:" + str(output["script"])) print("Script confidence in dict:" + str(output["script_conf"])) |
上述使用Output.DICT输出格式的代码测试结果如下:
1 2 3 4 5 6 |
Page number in dict:0 Orientation in degrees in dict:0 Rotate in dict:0 Orientation confidence in dict:11.03 Script in dict:Latin Script confidence in dict:20.00 |
本文后续均使用image_to_osd的默认输出格式Output.STRING。
接下来将pytesseract.png顺时针旋转90度之后得到的图像作为测试图像,其名称为pytesseract90.jpg,将图像放在与osd_test.py相同路径下,将函数imread中的图像名称更换为此图。
pytesseract90.jpg的输出结果如下:
1 2 3 4 5 6 |
Page number: 0 Orientation in degrees: 90 Rotate: 270 Orientation confidence: 1.03 Script: Latin Script confidence: 26.67 |
现在将pytesseract.png顺时针旋转270度之后得到的图像作为测试图像,其名称为pytesseract270.jpg,将图像放在与osd_test.py相同路径下,将函数imread中的图像名称更换为此图。
pytesseract270.jpg的输出结果如下:
1 2 3 4 5 6 |
Page number: 0 Orientation in degrees: 270 Rotate: 90 Orientation confidence: 1.74 Script: Latin Script confidence: 19.17 |
结合上述测试结果以及Tesseract源码src/ccmain/osdetect.cpp相关内容可知:
[Page number] 当前图像所属的分页。
[Orientation in degrees] 记录当前图像中文本相对于其阅读的角度顺时针的旋转角度,取值范围为[0, 270, 180, 90]。
[Rotate]:记录当前图像中文本要转换为可阅读的角度,相对于当前图像的顺时针旋转量,取值范围为[0, 270, 180, 90]。与[Orientation in degrees] 值互补。
[Orientation confidence] 则表示当前[Orientation in degrees]和[Rotate]检测值的置信度。该置信度越大,检测结果越可信,但目前未找到其取值范围的说明。
[Script] 当前图片中文字的编码类型。
[Script confidence] 当前图片中文字编码类型的置信度。
Script & Script confidence
我们换一张图来进行测试,该图名称为laugh.png,图中有两行中文和两行英文。
其输出结果如下:
1 2 3 4 5 6 |
Page number: 0 Orientation in degrees: 0 Rotate: 0 Orientation confidence: 6.39 Script: Latin Script confidence: 1.28 |
我们在laugh.png的基础上,再增加两行中文,将其命令为laughPlus.png:
再来看其输出结果:
1 2 3 4 5 6 |
Page number: 0 Orientation in degrees: 0 Rotate: 0 Orientation confidence: 9.93 Script: Han Script confidence: 1.11 |
由以上测试结果推测,当汉字数量在图像中占优势时,Tesseract检测到的字符编码类型会变成[Han]。据此推测,Tesseract OSD的检测结果中,Script会输出图像占比最大的编码类型。
关于字符编码类型,大家可以参考https://zh.wikipedia.org/wiki/ISO_15924。另外,在Tesseract源码中有如下定义,可以帮助我们更好的理解编码类型。
1 2 3 4 5 6 7 8 9 10 |
// General scripts static const char* han_script = "Han"; static const char* latin_script = "Latin"; static const char* katakana_script = "Katakana"; static const char* hiragana_script = "Hiragana"; static const char* hangul_script = "Hangul"; // Pseudo-scripts Name const char* ScriptDetector::korean_script_ = "Korean"; const char* ScriptDetector::japanese_script_ = "Japanese"; const char* ScriptDetector::fraktur_script_ = "Fraktur"; |
image_to_osd 不能做的
1. 无法检测文字极少的图片
以上一篇文章中的图片为例,image_to_osd检测时会报错。
1 2 |
raise TesseractError(proc.returncode, get_errors(error_string)) pytesseract.pytesseract.TesseractError: (1, 'Tesseract Open Source OCR Engine v4.1.0 with Leptonica Warning: Invalid resolution 0 dpi. Using 70 instead. Estimating resolution as 395 Too few characters. Skipping this page Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.') |
2. 倾斜度与[0, 270, 180, 90]偏差较大时,检测结果不可信
我们将laugh.png顺时针旋转30度之后,命令为laugh30.png,将图像放在与osd_test.py相同路径下,将函数imread中的图像名称更换为此图。
其输出结果:
1 2 3 4 5 6 |
Page number: 0 Orientation in degrees: 0 Rotate: 0 Orientation confidence: 2.65 Script: Japanese Script confidence: 1.09 |
可见输出结果中Script与实际编码类型不符,因此大家在使用image_to_osd 函数时要量其力而为。
本文到此结束,感谢阅读,欢迎关注。