当前位置:   article > 正文

C# 中文文字识别OCR_emgucv 4.4.0.4099

emgucv 4.4.0.4099

目录

效果

项目

代码

说明

下载


效果

项目

代码

using Emgu.CV;
using Emgu.CV.CvEnum;
using OcrLiteLib;
using System;
using System.IO;
using System.Windows.Forms;

namespace OcrLiteOnnxCs
{
    public partial class FormOcrLite : Form
    {
        private OcrLite ocrEngin;

        public FormOcrLite()
        {
            InitializeComponent();
        }

        private void FormOcrLite_Load(object sender, EventArgs e)
        {
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            string appDir = Directory.GetParent(appPath).FullName;
            string modelsDir = appPath + "models";
            modelsTextBox.Text = modelsDir;
            ocrEngin = new OcrLite(modelsDir,(int) numThreadNumeric.Value);
        }

        private void initBtn_Click(object sender, EventArgs e)
        {
            ocrEngin = new OcrLite(modelsTextBox.Text, (int) numThreadNumeric.Value);
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                dlg.Multiselect = false;
                dlg.Filter = "(*.JPG,*.PNG,*.JPEG,*.BMP,*.GIF)|*.JPG;*.PNG;*.JPEG;*.BMP;*.GIF|All files(*.*)|*.*";
                if (dlg.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(dlg.FileName))
                {
                    pathTextBox.Text = dlg.FileName;
                    Mat src = CvInvoke.Imread(dlg.FileName, ImreadModes.Color);
                    pictureBox.Image = src.ToBitmap();
                }
            }
        }

        private void modelsBtn_Click(object sender, EventArgs e)
        {
            using (var dlg = new FolderBrowserDialog())
            {
                dlg.SelectedPath = Environment.CurrentDirectory + "\\models";
                if (dlg.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(dlg.SelectedPath))
                {
                    modelsTextBox.Text = dlg.SelectedPath;
                }
            }
        }

        private void detectBtn_Click(object sender, EventArgs e)
        {
            int padding = (int)paddingNumeric.Value;
            int imgResize = (int)imgResizeNumeric.Value;
            float boxScoreThresh = (float)boxScoreThreshNumeric.Value;
            float boxThresh = (float)boxThreshNumeric.Value;
            float minArea = (float)minAreaNumeric.Value;
            float unClipRatio = (float)unClipRatioNumeric.Value;
            bool doAngle = doAngleCheckBox.Checked;
            bool mostAngle = mostAngleCheckBox.Checked;
            OcrResult ocrResult = ocrEngin.Detect(pathTextBox.Text, padding, imgResize, boxScoreThresh, boxThresh, minArea, unClipRatio, doAngle, mostAngle);
            ocrResultTextBox.Text = ocrResult.ToString();
            strRestTextBox.Text = ocrResult.StrRes;
            pictureBox.Image = ocrResult.BoxImg.ToBitmap();

        }

