问题描述
由于此JSON阅读脚本(Microsoft模板代码),我有一个Azure OCR输出:
# 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.'}
解决方法
在您的代码中,您已经获得了行信息。
line_infos = [region["lines"] for region in analysis["regions"]]
例如,我将此image用作OCR。
以下是line_infos
[{'boundingBox': '28,16,288,41','words': [{'boundingBox': '28,'text': 'NOTHING'}]},{'boundingBox': '27,66,283,52','words': [{'boundingBox': '27,'text': 'EXISTS'}]},128,292,49','text': 'EXCEPT'}]},{'boundingBox': '24,188,54','words': [{'boundingBox': '24,'text': 'ATOMS'}]},{'boundingBox': '22,253,297,32','words': [{'boundingBox': '22,105,'text': 'AND'},{'boundingBox': '144,175,'text': 'EMPTY'}]},{'boundingBox': '21,298,304,60','words': [{'boundingBox': '21,'text': 'SPACE.'}]},{'boundingBox': '26,387,294,37','words': [{'boundingBox': '26,210,'text': 'Everything'},{'boundingBox': '249,389,71,27','text': 'else'}]},{'boundingBox': '127,431,198,36','words': [{'boundingBox': '127,31,29','text': 'is'},{'boundingBox': '172,153,'text': 'opinion.'}]}]
让我们更接近图像中“其他所有内容”的输出,因为它们位于同一行:
{'boundingBox': '26,'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)
代码段的输出: