【问题标题】:Python Detect charecter tesseract ocr create blobs for text using pytesseractPython Detect charecter tesseract ocr 使用 pytesseract 为文本创建 blob
【发布时间】:2019-06-24 14:33:25
【问题描述】:

我正在研究 tesseract 并为图像中的字符创建了盒子文件。 在命令行中使用tesseract,它已经检测到了字符并显示了每个检测到的字符在box文件中的位置。

这是命令行输出。

/Desktop $ tesseract spa.arial.first_page.tif spa.arial.box nobatch 

box.train .stderr
read_params_file: Can't open .stderr
Tesseract Open Source OCR Engine v4.0.0-146-gc39a with Leptonica
Page 1
Detected 74 diacritics
row xheight=2, but median xheight = 17.4815
row xheight=2.5, but median xheight = 17.4815
row xheight=91, but median xheight = 17.4815
row xheight=2.5, but median xheight = 17.4815
row xheight=3, but median xheight = 17.4815
row xheight=61.875, but median xheight = 17.4815
row xheight=23, but median xheight = 17.4815
row xheight=3, but median xheight = 17.4815
row xheight=3, but median xheight = 17.4815
row xheight=12.8333, but median xheight = 17.4815
row xheight=15.1282, but median xheight = 17.4815
row xheight=3.5, but median xheight = 17.4815
row xheight=3.5, but median xheight = 17.4815
row xheight=3.5, but median xheight = 17.4815
row xheight=628, but median xheight = 17.4815
row xheight=415.5, but median xheight = 17.4815
row xheight=4, but median xheight = 17.4815
row xheight=630, but median xheight = 17.4815
FAIL!
APPLY_BOXES: boxfile line 7/A ((286,1979),(325,2002)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 11/U ((199,1943),(239,1967)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 14/R ((298,1943),(323,1967)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 16/M ((325,1943),(360,1967)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1611/a ((849,451),(875,480)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1617/5 ((947,457),(973,480)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1622/. ((1038,457),(1042,460)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1839/a ((679,280),(705,303)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1860/u ((1030,274),(1063,304)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1865/p ((1113,274),(1133,304)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1876/a ((1303,275),(1329,302)): FAILURE! Couldn't find a matching blob
FAIL!
APPLY_BOXES: boxfile line 1879/, ((1362,275),(1365,282)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1886/c ((1467,278),(1494,301)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1889/d ((1542,277),(1551,300)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1892/h ((1569,277),(1595,300)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1895/c ((619,245),(645,268)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1910/n ((888,245),(920,262)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1911/l ((941,245),(949,267)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: boxfile line 1913/e ((981,239),(997,267)): FAILURE! Couldn't find a matching blob
APPLY_BOXES: Unlabelled word at :Bounding box=(133,887)->(1631,893)
APPLY_BOXES: Unlabelled word at :Bounding box=(132,569)->(1631,575)
APPLY_BOXES: Unlabelled word at :Bounding box=(132,484)->(1631,491)
APPLY_BOXES: Unlabelled word at :Bounding box=(1408,418)->(1470,479)
APPLY_BOXES: Unlabelled word at :Bounding box=(132,413)->(1630,420)
APPLY_BOXES: Unlabelled word at :Bounding box=(1238,346)->(1415,400)
APPLY_BOXES: Unlabelled word at :Bounding box=(1408,359)->(1476,425)
APPLY_BOXES: Unlabelled word at :Bounding box=(133,341)->(1628,348)
APPLY_BOXES: Unlabelled word at :Bounding box=(133,205)->(137,1461)
APPLY_BOXES: Unlabelled word at :Bounding box=(598,203)->(602,1034)
APPLY_BOXES: Unlabelled word at :Bounding box=(133,200)->(1629,208)
APPLY_BOXES: Unlabelled word at :Bounding box=(1628,200)->(1633,1460)
   Found 1698 good blobs.
   Leaving 59 unlabelled blobs in 0 words.
   21 remaining unlabelled words deleted.
Generated training data for 353 words

我想为每个检测到的 blob 绘制 blob(box),我已搜索但未能获得参考。 谁能帮我在创建文件的图像上绘制 blob。

我尝试在下面的 python 代码中使用 pytesseract 为文本绘制 blob

import cv2
import pytesseract

file = '/home/Desktop/second_page.png'

img = cv2.imread(file)
h, w, _ = img.shape

boxes = pytesseract.image_to_boxes(img)

for b in boxes.splitlines():
    b = b.split(' ')
    img = cv2.rectangle(img, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 255, 0), 2)

cv2.imshow(filename, img)
cv2.waitKey(0)

得到的输出:

【问题讨论】:

  • 我可以帮助您编写相同的python代码...我不使用命令行中的OCR...所以无法帮助您
  • @PuneetSinha,请帮我编写 python 代码,我已经编辑了我的帖子并添加了我尝试过的代码。

标签: python blob ocr tesseract python-tesseract


【解决方案1】:

如需全面了解,请尝试使用链接 https://www.pyimagesearch.com/2017/07/10/using-tesseract-ocr-python/

import csv
import cv2
from pytesseract import pytesseract as pt

pt.run_tesseract('bw.png', 'output', lang=None, boxes=True, config="hocr")

# To read the coordinates
boxes = []
with open('output.box', 'rb') as f:
    reader = csv.reader(f, delimiter = ' ')
    for row in reader:
        if(len(row)==6):
            boxes.append(row)

# Draw the bounding box
img = cv2.imread('bw.png')
h, w, _ = img.shape
for b in boxes:
    img = cv2.rectangle(img,(int(b[1]),h-int(b[2])),(int(b[3]),h-int(b[4])),(255,0,0),2)

cv2.imshow('output',img)

休息一下,我建议您从上面提供的链接中获得更好的理解。

使用 pytesseract 和 image_to_boxes 方法获取图像中的字符及其位置:

ret = pt.image_to_boxes(pil_img)

结果是一个字符串,其中每一行都是一个字符,它的位置 (x1, y1, X2, y2) 用空格分隔:

a 30 10 45 20
b 49 12 54 23

【讨论】:

  • 我能够从图像中提取文本。我的目标是打印 tesseract 检测到的每个字符的 blob。
  • 那是图像分割的问题……我也会帮你写代码……给我一点时间。
  • 现在试试...如果有帮助,请告诉我
  • 抛出错误TypeError: run_tesseract() got an unexpected keyword argument 'boxes'
  • stackoverflow.com/questions/49614028/… 我的错...用这个...
猜你喜欢
  • 1970-01-01
  • 2015-09-04
  • 2012-11-05
  • 2021-12-12
  • 2018-09-29
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多