parent
425e8aa9d3
commit
aecd2576b5
@ -1,92 +1,123 @@
|
|||||||
|
from tkinter import *
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import messagebox
|
from tkinter import filedialog
|
||||||
import pymysql
|
import pymysql
|
||||||
|
from flask import Flask, render_template, request, redirect, url_for
|
||||||
|
|
||||||
class Application(tk.Frame):
|
conn = pymysql.connect(host='localhost',
|
||||||
def __init__(self, master=None):
|
user='root',
|
||||||
super().__init__(master)
|
password='123456',
|
||||||
self.master = master
|
database='scaner',
|
||||||
self.pack()
|
port=3306,
|
||||||
self.create_widgets()
|
charset='utf8')
|
||||||
|
|
||||||
def create_widgets(self):
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||||
self.label_username = tk.Label(self, text="Username:")
|
cursor = conn.cursor()
|
||||||
self.label_username.grid(row=0, column=0)
|
print("数据库连接成功!")
|
||||||
|
|
||||||
self.entry_username = tk.Entry(self)
|
|
||||||
self.entry_username.grid(row=0, column=1)
|
# 查询用户信息
|
||||||
|
def userinfo_query(username, password):
|
||||||
self.label_password = tk.Label(self, text="Password:")
|
cursor.execute("select * from `user`")
|
||||||
self.label_password.grid(row=1, column=0)
|
data = cursor.fetchall()
|
||||||
|
|
||||||
self.entry_password = tk.Entry(self, show="*")
|
username_list_length = len(data)
|
||||||
self.entry_password.grid(row=1, column=1)
|
|
||||||
|
for i in range(username_list_length):
|
||||||
self.register_button = tk.Button(self, text="Register", command=self.register_user)
|
username.append(data[i][0])
|
||||||
self.register_button.grid(row=2, column=1)
|
password.append(data[i][1])
|
||||||
|
|
||||||
self.login_button = tk.Button(self, text="Login", command=self.login_user)
|
|
||||||
self.login_button.grid(row=3, column=1)
|
def user_register():
|
||||||
|
deng = Tk()
|
||||||
def register_user(self):
|
deng.title("注册")
|
||||||
username = self.entry_username.get()
|
deng.geometry("300x200")
|
||||||
password = self.entry_password.get()
|
|
||||||
|
# 在GUI中添加新的输入字段和按钮
|
||||||
self.save_to_db(username, password)
|
label_new_username = Label(deng, text="新账号:")
|
||||||
self.clear_fields()
|
label_new_username.pack()
|
||||||
|
entry_new_username = Entry(deng)
|
||||||
def login_user(self):
|
entry_new_username.pack()
|
||||||
username = self.entry_username.get()
|
label_new_password = Label(deng, text="新密码:")
|
||||||
password = self.entry_password.get()
|
label_new_password.pack()
|
||||||
|
entry_new_password = Entry(deng, show="*")
|
||||||
self.validate_credentials(username, password)
|
entry_new_password.pack()
|
||||||
self.clear_fields()
|
|
||||||
|
btn_register = Button(deng, text="注册", command=lambda: register_action(entry_new_username, entry_new_password))
|
||||||
def save_to_db(self, username, password):
|
btn_register.pack(side='right', padx=50)
|
||||||
# 连接数据库
|
|
||||||
db = pymysql.connect(host='localhost',
|
|
||||||
user='your_username',
|
def register_action(entry_new_username, entry_new_password):
|
||||||
password='your_password',
|
cursor.execute("INSERT INTO user (`username`, `password`) VALUES (%s, %s)",
|
||||||
database='your_database',
|
(entry_new_username.get(), entry_new_password.get()))
|
||||||
charset='utf8mb4')
|
conn.commit()
|
||||||
|
print("注册成功!")
|
||||||
try:
|
|
||||||
with db.cursor() as cursor:
|
|
||||||
sql = "INSERT INTO users (username, password) VALUES (%s, %s)"
|
def user_login():
|
||||||
cursor.execute(sql, (username, password))
|
global count
|
||||||
db.commit()
|
count = 2
|
||||||
messagebox.showinfo("Registration", "User registered successfully!")
|
username_list = []
|
||||||
except Exception as e:
|
password_list = []
|
||||||
messagebox.showerror("Registration", f"Error: {e}")
|
userinfo_query(username_list, password_list)
|
||||||
finally:
|
|
||||||
db.close()
|
username_input = entry_username.get()
|
||||||
|
password_input = entry_password.get()
|
||||||
def validate_credentials(self, username, password):
|
|
||||||
# 连接数据库
|
if username_input not in username_list:
|
||||||
db = pymysql.connect(host='localhost',
|
label_message.config(text="账号错误!")
|
||||||
user='your_username',
|
else:
|
||||||
password='your_password',
|
if password_input in password_list:
|
||||||
database='your_database',
|
def choose_folde():
|
||||||
charset='utf8mb4')
|
root.destroy()
|
||||||
|
root5 = tk.Tk()
|
||||||
try:
|
root5.title('选择图片')
|
||||||
with db.cursor() as cursor:
|
root5.geometry('300x300')
|
||||||
sql = "SELECT * FROM users WHERE username=%s AND password=%s"
|
folder_path = filedialog.askdirectory()
|
||||||
cursor.execute(sql, (username, password))
|
if folder_path:
|
||||||
result = cursor.fetchone()
|
print(f"您选择了文件夹: {folder_path}")
|
||||||
if result:
|
|
||||||
messagebox.showinfo("Login", "Login successful!")
|
root.mainloop()
|
||||||
|
def callback():
|
||||||
|
rootback = tk.Tk()
|
||||||
|
rootback.title('用户界面')
|
||||||
|
rootback.geometry('300x300')
|
||||||
|
tk.Label(rootback, text='请选择你的功能').pack()
|
||||||
|
tk.Button(rootback, text="选择需要扫描的图片", command=choose_folde).pack()
|
||||||
|
rootback.mainloop()
|
||||||
|
|
||||||
|
# windows.destroy()
|
||||||
|
root = tk.Tk()
|
||||||
|
root.title('用户界面')
|
||||||
|
root.geometry('300x300')
|
||||||
|
tk.Label(root, text='请选择你的功能').pack()
|
||||||
|
tk.Button(root, text="选择需要扫描的图片", command=choose_folde).pack()
|
||||||
|
tk.mainloop()
|
||||||
else:
|
else:
|
||||||
messagebox.showerror("Login", "Invalid credentials.")
|
label_message.config(text="密码错误!还可以尝试{}次".format(count))
|
||||||
except Exception as e:
|
count += 1
|
||||||
messagebox.showerror("Login", f"Error: {e}")
|
if count == 3:
|
||||||
finally:
|
label_message.config(text="登录失败!")
|
||||||
db.close()
|
btn_login.config(state='disabled')
|
||||||
|
|
||||||
def clear_fields(self):
|
|
||||||
self.entry_username.delete(0, tk.END)
|
windows = Tk()
|
||||||
self.entry_password.delete(0, tk.END)
|
windows.title("登录")
|
||||||
|
windows.geometry("300x200")
|
||||||
root = tk.Tk()
|
label_username = Label(windows, text="账号:")
|
||||||
app = Application(master=root)
|
label_username.pack()
|
||||||
app.mainloop()
|
entry_username = Entry(windows)
|
||||||
|
entry_username.pack()
|
||||||
|
label_password = Label(windows, text="密码:")
|
||||||
|
label_password.pack()
|
||||||
|
entry_password = Entry(windows, show="*")
|
||||||
|
entry_password.pack()
|
||||||
|
|
||||||
|
btn_login = Button(windows, text="登录", command=user_login)
|
||||||
|
btn_login.pack(side='left', padx=50)
|
||||||
|
entry_password.pack()
|
||||||
|
btn_login = Button(windows, text='注册', command=user_register)
|
||||||
|
btn_login.pack(side='right', padx=50)
|
||||||
|
label_message = Label(windows, text="")
|
||||||
|
label_message.pack()
|
||||||
|
windows.mainloop()
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
# https://digi.bib.uni-mannheim.de/tesseract/
|
||||||
|
# 配置环境变量如E:\Program Files (x86)\Tesseract-OCR
|
||||||
|
# tesseract -v进行测试
|
||||||
|
# tesseract XXX.png 得到结果
|
||||||
|
# pip install pytesseract
|
||||||
|
# anaconda lib site-packges pytesseract pytesseract.py
|
||||||
|
# tesseract_cmd 修改为绝对路径即可
|
||||||
|
from PIL import Image
|
||||||
|
import pytesseract
|
||||||
|
import cv2
|
||||||
|
import os
|
||||||
|
|
||||||
|
preprocess = 'blur' #thresh
|
||||||
|
|
||||||
|
image = cv2.imread('scan.jpg')
|
||||||
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
|
if preprocess == "thresh":
|
||||||
|
gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
|
||||||
|
|
||||||
|
if preprocess == "blur":
|
||||||
|
gray = cv2.medianBlur(gray, 3)
|
||||||
|
|
||||||
|
filename = "{}.png".format(os.getpid())
|
||||||
|
cv2.imwrite(filename, gray)
|
||||||
|
|
||||||
|
text = pytesseract.image_to_string(Image.open(filename))
|
||||||
|
print(text)
|
||||||
|
os.remove(filename)
|
||||||
|
|
||||||
|
cv2.imshow("Image", image)
|
||||||
|
cv2.imshow("Output", gray)
|
||||||
|
cv2.waitKey(0)
|
Loading…
Reference in new issue