安装模块:baidu-aip和pillow
from aip import AipOcr
import os,shutil,time
from PIL import Image
print("\n\n ~~~~~~~ ~~~~~~~~\n ~~~~~~~~~~ ~~~~~~~~~~~\n ~~~~~~冯冯小甜心~~~~~\n ~~~~~ ~~~~\n ~~~~~~~~~~~~~~~\n ~~~~~~~~~\n ~~~\n")
#初始化全局变量
exit_='Y'
while True:
pwd = input('请输入工具码(输入exit退出):')
if pwd=='exit':
break
if pwd!='19961220':
print("密码错误,请重新输入!")
continue
# 设置登录密码
if pwd == '19961220':
print('登录成功!\n')
while True:
file_ph=input('请输入需处理图片的路径:')
#主功能区
try:
# 这里填你的 APPID AK SK 获取网址:https://ai.baidu.com/tech/ocr/general
APP_ID = '23562478'
API_KEY = 'hXl1TFWRDipv0WVyhj7c0Fkp'
SECRET_KEY = 'qz1k3Ulcq9YmlRM8IHqLRQ4MHSh5TLGs'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 定义参数变量
options = {
# 定义图像方向
'detect_direction': 'true',
# 识别语言类型,默认为'CHN_ENG'中英文混合
'language_type': 'CHN_ENG',
# 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
'detect_language': 'true',
# 是否返回识别结果中每一行的置信度
'probability': 'false',
}
file_ph_res=os.path.dirname(file_ph)+'\\处理后' # 取文件路径的上级路径,生成文件夹
# 若合并后文件路径不存在,创建
if not os.path.isdir(file_ph_res):
os.mkdir(file_ph_res)
is_ok = 0 # 解析文件计数器
for root, dirs, files in os.walk(file_ph):
if files:
for file in files:
file_name = root + '\\'+file
if '.png' in file_name:
print(file_name)
img = Image.open(file_name)
size = img.size
# 准备将图片横向切割需求内容
weight = int(size[0])
height = int(size[1])
weight_ym_x1 =int(weight*1299/1654)
weight_ym_y1 =0
weight_ym_x2 =int(weight*1511/1654)
weight_ym_y2 =int(height*53/1065)
box = (weight_ym_x1, weight_ym_y1, weight_ym_x2, weight_ym_y2)
region = img.crop(box)
region.save(file_ph+"\\页码.jpg")
# 读取图片
with open(file_ph+"\\页码.jpg", 'rb') as fp:
image_ym = fp.read()
# 调用通用文字识别接口
result = client.basicGeneral(image_ym, options)
time.sleep(1)
if result['words_result'] and '页码' in result['words_result'][0]['words']:
# 获取日期坐标
weight_rq_x1 = int(weight * 1225 / 1654)
weight_rq_y1 = int(height * 263 / 1065)
weight_rq_x2 = int(weight * 1375 / 1654)
weight_rq_y2 = int(height * 317 / 1065)
# 获取收款人坐标
weight_skr_x1 = int(weight * 477 / 1654)
weight_skr_y1 = int(height * 503 / 1065)
weight_skr_x2 = int(weight * 845 / 1654)
weight_skr_y2 = int(height * 555 / 1065)
# 获取金额坐标
weight_je_x1 = int(weight * 471 / 1654)
weight_je_y1 = int(height * 751 / 1065)
weight_je_x2 = int(weight * 845 / 1654)
weight_je_y2 = int(height * 800 / 1065)
box_rq = (weight_rq_x1, weight_rq_y1, weight_rq_x2, weight_rq_y2)
box_skr = (weight_skr_x1, weight_skr_y1, weight_skr_x2, weight_skr_y2)
box_je = (weight_je_x1, weight_je_y1, weight_je_x2, weight_je_y2)
region = img.crop(box_rq)
region.save(file_ph + "\\日期.jpg")
region = img.crop(box_skr)
region.save(file_ph + "\\收款人.jpg")
region = img.crop(box_je)
region.save(file_ph + "\\金额.jpg")
# 读取图片
with open(file_ph + "\\日期.jpg", 'rb') as fp:
image_rq = fp.read()
# 调用通用文字识别接口
result_rq = client.basicGeneral(image_rq, options)
time.sleep(1)
rq=result_rq['words_result'][0]['words']
with open(file_ph + "\\收款人.jpg", 'rb') as fp:
image_skr = fp.read()
# 调用通用文字识别接口
result_skr = client.basicGeneral(image_skr, options)
time.sleep(1)
skr=result_skr['words_result'][0]['words']
with open(file_ph + "\\金额.jpg", 'rb') as fp:
image_je = fp.read()
# 调用通用文字识别接口
result_je = client.basicGeneral(image_je, options)
time.sleep(1)
je=result_je['words_result'][0]['words']
file_name_final=skr + "_" + rq + "_" + je + ".png"
else:
# 获取日期坐标
weight_rq_x1 = int(weight * 1225 / 1654)
weight_rq_y1 = int(height * 157 / 1065)
weight_rq_x2 = int(weight * 1375 / 1654)
weight_rq_y2 = int(height * 205 / 1065)
# 获取收款人坐标
weight_skr_x1 = int(weight * 477 / 1654)
weight_skr_y1 = int(height * 391 / 1065)
weight_skr_x2 = int(weight * 845 / 1654)
weight_skr_y2 = int(height * 441 / 1065)
# 获取金额坐标
weight_je_x1 = int(weight * 480 / 1654)
weight_je_y1 = int(height * 635 / 1065)
weight_je_x2 = int(weight * 845 / 1654)
weight_je_y2 = int(height * 689 / 1065)
box_rq = (weight_rq_x1, weight_rq_y1, weight_rq_x2, weight_rq_y2)
box_skr = (weight_skr_x1, weight_skr_y1, weight_skr_x2, weight_skr_y2)
box_je = (weight_je_x1, weight_je_y1, weight_je_x2, weight_je_y2)
region = img.crop(box_rq)
region.save(file_ph + "\\日期.jpg")
region = img.crop(box_skr)
region.save(file_ph + "\\收款人.jpg")
region = img.crop(box_je)
region.save(file_ph + "\\金额.jpg")
# 读取图片
with open(file_ph + "\\日期.jpg", 'rb') as fp:
image_rq = fp.read()
# 调用通用文字识别接口
result_rq = client.basicGeneral(image_rq, options)
time.sleep(1)
rq = result_rq['words_result'][0]['words']
with open(file_ph + "\\收款人.jpg", 'rb') as fp:
image_skr = fp.read()
# 调用通用文字识别接口
result_skr = client.basicGeneral(image_skr, options)
time.sleep(1)
skr = result_skr['words_result'][0]['words']
with open(file_ph + "\\金额.jpg", 'rb') as fp:
image_je = fp.read()
# 调用通用文字识别接口
result_je = client.basicGeneral(image_je, options)
time.sleep(1)
je = result_je['words_result'][0]['words']
file_name_final=skr + "_" + rq + "_" + je + ".png"
# 选取固定位置解析的内容
shutil.copy(file_name,file_ph_res+'\\'+file_name_final)
is_ok+=1
if is_ok!=0:
print("处理成功!\n")
# 临时文件删除
os.unlink(file_ph + "\\页码.jpg")
os.unlink(file_ph + "\\日期.jpg")
os.unlink(file_ph + "\\收款人.jpg")
os.unlink(file_ph + "\\金额.jpg")
else:
print("处理失败,未检测到png文件!\n")
except Exception as e:
print("处理失败!错误代码:", e)
exit_=input("是否继续?(Y/N):")
if exit_.upper()=='N':
break
if exit_.upper()=='N':
break
os.system('pause')