百度api使用:文字识别(OCR)、长图文字识别、姓名识别

先安装 pip install baidu-aip

文字识别

百度OCR接口

简单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)

百度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)

# 结果
['张大天', '小天']
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值