多功能OCR系统:PyQt5、百度AI与SQLite的深度整合

一、系统架构概述

本系统以PyQt5为核心构建图形用户界面(GUI),通过集成主流云服务商的文字识别API实现多场景OCR功能,并采用SQLite轻量级数据库完成识别结果的持久化存储。系统架构分为三层:

  • 表现层:PyQt5负责界面交互与结果展示,支持拖拽上传、实时预览及历史记录查询。
  • 服务层:封装百度AI文字识别API调用逻辑,支持通用文字、身份证、银行卡、驾驶证四类识别模式。
  • 数据层:SQLite数据库存储识别记录,包含时间戳、文件路径、识别结果及分类标签。

该设计兼顾开发效率与运行性能,PyQt5的跨平台特性与SQLite的无服务器配置使其适用于个人电脑及轻量级服务器环境。

二、PyQt5图形界面实现

1. 主窗口设计

主窗口采用QMainWindow框架,包含以下核心组件:

  • 菜单栏:文件操作(打开、保存)、识别模式切换、历史记录管理。
  • 工具栏:快速调用识别功能按钮。
  • 中央区域:左侧为文件上传区(支持拖拽),右侧为识别结果展示区(QTextEditQTableWidget)。
  • 状态栏:显示当前操作状态及识别进度。
  1. from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QTextEdit, QFileDialog
  2. class MainWindow(QMainWindow):
  3. def __init__(self):
  4. super().__init__()
  5. self.initUI()
  6. def initUI(self):
  7. self.setWindowTitle("多功能OCR识别系统")
  8. self.setGeometry(100, 100, 800, 600)
  9. # 中央部件布局
  10. central_widget = QWidget()
  11. main_layout = QHBoxLayout()
  12. # 左侧文件上传区
  13. left_layout = QVBoxLayout()
  14. self.file_label = QLabel("拖拽文件至此或点击选择")
  15. self.upload_btn = QPushButton("选择文件")
  16. self.upload_btn.clicked.connect(self.open_file_dialog)
  17. left_layout.addWidget(self.file_label)
  18. left_layout.addWidget(self.upload_btn)
  19. # 右侧结果展示区
  20. right_layout = QVBoxLayout()
  21. self.result_text = QTextEdit()
  22. self.result_text.setReadOnly(True)
  23. right_layout.addWidget(QLabel("识别结果:"))
  24. right_layout.addWidget(self.result_text)
  25. main_layout.addLayout(left_layout, 1)
  26. main_layout.addLayout(right_layout, 2)
  27. central_widget.setLayout(main_layout)
  28. self.setCentralWidget(central_widget)
  29. def open_file_dialog(self):
  30. file_path, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.jpg *.bmp)")
  31. if file_path:
  32. self.file_label.setText(f"已选择: {file_path}")
  33. # 调用识别逻辑
  34. self.recognize_text(file_path)
  35. def recognize_text(self, file_path):
  36. # 此处调用百度AI API,后续章节详述
  37. pass

2. 拖拽功能实现

通过重写dragEnterEventdropEvent方法支持文件拖拽上传:

  1. from PyQt5.QtCore import Qt, QMimeData
  2. class DropArea(QLabel):
  3. def __init__(self, parent):
  4. super().__init__("拖拽文件至此", parent)
  5. self.setAlignment(Qt.AlignCenter)
  6. self.setStyleSheet("""
  7. QLabel {
  8. border: 2px dashed #aaa;
  9. padding: 20px;
  10. }
  11. """)
  12. self.setAcceptDrops(True)
  13. def dragEnterEvent(self, event):
  14. if event.mimeData().hasUrls():
  15. event.acceptProposedAction()
  16. def dropEvent(self, event):
  17. for url in event.mimeData().urls():
  18. file_path = url.toLocalFile()
  19. if file_path.lower().endswith(('.png', '.jpg', '.bmp')):
  20. self.setText(f"已选择: {file_path}")
  21. # 触发识别逻辑
  22. parent = self.parent()
  23. if hasattr(parent, 'recognize_text'):
  24. parent.recognize_text(file_path)
  25. break

三、百度AI文字识别API集成

1. API调用封装

