先安装 pip install baidu-aip
文字识别
简单demo
from aip import AipOcr
APP_ID = 'xx'
API_KEY = 'xxx'
SECRET_KEY = 'xx'
# 定义参数变量
options = {
'language_type': 'CHN_ENG',
"detect_direction": False,
"detect_language": False,
"probability": False,
}
# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 图片路径
file_path = 'D:\\1.jpg'
# 法1 open读取图片
with open(file_path, 'rb') as f:
img_data = f.read()
# 法2 image对象读取图片
def get_img_data(image):
"""读取image二进制数据"""
imgByteArr = io.BytesIO()
image.save(imgByteArr, format='PNG')
return imgByteArr.getvalue()
image = Image.open(file_path) # image对象
img_data = get_img_data(image)
result = aipOcr.general(img_data, options)
for item in result['words_result']:
print(item['words'])
长图文字识别
def cut_image(image):
"""切分图片"""
# 此处需要精确,避免字被切两份。
LH = 50.1 # 截图测行高(px)
TOP = 4 # 顶部空格(px)
LINES = 20 # 每张行数
cut_h = LINES * LH - LH / 2 + TOP
image_list = []
for i in range(0, image.size[1]*10, int(cut_h*10)):
i = i / 10 # 先乘后除,i取小数
# (左-左, 上-上, 右-左, 下-上) 左-左: 裁剪框(左边)到图像框(左边)的边距
# print((0, i, img.size[0], i + height))
crop_box = image.crop((0, i, image.size[0], i + cut_h))
image_list.append(crop_box)
return image_list
def img_to_words(img_data):
"""识别文字,并保存"""
result = aipOcr.general(img_data, options)
with open('D:\\1.txt', 'a', encoding='utf-8') as f:
for item in result['words_result']:
f.write(item['words'] + '\n')
# 裁剪图片并转为二进制数据
image_list = [get_img_data(img) for img in cut_image(image)]
for img_data in image_list:
# 图片识别并保存
img_to_words(img_data)
加上os.listdir可以一次访问文件夹下多个文件
for file_name in os.listdir(dir_path):
if os.path.splitext(file_name)[1] in ['jpg', 'png']:
print(file_name)
姓名识别
使用自然语言处理(NLP)
可以识别词性
pos
词性 | 含义 | 词性 | 含义 | 词性 | 含义 | 词性 | 含义 |
---|---|---|---|---|---|---|---|
n | 普通名词 | f | 方位名词 | s | 处所名词 | t | 时间名词 |
nr | 人名 | ns | 地名 | nt | 机构团体名 | nw | 作品名 |
nz | 其他专名 | v | 普通动词 | vd | 动副词 | vn | 名动词 |
a | 形容词 | ad | 副形词 | an | 名形词 | d | 副词 |
m | 数量词 | q | 量词 | r | 代词 | p | 介词 |
c | 连词 | u | 助词 | xc | 其他虚词 | w | 标点符号 |
ne
缩略词 | 含义 | 缩略词 | 含义 | 缩略词 | 含义 | 缩略词 | 含义 |
---|---|---|---|---|---|---|---|
PER | 人名 | LOC | 地名 | ORG | 机构名 | TIME | 时间 |
from aip import AipNlp
APP_ID = 'xx'
API_KEY = 'xxx'
SECRET_KEY = 'xx'
aipNlp= AipNlp(APP_ID, API_KEY, SECRET_KEY)
text = '我叫张大天,我有个弟弟叫小天'
# 如果出现编码错误,加上
# text = str(text.encode('gbk', 'ignore'), encoding='gbk')
# 分析结果
# print(aipNlp.lexer(text))
# 提取名字
result = []
for i in aipNlp.lexer(text)['items']:
if i['ne'] == 'PER' or i['pos'] == 'nr':
result.append(i['item'])
print(result)
# 结果
['张大天', '小天']