# 安装aip pip install baidu-aipimport os from aip import AipOcr print("正在努力识别中,请耐心等待".center(60)) APP_ID = '16741980' API_KEY = 'xxxxxxxxxxxxxxxx' # 需要自己去注册 SECRET_KEY = 'xxxxxxxxxxxxxxxxxxx' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) base_DIR = os.getcwd() id_list = os.listdir(os.path.join(base_DIR, "要识别的图片")) success_path = os.path.join(base_DIR, "successful.txt") count = 0 for id in id_list: img = open(os.path.join(base_DIR, "要识别的图片", id), 'rb').read() msg = client.basicGeneral(img) # 识别图片 str1 = "" for i in msg["words_result"]: print("{}".format(i["words"]).center(60)) str1 += "{} ".format(i["words"]) with open(success_path, 'a') as f: f.write("{} ".format(str1)) print(" ") print("写入中,请稍等。。。".center(60)) count += 1 print(" ") print("执行结束,共完成了{}图片的识别,资料已写入到successful.txt".format(count).center(60)) ss = input("")