gpt4 book ai didi

Python 检测字符 tesseract ocr 使用 pytesseract 为文本创建 blob

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:58 25 4
gpt4 key购买 nike

我正在研究超正方体并为图像中的字符创建了盒子文件。在命令行上使用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 (框),我已经搜索但未能获得引用。谁能帮我在创建文件的图像上绘制 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)

输出得到:

enter image description here

最佳答案

为了充分理解,请尝试使用链接 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

关于Python 检测字符 tesseract ocr 使用 pytesseract 为文本创建 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com