diff --git a/Python程序设计课程设计报告.doc b/Python程序设计课程设计报告.doc new file mode 100644 index 0000000..cb7ac52 Binary files /dev/null and b/Python程序设计课程设计报告.doc differ diff --git a/Scaner/.idea/.gitignore b/Scaner/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/Scaner/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Scaner/.idea/.name b/Scaner/.idea/.name new file mode 100644 index 0000000..2832c3e --- /dev/null +++ b/Scaner/.idea/.name @@ -0,0 +1 @@ +scan.py \ No newline at end of file diff --git a/Scaner/.idea/document-scanner.iml b/Scaner/.idea/document-scanner.iml new file mode 100644 index 0000000..955e5e8 --- /dev/null +++ b/Scaner/.idea/document-scanner.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Scaner/.idea/misc.xml b/Scaner/.idea/misc.xml new file mode 100644 index 0000000..d182c43 --- /dev/null +++ b/Scaner/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Scaner/.idea/modules.xml b/Scaner/.idea/modules.xml new file mode 100644 index 0000000..a44040c --- /dev/null +++ b/Scaner/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Scaner/10500.png b/Scaner/10500.png new file mode 100644 index 0000000..2ebee5a Binary files /dev/null and b/Scaner/10500.png differ diff --git a/Scaner/11048.png b/Scaner/11048.png new file mode 100644 index 0000000..2ebee5a Binary files /dev/null and b/Scaner/11048.png differ diff --git a/Scaner/1660.png b/Scaner/1660.png new file mode 100644 index 0000000..bd96425 Binary files /dev/null and b/Scaner/1660.png differ diff --git a/Scaner/21736.png b/Scaner/21736.png new file mode 100644 index 0000000..0946890 Binary files /dev/null and b/Scaner/21736.png differ diff --git a/Scaner/25680.png b/Scaner/25680.png new file mode 100644 index 0000000..2ebee5a Binary files /dev/null and b/Scaner/25680.png differ diff --git a/Scaner/26256.png b/Scaner/26256.png new file mode 100644 index 0000000..2ebee5a Binary files /dev/null and b/Scaner/26256.png differ diff --git a/Scaner/2948.png b/Scaner/2948.png new file mode 100644 index 0000000..bd96425 Binary files /dev/null and b/Scaner/2948.png differ diff --git a/Scaner/9704.png b/Scaner/9704.png new file mode 100644 index 0000000..2ebee5a Binary files /dev/null and b/Scaner/9704.png differ diff --git a/Scaner/__pycache__/scan.cpython-36.pyc b/Scaner/__pycache__/scan.cpython-36.pyc new file mode 100644 index 0000000..0e68f2a Binary files /dev/null and b/Scaner/__pycache__/scan.cpython-36.pyc differ diff --git a/Scaner/images/page.jpg b/Scaner/images/page.jpg new file mode 100644 index 0000000..8da4398 Binary files /dev/null and b/Scaner/images/page.jpg differ diff --git a/Scaner/images/receipt.jpg b/Scaner/images/receipt.jpg new file mode 100644 index 0000000..d06189a Binary files /dev/null and b/Scaner/images/receipt.jpg differ diff --git a/Scaner/login.py b/Scaner/login.py new file mode 100644 index 0000000..b3b9638 --- /dev/null +++ b/Scaner/login.py @@ -0,0 +1,150 @@ +from tkinter import * +import tkinter as tk +import tkinter.messagebox as messagebox +import subprocess +from tkinter import filedialog +import pymysql +from flask import Flask, render_template, request, redirect, url_for + +conn = pymysql.connect(host='localhost', + user='root', + password='123456', + database='scaner', + port=3306, + charset='utf8') + +# 使用 cursor() 方法创建一个游标对象 cursor +cursor = conn.cursor() +print("数据库连接成功!") + + +# 查询用户信息 +def userinfo_query(username, password): + cursor.execute("select * from `user`") + data = cursor.fetchall() + + username_list_length = len(data) + + for i in range(username_list_length): + username.append(data[i][0]) + password.append(data[i][1]) + + +def user_register(): + deng = Tk() + deng.title("注册") + deng.geometry("300x200+280+580") + + + + + # 在GUI中添加新的输入字段和按钮 + label_new_username = Label(deng, text="新账号:") + label_new_username.pack() + entry_new_username = Entry(deng) + entry_new_username.pack() + label_new_password = Label(deng, text="新密码:") + label_new_password.pack() + entry_new_password = Entry(deng, show="*") + entry_new_password.pack() + + btn_register = Button(deng, text="注册", command=lambda: register_action(deng, entry_new_username, entry_new_password)) + btn_register.pack(side='right', padx=50) + + +def register_action(deng, entry_new_username, entry_new_password): + cursor.execute("INSERT INTO user (`username`, `password`) VALUES (%s, %s)", + (entry_new_username.get(), entry_new_password.get())) + conn.commit() + deng.destroy() + messagebox.showinfo("注册结果", "注册成功!") + print("注册成功!") + + + +def user_login(): + global count + count = 2 + username_list = [] + password_list = [] + userinfo_query(username_list, password_list) + + username_input = entry_username.get() + password_input = entry_password.get() + + if username_input not in username_list: + messagebox.showerror("登录结果", "账号错误!") + label_message.config(text="账号错误!") + else: + if password_input in password_list: + messagebox.showinfo("登录结果", "登录成功!") + print("debug1") + + windows.destroy() + def choose_folde(): + print("debug2") + root.destroy() + root5 = tk.Tk() + root5.title('选择图片') + root5.geometry('300x300') + filename = filedialog.askopenfilename( + initialdir="/", # 设置对话框初始打开的目录 + title="选择图片", + filetypes=(("图片文件", "*.jpg;*.png;*.gif"), ("所有文件", "*.*")) # 指定文件类型过滤器 + ) + if filename: # 如果用户选择了文件 + # 使用subprocess运行另一个Python脚本,并将图片路径作为参数传递 + root5.destroy() + subprocess.run(["python", "scan.py", "-i", filename]) + exit() + + # 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() + print("debug3") + + else: + label_message.config(text="密码错误!还可以尝试{}次".format(3-count)) + count += 1 + if count == 3: + messagebox.showerror("登录结果", "登录失败!") + label_message.config(text="登录失败!") + btn_login.config(state='disabled') + + +windows = Tk() +windows.title("登录") +windows.geometry("300x200") +# windows.configure(bg='#3ffff3') +label_username = Label(windows, text="账号:") +label_username.pack() +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) +print("debug0") +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() diff --git a/Scaner/out.txt b/Scaner/out.txt new file mode 100644 index 0000000..084b78a --- /dev/null +++ b/Scaner/out.txt @@ -0,0 +1,86 @@ +we owe owe owe oke ome owk sh ok who o c %K + +WHOLE +FOODS +M __A R__K _E T} + +WHOLE FOODS MARKET - WESTPORT,.CT 06880 +399 POST RD WEST - (203) 227-6858 + +364 BACON LS + +365 BACON LS + +3565 BACON LS + +365 BACUN iS + +BRO TH CHIC + +FLOUR ALMUNL + +CHKN BRST BNLSS SK + +HEAVY CREAM + +BALSMC REDUCT + +BEEF - GRND - 85/15 + +JUICE COF CASHEW L + +DOCS PINT ORGAx IC + +HNY ALMOND Bul TiR +% % ## TAX . 00 BAL + +NP +NP +NP +NP +NP +NP +NP +NP +NP +NP +NP +NP +NP + +4 +4 +4 +4 + +11. + +~ n o + +14. + +« + +101 + +99 +. 99 +. 99 +29 +19 +99 +. 80 + +3.39 + +. 49 +04 +. 99 +49 +. 99 +. 33 + + + +m mom nom m ot ton n oto onmoy + diff --git a/Scaner/scan.jpg b/Scaner/scan.jpg new file mode 100644 index 0000000..1d577c3 Binary files /dev/null and b/Scaner/scan.jpg differ diff --git a/Scaner/scan.py b/Scaner/scan.py new file mode 100644 index 0000000..0f6c6f2 --- /dev/null +++ b/Scaner/scan.py @@ -0,0 +1,182 @@ +# 导入工具包 +import numpy as np +import argparse +import cv2 +import pytesseract +import os +from PIL import Image + +# def process_image(image_path): +# try: +# # 使用Pillow库打开并显示图片 +# with Image.open(image_path) as img: +# img.show() +# # 在这里添加更多处理图片的代码... +# except IOError: +# print(f"无法打开图片: {image_path}") + +def main(): + parser = argparse.ArgumentParser(description="处理图片的脚本") + parser.add_argument("-i", "--image", required=True, help="指定要处理的图片的路径") + args = parser.parse_args() + # process_image(args.image) + print("main") + +def order_points(pts): + # 一共4个坐标点 + rect = np.zeros((4, 2), dtype = "float32") + + # 按顺序找到对应坐标0123分别是 左上,右上,右下,左下 + # 计算左上,右下 + s = pts.sum(axis = 1) + rect[0] = pts[np.argmin(s)] + rect[2] = pts[np.argmax(s)] + + # 计算右上和左下 + diff = np.diff(pts, axis = 1) + rect[1] = pts[np.argmin(diff)] + rect[3] = pts[np.argmax(diff)] + + return rect + +def four_point_transform(image, pts): + # 获取输入坐标点 + rect = order_points(pts) + (tl, tr, br, bl) = rect + + # 计算输入的w和h值 + widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2)) + widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2)) + maxWidth = max(int(widthA), int(widthB)) + + heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2)) + heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2)) + maxHeight = max(int(heightA), int(heightB)) + + # 变换后对应坐标位置 + dst = np.array([ + [0, 0], + [maxWidth - 1, 0], + [maxWidth - 1, maxHeight - 1], + [0, maxHeight - 1]], dtype = "float32") + + # 计算变换矩阵 + M = cv2.getPerspectiveTransform(rect, dst) + warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)) + + # 返回变换后结果 + return warped + +def resize(image, width=None, height=None, inter=cv2.INTER_AREA): + dim = None + (h, w) = image.shape[:2] + if width is None and height is None: + return image + if width is None: + r = height / float(h) + dim = (int(w * r), height) + else: + r = width / float(w) + dim = (width, int(h * r)) + resized = cv2.resize(image, dim, interpolation=inter) + return resized + +# 设置参数 +ap = argparse.ArgumentParser() +ap.add_argument("-i", "--image", required = True, + help = "Path to the image to be scanned") +args = vars(ap.parse_args()) +print("arg") + +print(__name__) +if __name__ == "__main__": + main() + +# 读取输入 +image = cv2.imread(args["image"]) +#坐标也会相同变化 +ratio = image.shape[0] / 500.0 +orig = image.copy() +image = resize(orig, height = 500) + +# 预处理 +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 色彩空间转换 +gray = cv2.GaussianBlur(gray, (5, 5), 0) # 高斯模糊 +edged = cv2.Canny(gray, 75, 200) + +# 展示预处理结果 +print("STEP 1: 边缘检测") +cv2.imshow("Image", image) +cv2.imshow("Edged", edged) +cv2.waitKey(0) +cv2.destroyAllWindows() + +# 轮廓检测 +cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0] +cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5] # 降序排列+切片 + +# 遍历轮廓 +for c in cnts: + # 计算轮廓近似 + peri = cv2.arcLength(c, True) + # C表示输入的点集(数组) + # epsilon表示从原始轮廓到近似轮廓的最大距离,它是一个准确度参数 + # True表示封闭的 + approx = cv2.approxPolyDP(c, 0.10 * peri, True) # 近似 + + # 4个点的时候就拿出来 + if len(approx) == 4: + screenCnt = approx + break + +# 展示结果 + +print("STEP 2: 获取轮廓") +cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) +cv2.imshow("Outline", image) +cv2.waitKey(0) +cv2.destroyAllWindows() + +# 透视变换 +warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio) + +# 二值处理 +warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) +ref = cv2.threshold(warped, 100, 255, cv2.THRESH_BINARY)[1] +cv2.imwrite('scan.jpg', ref) + +# 展示结果 +print("STEP 3: 变换") +cv2.imshow("Original", resize(orig, height = 650)) +cv2.imshow("Scanned", resize(ref, height = 650)) +cv2.waitKey(0) +cv2.destroyAllWindows() + +# OCR扫描 +preprocess = "blur" + +if preprocess == "thresh": + gray = cv2.threshold(ref, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] +if preprocess == "blur": + gray = cv2.medianBlur(ref, 3) +cv2.imshow("Detect", gray) + +filename = "{}.png".format(os.getpid()) +cv2.imwrite(filename, gray) +text = pytesseract.image_to_string(Image.open(filename)) # 调用Tesseract OCR引擎对保存的图像进行文本识别 +os.remove(filename) + +encodings = ['utf-8', 'latin1', 'iso-8859-1', 'cp1252', 'gbk', 'big5'] +for encoding in encodings: + try: + with open("out.txt", 'w', encoding=encoding, errors="replace") as file: + file.write(text) + break + except UnicodeDecodeError: + continue +file.close() +print("text is written to out.txt") + +# Wait for pressing any key +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/Scaner/tesseract-ocr-setup-4.00.00dev.exe b/Scaner/tesseract-ocr-setup-4.00.00dev.exe new file mode 100644 index 0000000..359cdf0 Binary files /dev/null and b/Scaner/tesseract-ocr-setup-4.00.00dev.exe differ diff --git a/Scaner/test.py b/Scaner/test.py new file mode 100644 index 0000000..570ef67 --- /dev/null +++ b/Scaner/test.py @@ -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) diff --git a/login.py b/login.py index f9e121a..b3b9638 100644 --- a/login.py +++ b/login.py @@ -1,5 +1,6 @@ from tkinter import * import tkinter as tk +import tkinter.messagebox as messagebox import subprocess from tkinter import filedialog import pymysql @@ -56,6 +57,7 @@ def register_action(deng, entry_new_username, entry_new_password): (entry_new_username.get(), entry_new_password.get())) conn.commit() deng.destroy() + messagebox.showinfo("注册结果", "注册成功!") print("注册成功!") @@ -71,9 +73,11 @@ def user_login(): password_input = entry_password.get() if username_input not in username_list: + messagebox.showerror("登录结果", "账号错误!") label_message.config(text="账号错误!") else: if password_input in password_list: + messagebox.showinfo("登录结果", "登录成功!") print("debug1") windows.destroy() @@ -114,9 +118,10 @@ def user_login(): print("debug3") else: - label_message.config(text="密码错误!还可以尝试{}次".format(count)) + label_message.config(text="密码错误!还可以尝试{}次".format(3-count)) count += 1 if count == 3: + messagebox.showerror("登录结果", "登录失败!") label_message.config(text="登录失败!") btn_login.config(state='disabled')