📅  最后修改于: 2023-12-03 15:06:53.881000             🧑  作者: Mango
本项目是一个使用Python编写的名片扫描仪GUI应用程序,可以通过摄像头拍摄名片,识别名片信息并存储到本地或云端数据库中。该应用程序适用于需要快速录入名片信息的场景,如商务会议、招聘会等。
scanner.py
文件,即可看到程序的GUI界面拍照
按钮,使用摄像头拍摄名片照片识别
按钮,程序会自动识别名片信息,并在界面上显示保存
按钮,保存名片信息到本地或云端数据库中import cv2
import tkinter as tk
from PIL import Image, ImageTk
import pytesseract
import mysql.connector
# Capture video from camera
cap = cv2.VideoCapture(0)
# Load Tesseract OCR
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
# Connect to MySQL database
cnx = mysql.connector.connect(user='username', password='password',
host='127.0.0.1',
database='database_name')
# Initiate Tkinter GUI
root = tk.Tk()
# Create canvas to display video
canvas = tk.Canvas(root, width=640, height=480)
canvas.pack()
def capture():
"""Capture image from camera"""
ret, frame = cap.read()
cv2.imwrite("capture.jpg", frame)
cv2.imshow("image", frame)
def recognize():
"""Recognize text from image"""
image = cv2.imread("capture.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
text = pytesseract.image_to_string(gray)
text_box.config(text=text)
def save():
"""Save text to database"""
cursor = cnx.cursor()
query = "INSERT INTO cards (name, company, email, phone) VALUES (%s, %s, %s, %s)"
data = (name.get(), company.get(), email.get(), phone.get())
cursor.execute(query, data)
cnx.commit()
# Create buttons and textboxes
capture_button = tk.Button(root, text="Capture", command=capture)
recognize_button = tk.Button(root, text="Recognize", command=recognize)
save_button = tk.Button(root, text="Save", command=save)
name = tk.StringVar()
name_entry = tk.Entry(root, textvariable=name)
company = tk.StringVar()
company_entry = tk.Entry(root, textvariable=company)
email = tk.StringVar()
email_entry = tk.Entry(root, textvariable=email)
phone = tk.StringVar()
phone_entry = tk.Entry(root, textvariable=phone)
text_box = tk.Label(root, text="")
text_box.pack()
# Display buttons and textboxes
capture_button.pack()
recognize_button.pack()
save_button.pack()
name_entry.pack()
company_entry.pack()
email_entry.pack()
phone_entry.pack()
# Start main loop
root.mainloop()
以上是本项目的主要功能和代码片段,欢迎下载、使用、修改。