Tesseract-根本无法识别希腊字母

时间:2020-11-01 13:56:49

标签: python ocr tesseract training-data python-tesseract

我正在尝试自动从图像中提取比例尺(比例尺+数字+单位)。这是一个示例:

enter image description here

它用于将像素映射到现实世界中的测量。

我正在使用 PyTesseract (通过 Anaconda3 安装)。

这是我的代码:

import cv2
import pytesseract
import numpy as np

img = cv2.imread('pbmk_scale.tif')
#img = cv2.imread('ocr_test_greek_and_english.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Morph open to remove noise and invert image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
invert = 255 - opening

# Line detection for the scale line
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
x1,y1,x2,y2 = lines[0][0]
print('Line (' + str(x1) + ',' + str(y1) + ') -- (' + str(x2) + ',' + str(y2) + ')')
# Calculating lenght of scale line in pixels. Since the line is always horizontal we need to just subtract the X coordinates
l = abs(x1 - x2)
print('Line is ' + str(l) + 'px long')

# Text recognition for the scale number and real unit
# FIXME Greek not detected. Is it grc or ell for the configuration? Both don't work
custom_config = r'-l grc+eng --psm 1' # Greek (for mu and nu letters) and English (for m (metre))
text = pytesseract.image_to_string(img, config=custom_config)
print('OUTPUT:', text.split())
number = [int(s) for s in text.split() if s.isdigit()]
print('Number is ' + str(number))

到目前为止,它运行得非常好,特别是因为图像是通过氦离子显微镜生成的,并且标签(比例尺所在的位置)是自动生成的,并与图像一起存储为TIFF。因此,检测文本和行很重要。此外,比例尺始终位于图像中的同一位置,而实际比例线始​​终始终为水平。上面的代码有其缺陷,但是我对除了英语之外什么都检测不到的事实更感兴趣。

关于其提供的软件包的描述,Sanaly Anaconda非常具有加密性(特别是如果您查看导航器的话)。因此,我做了一些挖掘,在C:\Users\USER_NAME\anaconda3\envs\MachineLearning\tessdata(以MachineLearning为我的自定义虚拟环境)下,我发现了两件事:

  • 只有两个.traineddata文件-eng.traineddataosd.traineddata
  • eng.traineddata比我在Tstrong托管在 GitHub 上的Tesseract项目的 git回购中发现的要小得多(几乎十倍)。< / li>

我下载了多个训练有素的数据文件( eng ell grc )。我只用 grc ell (分开加在一起)进行了测试,并且图像中同时使用了希腊语和英语。例如下面的图片(在删除了上面代码的行检测部分之后)

enter image description here

产生以下结果:

OUTPUT: ['Here’s', 'some', 'GBeek', 'Od10', 'd1ota', 'iumEedit', 'Oy']

我尝试了 PSM 参数的各种值(这当然很合理),但没有任何改变。

我是OCR和Tesseract的新手,所以我可能缺少明显的东西。

0 个答案:

没有答案
相关问题