4.4 KiB
4.4 KiB
In [ ]:
In [ ]:
from aip import AipOcr
import glob
from opencc import OpenCC
import time
""" 你的 APPID AK SK """
APP_ID = '17553946'
API_KEY = 'IMC1ss3Tyo3vEAdVH6jgcdv2'
SECRET_KEY = 'Iua6KdhuDtz159rjzGeFGpqgjhnoU0zZ'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
fi_path = './pic'
fls = glob.glob(f'{fi_path}/*.jpg')
fls.sort()
""" 调用通用文字识别, 图片参数为本地图片 """
#client.basicGeneral(image);
""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
#options["detect_direction"] = "true"
cc = OpenCC('t2s')
for fi in fls:
image = get_file_content(fi)
result= client.basicAccurate(image, options)
print(fi)
list1 = []
if 'words_result' in result:
for w in result['words_result']:
list1.append(cc.convert(w['words']))
print('\n'.join(list1))
time.sleep(5)In [ ]:
s = '宋文帝使人齎藥賜王景文死'
print(cc.convert(s))In [ ]:
import json
import types
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 base64
cred = credential.Credential("AKIDoZHHEB2lHbluEZvN8uYHwcdvacqfmDeJ","fEkr2VucwEQXXVN8jCd0CFBxdAe6Xny9")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
req = models.GeneralAccurateOCRRequest()
with open("pic/5002.jpg","rb") as f:
img_data = f.read()
img_base64 = base64.b64encode(img_data)
params = {
"ImageBase64": img_base64.decode('utf-8')
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个GeneralAccurateOCRResponse的实例,与请求对象对应
resp = client.GeneralAccurateOCR(req)
txt = resp.to_json_string()
print(type(txt))
#except TencentCloudSDKException as err:
# print(err)In [ ]:
dict1 = json.loads(txt)
for item in dict1['TextDetections']:
print(item['DetectedText'])In [ ]: