rba*_*dar 7 python ocr tesseract training-data python-tesseract
我正在尝试从图像中自动提取比例(比例尺+数字+单位)。这是一个例子:
\n\n它用于将像素映射到现实世界的测量。
\n我正在使用PyTesseract(通过Anaconda3安装)。
\n这是我的代码:
\nimport cv2\nimport pytesseract\nimport numpy as np\n\nimg = cv2.imread(\'pbmk_scale.tif\')\n#img = cv2.imread(\'ocr_test_greek_and_english.png\')\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\nblur = cv2.GaussianBlur(gray, (3,3), 0)\nthresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]\n\n# Morph open to remove noise and invert image\nkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))\nopening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)\ninvert = 255 - opening\n\n# Line detection for the scale line\nedges = cv2.Canny(gray,50,150,apertureSize = 3)\nminLineLength = 100\nmaxLineGap = 10\nlines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)\nx1,y1,x2,y2 = lines[0][0]\nprint(\'Line (\' + str(x1) + \',\' + str(y1) + \') -- (\' + str(x2) + \',\' + str(y2) + \')\')\n# Calculating lenght of scale line in pixels. Since the line is always horizontal we need to just subtract the X coordinates\nl = abs(x1 - x2)\nprint(\'Line is \' + str(l) + \'px long\')\n\n# Text recognition for the scale number and real unit\n# FIXME Greek not detected. Is it grc or ell for the configuration? Both don\'t work\ncustom_config = r\'-l grc+eng --psm 1\' # Greek (for mu and nu letters) and English (for m (metre))\ntext = pytesseract.image_to_string(img, config=custom_config)\nprint(\'OUTPUT:\', text.split())\nnumber = [int(s) for s in text.split() if s.isdigit()]\nprint(\'Number is \' + str(number))\nRun Code Online (Sandbox Code Playgroud)\n到目前为止,它工作得相当好,特别是因为图像是通过氦离子显微镜生成的,并且标签(比例尺所在的位置)是自动生成的,并与图像一起存储为 TIFF。因此,检测文本和线条是正确的。此外,比例尺始终位于图像中的同一位置,并且实际比例线始终是水平的。上面的代码有其缺陷,但我更感兴趣的是我无法检测到除英语之外的任何内容。
\n遗憾的是,Anaconda 在其提供的包的描述方面非常加密(特别是如果您查看导航器)。所以我做了一些挖掘C:\\Users\\USER_NAME\\anaconda3\\envs\\MachineLearning\\tessdata(作为MachineLearning我的自定义虚拟环境),我发现了两件事:
.traineddata文件 -eng.traineddata和osd.traineddataeng.traineddata中找到的小得多(几乎是 10 倍)。我下载了多个经过训练的数据文件(eng、ell和grc)。我仅使用grc和ell (单独加组合)进行了测试,并使用图像中的希腊语和英语进行了测试。例如下图(删除上面代码的线条检测部分后)
\n\n产生以下结果:
\nOUTPUT: [\'Here\xe2\x80\x99s\', \'some\', \'GBeek\', \'Od10\', \'d1ota\', \'iumEedit\', \'Oy\']\nRun Code Online (Sandbox Code Playgroud)\n我尝试了PSM参数的各种值(这当然是有意义的),但没有任何变化。
\n我是 OCR 和 Tesseract 的新手,所以我可能遗漏了一些非常明显的东西。
\n小智 0
尝试使用 png 图像和代码:
from PIL import Image
from pytesseract import *
img_path = r'your image path'
tessdata_dir_config = r'C:\Program Files\Tesseract-OCR\tessdata'
language = 'grc'
def process_image(iamge_name, lang_code, tessdata_dir_config):
return pytesseract.image_to_string(Image.open(iamge_name), lang=lang_code, config=tessdata_dir_config)
def print_data(data):
print(data)
def output_file(filename, data):
file = open(filename, "w+")
file.write(data)
file.close()
def main():
data_gr = process_image(img_path, language, tessdata_dir_config)
print_data(data_gr)
#output_file('my_ocr', data_gr)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)