使用此代码,我在下图中的字符周围创建了一些边界框:
import csv
import cv2
from pytesseract import pytesseract as pt
pt.run_tesseract('bb.png', 'output', lang=None, boxes=True, config="hocr")
# To read the coordinates
boxes = []
with open('output.box', 'rt') 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('bb.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])), (0, 255, 0), 2)
cv2.imshow('output', img)
cv2.waitKey(0)
输出
我想要的是:
程序应在边界框的X轴上绘制一条垂直线(仅适用于第一个和第三个文本区域。中间的一个不得对该过程感兴趣)。
目标是这个(并且有另一种方法来实现它,请解释):一旦我有这两行(或更好,一组坐标),使用掩码覆盖这两个区域。
有可能吗?
来源图片:
按要求提供的CSV格式: 打印(框)
[['l', '56', '328', '63', '365', '0'], ['i', '69', '328', '76', '365', '0'], ['n', '81', '328', '104', '354', '0'], ['e', '108', '328', '130', '354', '0'], ['1', '147', '328', '161', '362', '0'], ['m', '102', '193', '151', '227', '0'], ['i', '158', '193', '167', '242', '0'], ['d', '173', '192', '204', '242', '0'], ['d', '209', '192', '240', '242', '0'], ['l', '247', '193', '256', '242', '0'], ['e', '262', '192', '292', '227', '0'], ['t', '310', '192', '331', '235', '0'], ['e', '334', '192', '364', '227', '0'], ['x', '367', '193', '398', '227', '0'], ['t', '399', '192', '420', '235', '0'], ['-', '440', '209', '458', '216', '0'], ['n', '481', '193', '511', '227', '0'], ['o', '516', '192', '548', '227', '0'], ['n', '553', '193', '583', '227', '0'], ['t', '602', '192', '623', '235', '0'], ['o', '626', '192', '658', '227', '0'], ['t', '676', '192', '697', '235', '0'], ['o', '700', '192', '732', '227', '0'], ['u', '737', '192', '767', '227', '0'], ['c', '772', '192', '802', '227', '0'], ['h', '806', '193', '836', '242', '0'], ['l', '597', '49', '604', '86', '0'], ['i', '610', '49', '617', '86', '0'], ['n', '622', '49', '645', '75', '0'], ['e', '649', '49', '671', '75', '0'], ['2', '686', '49', '710', '83', '0']]
修改
要使用zindarod
答案,您需要tesserocr。通过pip install tesserocr
安装可以为您提供各种错误。
我发现它的轮版(在几小时后尝试安装和解决错误,请在答案下面看我的评论......):here you can find/download it。
希望这会有所帮助..
答案 0 :(得分:3)
Googles tesseract-ocr已在page segmentation method(psm)中具有此功能。你只需要使用一个更好的python包装器,它会比 pytesseract 更多地暴露tesseract的功能。其中一个更好的是tesserocr。
图片的一个简单示例:
import cv2
import numpy as np
import tesserocr as tr
from PIL import Image
cv_img = cv2.imread('text.png', cv2.IMREAD_UNCHANGED)
# since tesserocr accepts PIL images, converting opencv image to pil
pil_img = Image.fromarray(cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB))
#initialize api
api = tr.PyTessBaseAPI()
try:
# set pil image for ocr
api.SetImage(pil_img)
# Google tesseract-ocr has a page segmentation methos(psm) option for specifying ocr types
# psm values can be: block of text, single text line, single word, single character etc.
# api.GetComponentImages method exposes this functionality
# function returns:
# image (:class:`PIL.Image`): Image object.
# bounding box (dict): dict with x, y, w, h keys.
# block id (int): textline block id (if blockids is ``True``). ``None`` otherwise.
# paragraph id (int): textline paragraph id within its block (if paraids is True).
# ``None`` otherwise.
boxes = api.GetComponentImages(tr.RIL.TEXTLINE,True)
# get text
text = api.GetUTF8Text()
# iterate over returned list, draw rectangles
for (im,box,_,_) in boxes:
x,y,w,h = box['x'],box['y'],box['w'],box['h']
cv2.rectangle(cv_img, (x,y), (x+w,y+h), color=(0,0,255))
finally:
api.End()
cv2.imshow('output', cv_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 1 :(得分:0)
我迟到了,正在寻找别的东西。我从来没有使用过tesser包装器,它们似乎只是为了没有真正的好处。他们所做的只是抽象出对子进程的调用吗?
这是我通过传递给子进程的args访问psm配置的方法。我已经包含oem,pdf和hocr参数以及完整性,但这不是必需的,你可以只传递psm参数。在终端进行帮助呼叫,因为有13个psm选项,4个用于oem。根据您的工作情况,质量可能高度依赖于psm。
可以使用subprocess.Popen()进行管道输入和输出,或者如果您有冒险精神,可以使用asyncio.create_subprocess_exec()以非常相同的方式异步进行。
import subprocess
# args
# 'tesseract' - the executable name
# path to the image file
# output file name - no extension tesser will add .txt .pdf .hocr etc etc
# optional params
# -psm x to set the page segmentation mode see more with tesseract --help-psm at the cli
# -oem x to set ocr engine mode see more with tesseract --help-osm
# can add a mode parameter to the end of the args list to get output in :
# searchable pdf - just add a parameter 'pdf' as below
# hOCR output (html) - just add 'hocr' as below
args = ['tesseract', 'Im1.tiff', 'Im1', '-psm 1', '-oem 2']
# args = ['tesseract', 'Im1.tiff', 'Im1', '-psm 1', '-oem 2', 'pdf']
# args = ['tesseract', 'Im1.tiff', 'Im1', '-psm 1', '-oem 2', 'hocr']
try:
proc = subprocess.check_call(args)
print('subprocess retcode {r}'.format(r=proc))
except subprocess.CalledProcessError as exp:
print('subprocess.CalledProcessError : ', exp)