1.申请API key
腾讯云目前提供每个月1000次图片识别api调用次数,
开通文字识别api地址如下https://console.cloud.tencent.com/ocr/overview,找不到的话在云产品下找通用文字识别
获取api密钥:https://console.cloud.tencent.com/cam/capi,如图所示
2.安装python环境
下载地址如下:https://www.python.org/downloads/,安装过程需要选中添加环境变量,然后一路回车即可
win10菜单搜索【管理应用执行别名】关闭下面两个按钮【应用安装程序】,如下图:
3.安装相关的包
win建+cmd在命令行中运行如下内容:
python -m pip install --upgrade pip
pip install jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install jupyterlab -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install tencentcloud-sdk-python -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
jupyter lab --ip='*' --port=8888 --no-browser --allow-root
然后浏览器访问127.0.0.1:8888并输入token,token位置如下:
4.目录结构
from目录为原始图片位置
to目录为输出txt文件夹位置
ipython为主程序文章来源:https://www.toymoban.com/news/detail-460613.html
新建ipython页面并将代码复制如下:
注:需要使用自己的SecretId和SecretKey替换这一行cred = credential.Credential(“SecretId”, “SecretKey”):文章来源地址https://www.toymoban.com/news/detail-460613.html
import base64
import os
import json
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
def translate(image_base64):
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 = {"ImageBase64": image_base64,
"Preprocess": True
}
req.from_json_string(json.dumps(params))
resp = client.EnglishOCR(req)
return json.loads(resp.to_json_string())
#print(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
image_dir = r'from'
txt_dir = r'to'
images= os.listdir(image_dir)
s = []
for image in images:
if not os.path.isdir(image):
image_path=image_dir+"/"+image
type1=str.lower(image_path.split(".")[1])
with open(image_path, 'rb') as f:
imagefile = f.read()
image_base64 = "data:image/"+type1+";base64,"+str(base64.b64encode(imagefile), encoding='utf-8')
dict1=translate(image_base64)
#print(dict1)
str1=''
for i in dict1['TextDetections']:
str1=str1+i['DetectedText']+'\n'
txt_path=txt_dir+"/"+image.split(".")[0]+".txt"
with open(txt_path, "w", encoding='utf-8') as f:
f.write(str(str1))
f.close()
#print(image_base64)
到了这里,关于利用腾讯云api实现手写字体识别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!