📜  使用Python的名片扫描仪 GUI 应用程序(1)

📅  最后修改于: 2023-12-03 15:06:53.881000             🧑  作者: Mango

使用Python的名片扫描仪 GUI 应用程序

介绍

本项目是一个使用Python编写的名片扫描仪GUI应用程序,可以通过摄像头拍摄名片,识别名片信息并存储到本地或云端数据库中。该应用程序适用于需要快速录入名片信息的场景,如商务会议、招聘会等。

功能
  • 拍摄名片并识别名片信息
  • 将识别结果保存到本地或云端数据库中
技术栈
  • Python
  • OpenCV
  • Tesseract OCR
  • Tkinter GUI
  • SQLite/MySQL
安装
  1. 安装Python3
  2. 安装以下Python库:OpenCV, Pillow, pytesseract, mysql-connector-python
  3. 安装Tesseract OCR
  4. 配置MySQL数据库(仅在使用MySQL时需要)
使用
  1. 启动scanner.py文件,即可看到程序的GUI界面
  2. 点击拍照按钮,使用摄像头拍摄名片照片
  3. 点击识别按钮,程序会自动识别名片信息,并在界面上显示
  4. 点击保存按钮,保存名片信息到本地或云端数据库中
代码片段
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()

以上是本项目的主要功能和代码片段,欢迎下载、使用、修改。