今天因为网课需要(实则自己懒)需要使用图片转文字,于是一开始博主准备用百度云,但是因为通用印刷没有英文标示,于是换到了腾讯云(API生成器好评),腾讯云给的额度没有百度云的高,但是也够用,1千次一个月的英文图片调用量。
这次用的是python,首先的准备工作是,以此为例
pip install tencentcloud-sdk-python
下载我们腾讯的py-sdk。然后使用我们的
API Explorer
然后生成出一段调用API接口的代码(博主用的HTTP调用)
# -*- coding = utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
try:
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-shanghai", clientProfile)
req = models.EnglishOCRRequest()
params = '{"ImageUrl":"图床地址"}'
req.from_json_string(params)
resp = client.EnglishOCR(req)
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
这样我们就获取到了json返回的图片解析。可以看到返回的内容在DetectedText上
但是如果在控制台上就是另一番风景,这个怎么看嘛,没法看,那有什么办法直接提取出来么,当然肯定有啦。用标准库里的json以及json.loads函数,废话不多说直接上代码
# -*- coding = utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
import json
try:
cred = credential.Credential("ID", "KEY")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-shanghai", clientProfile)
req = models.EnglishOCRRequest()
params = '{"ImageUrl":"图片地址"}'
req.from_json_string(params)
resp = client.EnglishOCR(req)
#print(resp.to_json_string())
t = json.loads(resp.to_json_string())
#print(type(t))
for item in t["TextDetections"]:
print(item["DetectedText"])
except TencentCloudSDKException as err:
print(err)
这样我们就搞定了控制台里输出的混乱问题。