You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pytesseract无法稳定识别清晰文本的问题求助(附代码)

Pytesseract识别Consolas字体文本不稳定的问题

使用Pytesseract识别Consolas字体的清晰文本时,无法实现持续准确识别。已尝试最近邻插值、双线性插值、高斯模糊等多种图像预处理方式,问题仍未解决。

具体识别异常情况:

  • 可正常识别:JZXN、IBG、DVLT
  • 完全无法识别:MMMM
  • 识别错误:JXG被识别为IXG

测试截图

测试截图1
测试截图2
测试截图3
测试截图4
测试截图5

相关代码

from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QUrl, QTimer
import sys
import mss
from PIL import Image
import pytesseract
from datetime import datetime

class CustomWebEnginePage(QWebEnginePage):
    def javaScriptConsoleMessage(self, level, message, lineNumber, sourceID):
        pass  # Suppresses output to terminal

class ScreenMonitorApp:
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.window = QMainWindow()
        self.window.setGeometry(100, 100, 1400, 800)
       
        central_widget = QWidget()
        layout = QHBoxLayout(central_widget)
      
        self.left_web = QWebEngineView()
        self.left_web.setPage(CustomWebEnginePage(self.left_web))
        self.right_web = QWebEngineView()
        self.right_web.setPage(CustomWebEnginePage(self.right_web))
      
        layout.addWidget(self.left_web, 1)
        layout.addWidget(self.right_web, 1)
      
        self.window.setCentralWidget(central_widget)
      
        self.previous_text = ""
      
        self.region = {"top": 80, "left": 80, "width": 78, "height": 30}
      
        self.timer = QTimer()
        self.timer.timeout.connect(self.check_region)
        self.timer.start(2000)
      
        screens = self.app.screens()
      
        monitor_index = 3
        if monitor_index < len(screens):
            screen = screens[monitor_index]
            geometry = screen.geometry()
            x = geometry.x() + (geometry.width() - self.window.width()) // 2
            y = geometry.y() + (geometry.height() - self.window.height()) // 2
            self.window.move(x, y)
        else:
            print("Monitor index out of range. Opening on the primary monitor.")
        self.window.show()
        sys.exit(self.app.exec_())
  
    def load_url(self, url_l, url_r):
        print("URLs loaded")
        self.left_web.setUrl(QUrl(f"https://example.com/"))
        self.right_web.setUrl(QUrl(f"https://example.com/"))
  
    def perform_ocr(self):
        """Capture screen region and perform OCR, saving the image for debug"""
        with mss.mss() as sct:
            img = sct.grab(self.region)
            pil_img = Image.frombytes("RGB", img.size, img.bgra, "raw", "BGRX")
            # Save the image with a timestamp
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            pil_img.save(f"ocr_capture_{timestamp}.png")
            text = pytesseract.image_to_string(pil_img).strip()
            return text
  
    def check_region(self):
        current_text = self.perform_ocr()
        if current_text != self.previous_text and current_text:
            self.previous_text = current_text
            new_url_l = current_text
            new_url_r = current_text
            self.load_url(new_url_l, new_url_r)
            print(f"Updated search for: {current_text}")

if __name__ == "__main__":
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    app = ScreenMonitorApp()

内容的提问来源于stack exchange,提问作者RvBVakama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:53:22