系统封装了百度AI文字识别的四种模式,通过统一接口处理请求与响应:

  1. import requests
  2. import base64
  3. import json
  4. class BaiduOCR:
  5. def __init__(self, api_key, secret_key):
  6. self.api_key = api_key
  7. self.secret_key = secret_key
  8. self.access_token = self._get_access_token()
  9. def _get_access_token(self):
  10. auth_url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={self.api_key}&client_secret={self.secret_key}"
  11. response = requests.get(auth_url)
  12. return response.json().get("access_token")
  13. def recognize(self, image_path, recognition_type="general"):
  14. with open(image_path, "rb") as f:
  15. image_base64 = base64.b64encode(f.read()).decode("utf-8")
  16. url_map = {
  17. "general": "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic",
  18. "id_card": "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard",
  19. "bank_card": "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard",
  20. "driving_license": "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license"
  21. }
  22. url = url_map.get(recognition_type)
  23. if not url:
  24. raise ValueError("不支持的识别类型")
  25. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  26. params = {"access_token": self.access_token}
  27. data = {"image": image_base64, "recognize_granularity": "big"}
  28. response = requests.post(url, params=params, headers=headers, data=data)
  29. return response.json()

2. 多模式识别实现

  • 通用文字识别:适用于普通文档、截图等场景。
  • 身份证识别:自动区分正面(人像面)与反面(国徽面),提取姓名、身份证号等信息。
  • 银行卡识别:返回卡号、有效期、银行名称。
  • 驾驶证识别:解析证号、姓名、准驾车型、有效期等字段。

调用示例:

  1. ocr = BaiduOCR("your_api_key", "your_secret_key")
  2. result = ocr.recognize("id_card.jpg", "id_card")
  3. print(json.dumps(result, indent=2))

四、SQLite数据持久化

1. 数据库设计

创建records表存储识别历史:

  1. CREATE TABLE IF NOT EXISTS records (
  2. id INTEGER PRIMARY KEY AUTOINCREMENT,
  3. timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
  4. file_path TEXT NOT NULL,
  5. recognition_type TEXT NOT NULL,
  6. result TEXT NOT NULL,
  7. accuracy REAL
  8. );

2. Python操作示例

使用sqlite3模块实现数据存取:

  1. import sqlite3
  2. from datetime import datetime
  3. class OCRDatabase:
  4. def __init__(self, db_path="ocr_records.db"):
  5. self.conn = sqlite3.connect(db_path)
  6. self._init_db()
  7. def _init_db(self):
  8. cursor = self.conn.cursor()
  9. cursor.execute("""
  10. CREATE TABLE IF NOT EXISTS records (
  11. id INTEGER PRIMARY KEY AUTOINCREMENT,
  12. timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
  13. file_path TEXT NOT NULL,
  14. recognition_type TEXT NOT NULL,
  15. result TEXT NOT NULL,
  16. accuracy REAL
  17. );
  18. """)
  19. self.conn.commit()
  20. def add_record(self, file_path, recognition_type, result, accuracy=None):
  21. cursor = self.conn.cursor()
  22. cursor.execute("""
  23. INSERT INTO records (file_path, recognition_type, result, accuracy)
  24. VALUES (?, ?, ?, ?)
  25. """, (file_path, recognition_type, result, accuracy))
  26. self.conn.commit()
  27. def get_records(self, recognition_type=None, limit=10):
  28. cursor = self.conn.cursor()
  29. if recognition_type:
  30. cursor.execute("SELECT * FROM records WHERE recognition_type=? ORDER BY timestamp DESC LIMIT ?",
  31. (recognition_type, limit))
  32. else:
  33. cursor.execute("SELECT * FROM records ORDER BY timestamp DESC LIMIT ?", (limit,))
  34. return cursor.fetchall()

五、系统优化与扩展建议

  1. 性能优化

    • 对大图片进行压缩或分块处理,减少API调用耗时。
    • 使用多线程分离UI线程与识别线程,避免界面卡顿。
  2. 功能扩展

    • 增加批量识别功能,支持多文件同时处理。
    • 添加识别结果导出为Excel或PDF的选项。
    • 实现自动分类存储,按识别类型建立子目录。
  3. 错误处理

    • 捕获API调用异常(如网络错误、配额超限)。
    • 验证图片格式与大小,提前过滤无效文件。

六、总结

本系统通过整合PyQt5、百度AI文字识别API与SQLite数据库,实现了集多场景识别与数据管理于一体的OCR工具。其优势在于:

  • 用户友好:直观的GUI与拖拽操作降低使用门槛。
  • 功能全面:覆盖主流证件与文档识别需求。
  • 轻量高效:无需部署复杂服务,适合个人及小型团队使用。

开发者可基于此框架进一步扩展功能,如增加更多识别类型或集成其他云服务API,构建更强大的文档处理平台。