        private void partImgCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            ocrEngin.isPartImg = partImgCheckBox.Checked;
        }

        private void debugCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            ocrEngin.isDebugImg = debugCheckBox.Checked;
        }
    }
}
 

  1. using Emgu.CV;
  2. using Emgu.CV.CvEnum;
  3. using OcrLiteLib;
  4. using System;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. namespace OcrLiteOnnxCs
  8. {
  9. public partial class FormOcrLite : Form
  10. {
  11. private OcrLite ocrEngin;
  12. public FormOcrLite()
  13. {
  14. InitializeComponent();
  15. }
  16. private void FormOcrLite_Load(object sender, EventArgs e)
  17. {
  18. string appPath = AppDomain.CurrentDomain.BaseDirectory;
  19. string appDir = Directory.GetParent(appPath).FullName;
  20. string modelsDir = appPath + "models";
  21. modelsTextBox.Text = modelsDir;
  22. ocrEngin = new OcrLite(modelsDir,(int) numThreadNumeric.Value);
  23. }
  24. private void initBtn_Click(object sender, EventArgs e)
  25. {
  26. ocrEngin = new OcrLite(modelsTextBox.Text, (int) numThreadNumeric.Value);
  27. }
  28. private void openBtn_Click(object sender, EventArgs e)
  29. {
  30. using (var dlg = new OpenFileDialog())
  31. {
  32. dlg.Multiselect = false;
  33. dlg.Filter = "(*.JPG,*.PNG,*.JPEG,*.BMP,*.GIF)|*.JPG;*.PNG;*.JPEG;*.BMP;*.GIF|All files(*.*)|*.*";
  34. if (dlg.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(dlg.FileName))
  35. {
  36. pathTextBox.Text = dlg.FileName;
  37. Mat src = CvInvoke.Imread(dlg.FileName, ImreadModes.Color);
  38. pictureBox.Image = src.ToBitmap();
  39. }
  40. }
  41. }
  42. private void modelsBtn_Click(object sender, EventArgs e)
  43. {
  44. using (var dlg = new FolderBrowserDialog())
  45. {
  46. dlg.SelectedPath = Environment.CurrentDirectory + "\\models";
  47. if (dlg.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(dlg.SelectedPath))
  48. {
  49. modelsTextBox.Text = dlg.SelectedPath;
  50. }
  51. }
  52. }
  53. private void detectBtn_Click(object sender, EventArgs e)
  54. {
  55. int padding = (int)paddingNumeric.Value;
  56. int imgResize = (int)imgResizeNumeric.Value;
  57. float boxScoreThresh = (float)boxScoreThreshNumeric.Value;
  58. float boxThresh = (float)boxThreshNumeric.Value;
  59. float minArea = (float)minAreaNumeric.Value;
  60. float unClipRatio = (float)unClipRatioNumeric.Value;
  61. bool doAngle = doAngleCheckBox.Checked;
  62. bool mostAngle = mostAngleCheckBox.Checked;
  63. OcrResult ocrResult = ocrEngin.Detect(pathTextBox.Text, padding, imgResize, boxScoreThresh, boxThresh, minArea, unClipRatio, doAngle, mostAngle);
  64. ocrResultTextBox.Text = ocrResult.ToString();
  65. strRestTextBox.Text = ocrResult.StrRes;
  66. pictureBox.Image = ocrResult.BoxImg.ToBitmap();
  67. }
  68. private void partImgCheckBox_CheckedChanged(object sender, EventArgs e)
  69. {
  70. ocrEngin.isPartImg = partImgCheckBox.Checked;
  71. }
  72. private void debugCheckBox_CheckedChanged(object sender, EventArgs e)
  73. {
  74. ocrEngin.isDebugImg = debugCheckBox.Checked;
  75. }
  76. }
  77. }

说明

1. Vs2017打开OcrLiteOnnxCs.sln。
2. 解决方案资源管理器->OcrLiteLib->右键->管理NuGet程序包->浏览->搜索->安装
* 注意:Emgu.CV要选作者是“Emgu Corporation”
* Emgu.CV 4.4.0.4099
* Emgu.CV.runtime.windows 4.4.0.4099
* MicroSoft.ML.OnnxRuntime 1.5.2
* clipper_library 6.2.1
3. 解决方案资源管理器->OcrLiteOnnxForm->右键->管理NuGet程序包->浏览->搜索->安装
* 注意:Emgu.CV要选作者是“Emgu Corporation”
* Emgu.CV 4.4.0.4099
* Emgu.CV.Bitmap 4.4.0.4099
4. 确保:OcrLiteOnnxForm设为启动项目
5. 确保:OcrLiteOnnxForm->右键->属性->生成->平台目标:x64
6. 确保:OcrLiteLib->右键->属性->生成->平台目标:x64
7. 生成解决方案 

基于开源项目chineseocr_lite

下载

完整Demo下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/368753
推荐阅读
相关标签
  

闽ICP备14008679号