赞
踩
一般百度账号都是通用的,如果没有可以在百度AI开放平台注册一个账号。
地址:http://ai.baidu.com
pip install baidu-aip
备注:在pycharm里也可以在setting----Project Interpreter—右边绿色加号,输入baidu,安装baidu-aip
登录百度云控制台,文字识别 --> 创建应用 --> 应用名称等内容自己根据需要填写
应用创建完成后,会生成一个应用,字段代表的含义
AppID #账号ID
APIkey #针对接口访问的授权方式
SecretKey #密钥
#!/usr/bin/env python #-*- coding:utf-8 -*- from aip import AipOcr # 文字识别 pip install baidu-aip import os # 定义变量 AppID = "15245350" #账号ID APIkey = "6CVO9xxxxxxxxxxxx" #针对接口访问的授权方式 SecretKey = "BkrxxxxxxxxxxxxB6W7AMuC" #密钥 IMG_EXT = ['.png', '.jpg', '.jpeg', '.bmp'] #常见图片格式后缀名 # 初始化操作 client = AipOcr(AppID, APIkey, SecretKey) # 1、调用接口获取图片里面的文本内容 def cor_basic_general(file_path_name): print('图片转换地址为:{}'.format(file_path_name)) with open(file_path_name, 'rb') as f: content = f.read() api_result = client.basicGeneral(content) # 调用通用文字识别接口 # print(api_result) """ api_result的值:{ 'log_id': 3303489525243973687, 'words_result_num': 4, 'words_result': [{ 'words': '修仙路,踏歌行。身怀万阵之祖,如何登上遗忘之巅?世界顶级雇佣兵王穿越了,来到了一片遗忘之地,这里崇' }, { 'words': '尚力量,强者为尊。造枪造炮卖胸罩,打人打脸卖保险,弹琴唱歌修真路,杀人夺宝难拘束!这个世界太疯狂' }, { 'words': '了,摩托车会飞,枪炮与法宝共存,不仅有飞来飞去的神仙姐姐,还有这身材惹火的职业御姐……这不是想象' }, { 'words': '是真实的,因为它就在我们“隔壁' }]}""" words_result = (i['words'] for i in api_result['words_result']) # 文本内容 result = '\n'.join(words_result) # 图片的文本内容按照换行符拼接 return result # 2、判断文件类型,写入文件 def handdle_file(file_path_name): filename,ext = os.path.splitext(file_path_name) # 将文件名和拓展名分开 if ext in IMG_EXT: newname = filename + '.txt' # 保存到txt文件 result = cor_basic_general(file_path_name) # 调用上述方法 with open(newname, 'w',encoding='utf-8') as f: f.write(result) # 3、处理路径下的所有图片 def handdle_path(path): if os.path.isdir(path): for child_dir_or_file in os.listdir(path): child_path = os.path.join(path, child_dir_or_file) if os.path.isfile(child_path): handdle_file(child_path) else: handdle_path(child_path) if __name__ == '__main__': file_path = r'H:\python\ocr' # 图片路径 handdle_path(file_path)
人脸识别和文字识别类似,代码如下
#!/usr/bin/env python #-*- coding:utf-8 -*- from aip import AipFace """ 人脸识别的 APPID AK SK """ AppID = "15245350" #账号ID APIkey = "6CVO9xxxxxxxxxxxx" #针对接口访问的授权方式 SecretKey = "BkrxxxxxxxxxxxxB6W7AMuC" #密钥 #初始化操作 client = AipFace(AppID, APIkey, SecretKey) """ 读取图片 """ def get_file_content(file_path_name): with open(file_path_name, 'rb') as fp: return fp.read() images = [ get_file_content('example0.jpg'), get_file_content('example1.jpg'), ] """ 调用人脸比对 """ result_json = client.match(images); print(result_json) result = result_json['result'][0]['score'] if result > 80: print("同一個人") else: print("不是同一個人")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。