python - OCR 空间 API Python 不打印文本
问题描述
我正在尝试发送图像以ocr.space
使用他们的 api 和 python。这是我的代码。代码运行没有错误,但不打印任何内容。为什么?我怎样才能做到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')
这是我的代码的粘贴箱。
解决方案
我修好了它。我修复了代码中的最后一行。这是我新的最后一行 - (ocr_space_file(filename='example_image.png', language='eng'))
推荐阅读
- uwp - 使用在 Windows Template Studio 中创建的 UWP 到 Uno 平台
- sql-server - SQL 函数 - 当子查询跟随 =、!=、<、<=、>、>= 或当子查询用作表达式时,这是不允许的
- android - 如何在 Jetpack Compose 中使用小部件?
- javascript - 如何屏蔽和取消屏蔽 HTML 标头文本
- twig - 在“for”上使用“if”条件
- google-sheets - 仅删除 Google 表格中的空白单元格(而不是行)
- navigation - Android 底部导航 - 从另一个导航图中调用操作
- c# - 如何在 C# 中使用 lambda 表达式来处理矩阵 LinkedList