欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Ocr 小工具(文本识别)

最编程 2024-04-08 16:10:46
...

前言

之前看到同事使用了一款叫做天若的Ocr文本识别小工具,觉得很不错,但是免费版有使用次数,虽可自定义供应商,但归根结底还是需要自己去购买相应的Ocr服务(修正下,了解到天若Ocr有离线版本的,我没找到相关资源就不放了),想起自己当初给前东家做Ocr服务时了解过一款开源的,可本地化的超轻量级中文OCR(可训练) PaddleOCR-Gitee, 然后结合自己的熟悉的C#语言用Winform写了款简洁版的离线版Ocr文本识别小工具Demo。虽说没有天若那么专业,但是基本也够用了,提供下源码有兴趣的小伙伴可自行拿去优化。

预览展示

image.png

image.png

关键代码

第一步引入PaddleOCR的.NET里的 NutGet工具类库PaddleOCRSharp:

使用示例

OpenFileDialog ofd = new OpenFileDialog();
  ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  if (ofd.ShowDialog() != DialogResult.OK) return;
//使用默认中英文V3模型
  OCRModelConfig config = null;
//使用默认参数
  OCRParameter oCRParameter = new  OCRParameter ();
//识别结果对象
  OCRResult ocrResult = new OCRResult();
  //建议程序全局初始化一次即可,不必每次识别都初始化,容易报错。     
  PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);
   {
    ocrResult = engine.DetectText(ofd.FileName );
   }
 if (ocrResult != null) MessageBox.Show(ocrResult.Text,"识别结果"); 

第二步写好截图代码,我这里是用了一个新窗体保存当前屏幕截图,然后进行裁剪,再传递给上述方法进行识别

public partial class ScreenMain : Form
{ 

    int x, y, nowX, nowY, width, height;
    bool isMouthDown = false;
    bool Ocring = false;
    Graphics g;

    public ScreenMain()
    {
        InitializeComponent();
    }

    private void Form2_MouseDown(object sender, MouseEventArgs e)
    {
        x = MousePosition.X;
        y = MousePosition.Y;
        isMouthDown = true;
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMouthDown && !Ocring)
        {
            width = Math.Abs(MousePosition.X - x);
            height = Math.Abs(MousePosition.Y - y);
            g = CreateGraphics();
            g.Clear(BackColor);
            g.FillRectangle(Brushes.CornflowerBlue, x < MousePosition.X ? x : MousePosition.X, y < MousePosition.Y ? y : MousePosition.Y, width + 1, height + 1);
        }
    }

    private void Form2_MouseUp(object sender, MouseEventArgs e)
    {
        nowX = MousePosition.X + 1;
        nowY = MousePosition.Y + 1;
        this.Close();
        //formMain.pcurrentWin.sendEndMes += tellEnd;
        // 开启线程处理数据
        Task t = new(() =>
        {
            Ocring = true;
            formMain.pcurrentWin.Snap(x < nowX ? x : nowX, y < nowY ? y : nowY, Math.Abs(nowX - x), Math.Abs(nowY - y));
        }); 
        t.Start();
        formMain.pcurrentWin.Show();
    }

    /// <summary>
    /// 通知load数据结束的方法
    /// 此方法仍为子线程中的方法,因为被子线程中的委托调用
    /// </summary>
    /// <param name="mes"></param>
    private void tellEnd(string mes)
    {
        Ocring = false;
        // 处理数据完成后的提示信息
        formMain.pcurrentWin.Show(); 
    } 
}

最后就是主程序的一个识别展示处理,没啥特别之处

public void Snap(int x, int y, int width, int height)
{ 
    try
    { 
        image = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(image);
        g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));
        var ocrResult = engine.DetectText(image);

        pictureBox2.Image = image;
        if (ocrResult != null)
        {
            textBox1.Text = string.Join("\r\n",ocrResult.TextBlocks.Select(o=>o.Text));
        }
    }
    catch (Exception e)
    {
        MessageBox.Show("识别出错:" + e.Message);
    }
    pictureBox1.Hide();
    //sendEndMes("处理结束");
}

然后就完成了我们的小工具了

提取码: jmkc
Exe文件: pan.baidu.com/s/1TcChilVH…
(Win64需要.Net6运行时)
Exe文件: pan.baidu.com/s/1UnrgnllP…
(自带.Net6运行时,文件比较多)

码云地址: gitee.com/huangguishe…

相关资料

PaddleOCR-Gitee
PaddleSharp
WinForm
WPF