python编写ocr识别图片汉字

当你需要构建一个简单的图形用户界面(GUI)应用程序,并在其中实现光学字符识别(OCR)功能时,wxPython是一个强大而灵活的选择。wxPython是一个基于Python的跨平台GUI开发框架,结合了wxWidgets C++库和Python语言的优势。结合pytesseract和OpenCV等库,你可以轻松地创建一个具有OCR功能的应用程序。

在这篇博客中,我们将介绍一个使用wxPython构建的简单OCR应用程序示例。我们将使用wxPython创建一个框架,并在其中添加一个选择图像的按钮和一个用于显示识别文本的文本控件。当用户选择图像后,我们将使用pytesseract和OpenCV对图像进行处理和OCR,并将识别到的文本显示在应用程序中。

下载tesseract安装盘:

https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-5.3.1.20230401.exe

安装tesseract程序。

设置环境变量。

下载字库:

blob:https://github.com/cd52fa55-b81f-444c-8c3d-1075aabb15a9

安装pytesseract模块:

pip install pytesseract

测试:

tesseract C:\myimages\1.png result -l chi_sim

源代码:

import wx
import pytesseract
import cv2


class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(400, 300))

        panel = wx.Panel(self)
        self.text_ctrl = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        self.button = wx.Button(panel, label="Select Image")
        self.button.Bind(wx.EVT_BUTTON, self.on_select_image)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.text_ctrl, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
        sizer.Add(self.button, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
        panel.SetSizer(sizer)

    def on_select_image(self, event):
        wildcard = "JPEG files (*.jpg)|*.jpg|PNG files (*.png)|*.png"
        dialog = wx.FileDialog(self, "Select Image", wildcard=wildcard, style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

        if dialog.ShowModal() == wx.ID_CANCEL:
            return

        image_path = dialog.GetPath()
        dialog.Destroy()

        # Perform OCR on the selected image
        text = self.perform_ocr(image_path)

        # Display the recognized text in the text control
        self.text_ctrl.SetValue(text)

    def perform_ocr(self, image_path):
        # Load the image using OpenCV
        image = cv2.imread(image_path)

        # Preprocess the image (you may need to modify this based on your requirements)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        # Perform OCR using pytesseract
        text = pytesseract.image_to_string(gray)

        return text


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None, "OCR with wxPython")
    frame.Show()
    app.MainLoop()

代码说明:

结果如下:

python编写ocr识别图片汉字_第1张图片

你可能感兴趣的:(ocr,python)