下面识别单张图像中数字 代码
。
# -*- coding: UTF-8 -*-
#识别一张图片
from aip import AipOcr
# 定义常量
APP_ID = '24265076'
API_KEY = 'WKOU9l8FUbPi0Hbpr4itc4di'
SECRET_KEY = 'r7tDGFBciqVUAXH3SbgBnDfe6L6lCeVB'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 的方法"""
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定义图片的绝对路径
image = get_file_content('C:\\Users\\Lenovo\\Desktop\\test\\re\\11_re\\0387.png')
""" 调用通用文字识别(高精度版) """
restu1 = client.basicAccurate(image);
lists = restu1['words_result'] # 列表
for listss in lists:
print(listss['words'])
下面批量识别图像中数字 代码
。
下面展示一些 内联代码片
。
# python 3.7
# 百度tesseract-ocr使用,先注册获取API
#循环识别数字并输出,尚未实现输出到文本(问题在于输出一个替换一个,最后只有一个)
import os
from aip import AipOcr
""" API """
APP_ID = '24265076'
API_KEY = 'WKOU9l8FUbPi0Hbpr4itc4di'
SECRET_KEY = 'r7tDGFBciqVUAXH3SbgBnDfe6L6lCeVB'
# 初始化AipFace对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def img_to_str(image_path):
""" 可选参数 """
options = {}
options["language_type"] = "CHN_ENG" # 中英文混合
options["detect_direction"] = "true" # 检测朝向
options["detect_language"] = "true" # 是否检测语言
options["probability"] = "false" # 是否返回识别结果中每一行的置信度
image = get_file_content(image_path)
""" 带参数调用通用文字识别 """
result = client.basicGeneral(get_file_content(filePath), options)
# 格式化输出-提取需要的部分
if 'words_result' in result:
text = ('\n'.join([w['words'] for w in result['words_result']]))
#print(type(result), "和", type(text))
""" save """
#fs = open("C:\\Users\\Lenovo\\Desktop\\test\\re.txt", 'w') # 将str,保存到txt
#fs.write(text)
#fs.close()
return text