我试图使用python的api向ocr.space
发送图像。这是我的密码。代码运行时没有错误,但不打印任何内容。为什么?我怎样才能使它print
??在
import requests
import json
def ocr_space_file(filename, overlay=False, api_key='helloworld', 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: 2fe3ae129e88957
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,
)
return r.content.decode()
def ocr_space_url(url, overlay=False, api_key='helloworld', language='eng'):
""" OCR.space API request with remote file.
Python3.5 - not tested on 2.7
:param url: Image url.
:param overlay: Is OCR.space overlay required in your response.
Defaults to False.
:param api_key: 2fe3ae129e88957
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 = {'url': url,
'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
}
m = r.content.decode()
jsonstr = json.loads(m)
print (jsonstr["ParsedResults"][0]["ParsedText"])
ocr_space_file(filename='example_image.png', language='eng')
这是我的代码的pastebin。在
我修好了。我修正了代码中的最后一行。这是我新的最后一行-(ocr_space_file(filename='example_图像.png',language='eng'))
相关问题 更多 >
编程相关推荐