我使用OCR空间API从图像中提取文本。我希望'ParsedText'单独在一个字符串中。在
import requests
import json
def ocr_space_file(filename, overlay=False, api_key=API_KEY, language='eng'):
""" OCR.space API request with local file.
Python3.5 - not tested on 2.7
:param filename: Your file path & name.
:param overlay: Is OCR.space overlay required in your response.
Defaults to False.
:param api_key: OCR.space API key.
Defaults to 'helloworld'.
:param language: Language code to be used in OCR.
List of available language codes can be found on https://ocr.space/OCRAPI
Defaults to 'en'.
:return: Result in JSON format.
"""
payload = {'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
}
with open(filename, 'rb') as f:
r = requests.post('https://api.ocr.space/parse/image',
files={filename: f},
data=payload,
)
m = r.content.decode()
jsonstr = json.loads(m)
print jsonstr["ParsedResults"]
ocr_space_file(filename='sample.png', language='eng')
输出:
^{pr2}$我试过了
print jsonstr["ParsedResults"]["ParsedText"]
但它给出了一个错误:
Traceback (most recent call last):
File "img.py", line 33, in <module>
ocr_space_file(filename='sample.png', language='eng')
File "img.py", line 29, in ocr_space_file
print jsonstr["ParsedResults"]["ParsedText"]
TypeError: list indices must be integers, not str
请帮帮我。在
谢谢!在
使用这样的方法:
您的
jsonstr["ParsedResults"]
是数组中的单个字典。在执行
jsonstr["ParsedResults"][0]
以取出字典,例如:jsonstr["ParsedResults"][0]["ParsedText"]
相关问题 更多 >
编程相关推荐