在您的代码中,您已经获得了线路信息。
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)
片段的输出: