【问题标题】:Display Azure OCR output in logic order / reading-direction按逻辑顺序/读取方向显示 Azure OCR 输出
【发布时间】:2020-09-20 14:37:33
【问题描述】:

我有一个 Azure OCR 输出作为这个 JSON 读取脚本(Microsoft 模板代码)的结果:

# Extract the word bounding boxes and text.
line_infos = [region["lines"] for region in analysis["regions"]]
word_infos = []
for line in line_infos:
    for word_metadata in line:
        for word_info in word_metadata["words"]:
            word_infos.append(word_info)
word_infos

输出:

{'boundingBox': '183,73,624,102', 'text': 'This'},
{'boundingBox': '851,100,160,67', 'text': 'person'},
{'boundingBox': '1052,109,448,97', 'text': 'plays.'},
...

问题: 这三个词原本属于扫描文档的一行,但在 Azure OCR 输出中具有不同的边界框。我可以在 OCR 服务中调整边界框限制吗?或者是否有一个简洁的辅助函数来分析边界框坐标以便将最近的邻居捕捉在一起?

请求的输出将是:

{'boundingBox': 'xxx,xx,xxx,xxx', 'text': 'This person plays.'}

【问题讨论】:

    标签: python json azure ocr


    【解决方案1】:

    在您的代码中,您已经获得了线路信息。

    line_infos = [region["lines"] for region in analysis["regions"]]
    

    例如,我将这个 image 用于 OCR。

    下面是line_infos的输出

    [{'boundingBox': '28,16,288,41', 'words': [{'boundingBox': '28,16,288,41', 'text': 'NOTHING'}]}, {'boundingBox': '27,66,283,52', 'words': [{'boundingBox': '27,66,283,52', 'text': 'EXISTS'}]}, {'boundingBox': '27,128,292,49', 'words': [{'boundingBox': '27,128,292,49', 'text': 'EXCEPT'}]}, {'boundingBox': '24,188,292,54', 'words': [{'boundingBox': '24,188,292,54', 'text': 'ATOMS'}]}, {'boundingBox': '22,253,297,32', 'words': [{'boundingBox': '22,253,105,32', 'text': 'AND'}, {'boundingBox': '144,253,175,32', 'text': 'EMPTY'}]}, {'boundingBox': '21,298,304,60', 'words': [{'boundingBox': '21,298,304,60', 'text': 'SPACE.'}]}, {'boundingBox': '26,387,294,37', 'words': [{'boundingBox': '26,387,210,37', 'text': 'Everything'}, {'boundingBox': '249,389,71,27', 'text': 'else'}]}, {'boundingBox': '127,431,198,36', 'words': [{'boundingBox': '127,431,31,29', 'text': 'is'}, {'boundingBox': '172,431,153,36', 'text': 'opinion.'}]}]
    

    让我们从图像中更接近“其他”的输出,因为它们在同一行中:

    {'boundingBox': '26,387,294,37', 'words': [{'boundingBox': '26,387,210,37', 'text': 'Everything'}, {'boundingBox': '249,389,71,27', 'text': 'else'}]}
    

    它们已经在行级别分组,您必须相应地提取它。 以下是修改后的代码示例,用于在行级别提取它:

    line_num = 0 
    for line in line_infos:
        for word_metadata in line:
            word_infos = []
            line_num +=1
            for word_info in word_metadata["words"]:
                word_infos.append(word_info["text"])
            print(line_num)
            print (word_infos)
    

    片段的输出:

    【讨论】:

    • @jakhaf - 想进去看看上面的方法是否有帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 2019-09-02
    • 2016-01-31
    相关资源
    最近更新 更多