# -*- coding = utf-8 -*-
# @Software : PyCharm
import cv2
import requests
import base64
import json
import re
import openpyxl, random
import os
import win32api, win32con
import glob
if os.path.lexists('数据库.xlsx'):
# os.remove('数据库.xlsx')
# print('已经存在数据表')
wb = openpyxl.load_workbook('数据库.xlsx')
ws = wb.active
ws['A1'].value = '图片名称'
ws['B1'].value = '付款金额'
ws['C1'].value = '转账-交易单号'
ws['D1'].value = '微信号'
ws['E1'].value = '时间'
else:
wb = openpyxl.Workbook()
ws = wb.active
ws['A1'].value = '图片名称'
ws['B1'].value = '付款金额'
ws['C1'].value = '转账-交易单号'
ws['D1'].value = '微信号'
ws['E1'].value = '时间'
# wb.save('./数据库.xlsx')
litq = []
lith = []
litn = []
littime = []
litw = []
stu_num = []
stu_num2 = []
stu_num3 = []
stu_num4 = []
jinge = ''
dh = ''
wxh = ''
s_time = ''
# 获取图像
def get_file_content(file):
with open(file, 'rb') as f:
return f.read()
# 获取access_token
def gettoken():
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=PPKWbCWxkIh5kPhaYtoaZ5jr&client_secret=6f2adCZkDY6EOr8Z8FKhjceDwPRr89kQ'
response = requests.get(host)
# if response:
# print(response.json())
return response.json()
# 把图片里的文字识别出来
def img_to_str(filename):
# 通用文字识别(标准版)
#request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
# 通用文字识别(高精度版)
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# 网络图片识别
# request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
# 二进制方式打开图片文件
# request_url ="https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add"
f = get_file_content(filename)
img = base64.b64encode(f)
params = {"image": img}
access_token = gettoken()
request_url = request_url + "?access_token=" + access_token['access_token']
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
# print(response.json())
result_list = response.json()
return result_list
def main():
# 创建窗口
cv2.namedWindow("camera", 1)
# IP摄像头
# capture = cv2.VideoCapture('http://admin:admin@192.168.0.100:8081/video')
# 电脑本地摄像头
capture = cv2.VideoCapture(0)
while True:
success, img = capture.read()
cv2.imshow("camera", img)
# 按键
key = cv2.waitKey(10)
if key == 27: # 对应esc键
break
if key == 32: # 对应空格键
filename = 'frames.jpg' # 拍照存放照片的路径
cv2.imwrite(filename, img)
s = img_to_str(filename)
sxtou(s)
#print(s)
# 释放摄像头
capture.release()
# 关闭窗口
cv2.destroyWindow("camera")
def sxtou(ss):
for m in range(1, 1000):
if "当前状态" in ss['words_result'][m]['words']:
jinge = ss['words_result'][m - 1]['words']
# print(jinge)
if (ss['words_result'][m]['words'] == "交易单号") or (ss['words_result'][m]['words'] == "转账单号"):
dh = ss['words_result'][m + 1]['words']
chuli(jinge, dh)
# print(dh)
# wb.save('./数据库.xlsx')
break
# return jinge,dh
def tupian(tex):
for i in range(1, 1000):
if tex['words_result'][i]['words'] == "当前状态":
jine = tex['words_result'][i-1]['words']
litq.append(jine)
break
for i in range(1,1000):
if (tex['words_result'][i]['words'] == "交易单号") or (tex['words_result'][i]['words'] == "转账单号"):
danhao = tex['words_result'][i+1]['words']
lith.append(danhao)
break
for i in range(1,1000):
if "微信"in tex['words_result'][i]['words']:
wxh = tex['words_result'][i]['words']
litw.append(wxh)
break
for i in range(1,1000):
if (tex['words_result'][i]['words'] == "转账时间") or (tex['words_result'][i]['words'] == "支付时间"):
s_time = tex['words_result'][i+1]['words']
littime.append(s_time)
# wb.save('./数据库.xlsx')
break
# return litq,lith
# def to_excel(n, y, m, t):
def to_excel(n, y, m, w, t):
for i in range(0, len(n)):
ws.cell(row=i + 2, column=1, value=m[i])
ws.cell(row=i + 2, column=2, value=n[i])
ws.cell(row=i + 2, column=3, value=y[i])
ws.cell(row=i + 2, column=4, value=w[i])
ws.cell(row=i + 2, column=5, value=t[i])
wb.save('./原始数据库.xlsx')
def to_excellog(a,b):
num=1
if os.path.lexists('验证通过数据库.xlsx'):
# os.remove('数据库.xlsx')
# print('已经存在数据表')
wb = openpyxl.load_workbook('验证通过数据库.xlsx')
ws = wb.active
ws['A1'].value = '微信'
ws['B1'].value = '时间'
else:
wb = openpyxl.Workbook()
ws = wb.active
ws['A1'].value = '微信'
ws['B1'].value = '时间'
while 1:
cell = ws.cell(row=num, column=1).value
if cell:
num = num +1
else:
#print(num)
exit()
ws.cell(row=num, column=1, value=a)
ws.cell(row=num, column=2, value=b)
wb.save('./验证通过数据库.xlsx')
# 遍历邮箱存放地址的函数
def get_filelist(dir):
for home, dirs, files in os.walk(dir):
for dir in dirs:
print(dir)
for filename in files:
print(filename)
litn.append(filename)
fullname = os.path.join(home, filename)
text = img_to_str(fullname)
print(text)
tupian(text)
# to_excel(litq, lith, litn, littime)
to_excel(litq, lith, litn, litw, littime)
# print(text)
print(fullname)
def chuli(str_a, str_b):
mm=''
nn=''
workbook1 = openpyxl.load_workbook('原始数据库.xlsx')
# 选定目标sheet
worksheet1 = workbook1.active
py2 = False
for cell in worksheet1['B']:
# print(cell.value)
stu_num.append(cell.value) # 这里用循环把A列每个cell的值写入开始定义的空列表
for cell2 in worksheet1['C']:
# print(cell2.value)
stu_num2.append(cell2.value) # 这里用循环把A列每个cell的值写入开始定义的空列表
for cell3 in worksheet1['D']:
stu_num3.append(cell3.value)
for cell4 in worksheet1['E']:
stu_num4.append(cell4.value)
for i in range(0, len(stu_num)):
if (str_a == stu_num[i]) and (str_b == stu_num2[i]):
mm=stu_num3[i]
nn=stu_num4[i]
py2 = True
if py2:
to_excellog(mm,nn)
win32api.MessageBox(0, "数据库里有完全相同的数据,检验通过!!" + mm + nn , "提示", win32con.MB_OK)
# ('数据库里有完全相同的数据,检验通过!!')
else:
win32api.MessageBox(0, "检验不通过!!", "提示", win32con.MB_OK)
if __name__ == '__main__':
path_file_number = glob.glob('./邮箱图片存放')
if len(stu_num) - 1 != path_file_number:
get_filelist('D:\\修改\\邮箱图片存放')
main()