1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
from aip import AipOcr import time import os
start = time.time()
""" 你的 APPID AK SK """ APP_ID = '百度APPID' API_KEY = '百度AK' SECRET_KEY = '百度SK'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """ def get_file_content(filePath): print(filePath) with open(filePath, 'rb') as fp: return fp.read()
""" 写入文本 """ def write_on_txt(content,filePath,linefeed = "1"): """ content:要写入的内容 filePath:要写入文件的路径 linefeed :判断是否换行 - 1 为不换行 - 其他 为换行 """ with open(filePath,"a") as file: try: file.write(content) except: print("写入错误") else: if linefeed != "1": file.write("\n")
img_path = r"G:\Pictures\jy\test"
txt_path = r"G:\Pictures\jy\OCR\图片.txt"
options = {}
for root,dirs,files in os.walk(img_path): for file in files: file_dir = os.path.join(root, file) print(file_dir) write_on_txt("=============================",txt_path,"0") write_on_txt("文件名:"+ file_dir,txt_path,"0") if file_dir[-4:]==".png"or file_dir[-4:]==".JPG": image = get_file_content(file_dir) """ 调用通用文字识别, 图片参数为本地图片 """ a = client.basicGeneral(image, options) print() for dic in a['words_result']: print(dic['words']) write_on_txt(dic['words'],txt_path,"0")
end = time.time() print('Running time: %1.2f Seconds'%(end-start))
|