【问题标题】:How can I get line coordinates that readed by tesseract?如何获得由 tesseract 读取的线坐标?
【发布时间】:2020-04-21 15:57:44
【问题描述】:

有什么方法可以用tesseract逐行读取图像并获取线条的坐标?通常我可以阅读每个单词 tesseract 返回字典,我可以获得所有位置但没有线坐标选项?我正在使用 psm 6 逐行读取,但即使我使用它,我也会收到单词的坐标

d = pytesseract.image_to_data(img, lang="eng", output_type=Output.DICT)

【问题讨论】:

    标签: computer-vision tesseract python-tesseract


    【解决方案1】:

    您可以将属于每行的单词组合在一起,并从最左侧和最右侧的单词边界框中找到行边界框。 下面是用于将每行的单词组合在一起的 python 实现。

    text = pytesseract.image_to_data(img, lang="eng", output_type=Output.DICT)
    
    data = {}
    for i in range(len(text['line_num'])):
        txt = text['text'][i]
        block_num = text['block_num'][i]
        line_num = text['line_num'][i]
        top, left = text['top'][i], text['left'][i]
        width, height = text['width'][i], text['height'][i]
        if not (txt == '' or txt.isspace()):
            tup = (txt, left, top, width, height)
            if block_num in data:
                if line_num in data[block_num]:
                    data[block_num][line_num].append(tup)
                else:
                    data[block_num][line_num] = [tup]
            else:
                data[block_num] = {}
                data[block_num][line_num] = [tup]
    
    linedata = {}
    idx = 0
    for _, b  in data.items():
        for _, l in b.items():
            linedata[idx] = l
            idx += 1
    line_idx = 1
    for _, line in linedata.items():
         xmin, ymin = line[0][1], line[0][2]
         xmax, ymax = (line[-1][1] + line[-1][3]), (line[-1][2] + line[-1][4])
         print("Line {} : {}, {}, {}, {}".format(line_idx, xmin, ymin, xmax, ymax))
         line_idx += 1
    

    【讨论】:

    • 感谢您的回答flamelite,我们有线条和坐标。现在我需要达到特定的索引值是 tesseract 在字典中给出的哪一行具有该特定索引
    • 这些行按从上到下的顺序排序,因此您可以找到特定索引处的行
    • 实际上我的意思是当我从 tesseract 作为字典得到结果时,每个单词都有一个索引; ['', '', '', '', '“İGDAŞ', '', '', '', 'İİ', 'Y', '', 'Sanayi', 've', 'Ticaret', 'A.Ş.', ';', 'O', 'A', '', 'Kazım', 'Karabekir', 'Cd.No:4', 'J', ')', '', '', '', 'B.Mükellefler', 'V.D.4700022607', 'Belge', 'No:', '', '', '', '] 在该 dic 中,例如 4 索引,如“İGDAŞ”,我想在您的代码创建的行中找到该索引。它可以在任何一行。
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2012-07-22
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多