首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# PaddleOCR 单字识别效果

  • 25-02-19 03:20
  • 4175
  • 6285
blog.csdn.net

C# PaddleOCR  单字识别效果

效果

有问题和需求可以私信博主

说明

        根据《百度办公文档识别C++离线SDKV1.2用户接入文档.pdf》,使用C++封装DLL,C#调用。

背景

        为使客户、第三方开发者等能够更快速、方便的接入使用百度办公文档识别 SDK、促进百度 OCR产品赋能更多客户,特设计支持 c++语言的 Windows 高精通用文字识别 SDK,该 SDK 提供 pdf 转图文的能力和通过 pdf 识别文字并可以转存成 word 的能力。

SDK 简介

        本 SDK 适应于 Windows 平台下的人脸识别系统, ,开发者可在 vs2015 下⾯进⾏开发(推荐使⽤,不保证其他版本 vs 都兼容)。SDK 采⽤ c++的动态库 dll 的⽅式。上层 UI 框架支持主流框架如QT,MFC 等。

自动批量授权

        鉴权采用自动激活的方式进行授权,可参考 SDK 示例中,把申请到的授权 key 串码(仅支持批量授权)填入到 license 文件夹的 license.key 文件中,运行 SDK,即可自动激活生成授权文件 license.ini 在license 文件夹中。SDK 授权是通过接口方法 auth_from_file 实现,该方法参数分别是传入授权 key 的串码和授权文件 license.ini 的绝对路径。确保参数正确后,在 SDK 中运行了该方法,就会生成授权license.ini 文件。若授权正确,该方法的返回值为 0,若非 0,则为授权失败,错误原因可根据错误码参考后续文档查看。

离线授权

        离线授权,采用从 sdk 附带的 license_tool 工具,bin 文件夹的 license_tool 下,双击 LicenseTool.exe,再点击拷贝,把设备指纹拷贝到剪贴板中,到百度 OCR 官网进行离线激活,填入得到的设备指纹后,从官网下载离线授权文件,解压,形成 license.key 和 license.ini 两个文件,替换到 SDK 中的 license 文件夹中,运行 SDK,若在 SDK 的授权方法 auth_from_file 中返回 0,则为通过了授权。(具体可参考SDK 中的授权代码示例)

项目

代码

using HightOCRTest.Common;
using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace HightOCRTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        bool isDraw = false;
        static IntPtr engine;

        private void button6_Click(object sender, EventArgs e)
        {
            //授权校验 初始化引擎
            string key = "";
            string licenseKeyPath = Application.StartupPath + "\\license\\license.key";
            string licenseFile = Application.StartupPath + "\\license\\license.ini";
            int res = -1;
            string ini_path = "";

            key = File.ReadAllText(licenseKeyPath);

            res = Native.init_license(key, licenseFile);
            if (res != 0)
            {
                MessageBox.Show(res.ToString());
                return;
            }

            engine = Native.create();
            if (engine == null)
            {
                MessageBox.Show("创建引擎失败!");
                return;
            }

            ini_path = Application.StartupPath + "\\resource";
            res = Native.init(engine, "", 6);
            if (res != 0)
            {
                MessageBox.Show(res.ToString());
                return;
            }

            MessageBox.Show("初始化成功!");

            button1.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            

            button6.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //image_path = Application.StartupPath + "\\images\\1.jpg";
            image_path = Application.StartupPath + "\\test2.jpg";
            pictureBox1.Image = new Bitmap(image_path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
        }

        StringBuilder ocr_result_texts = new StringBuilder(1024 * 10);
        StringBuilder ocr_result_words = new StringBuilder(1024 * 100);

        private void button1_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            textBox1.Text = "";
            Application.DoEvents();

            ocr_result_texts.Clear();
            ocr_result_words.Clear();
            Mat image = new Mat(image_path);
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            int res = Native.ocr(engine, image.CvPtr, ocr_result_texts, ocr_result_words);

            stopwatch.Stop();
            double totalTime = stopwatch.Elapsed.TotalSeconds;
            textBox1.Text += $"耗时: {totalTime:F2}s";
            textBox1.Text += "\r\n-------------------\r\n";

            if (res == 0)
            {
                textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented);
                textBox1.Text += "\r\n-------------------\r\n";
                textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented);
            }
            else
            {

                textBox1.Text = "识别失败";
            }
        }

        //绘制文字区域
        private void button3_Click(object sender, EventArgs e)
        {
            if (ocr_result_texts.Length == 0)
            {
                return;
            }

            Mat image = new Mat(image_path);
            List lt = JsonConvert.DeserializeObject>(ocr_result_texts.ToString());

            foreach (OcrResTexts item in lt)
            {
                string[] pts = item.coordinator.Split(' ');

                //多边形的顶点
                OpenCvSharp.Point[] points = new OpenCvSharp.Point[]
                {
                        new OpenCvSharp.Point(Convert.ToDouble( pts[0]), Convert.ToDouble( pts[1])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[2]), Convert.ToDouble( pts[3])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[4]), Convert.ToDouble( pts[5])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[6]), Convert.ToDouble( pts[7])),
                };

                // 绘制多边形
                Cv2.Polylines(image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 2);
            }

            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }

            pictureBox1.Image = new Bitmap(image.ToMemoryStream());
            image.Dispose();

        }

        //绘制单字区域
        private void button4_Click(object sender, EventArgs e)
        {
            if (ocr_result_words.Length == 0)
            {
                return;
            }

            Mat image = new Mat(image_path);
            List lt = JsonConvert.DeserializeObject>(ocr_result_words.ToString());

            foreach (OcrResWords item in lt)
            {
                string[] pts = item.coordinator.Split(' ');

                //left top width height

                OpenCvSharp.Rect rect = new Rect((int)Convert.ToDouble(pts[0]), (int)Convert.ToDouble(pts[1]), (int)Convert.ToDouble(pts[2]), (int)Convert.ToDouble(pts[3]));

                Cv2.Rectangle(image, rect, color: new Scalar(255, 0, 0), thickness: 1);

            }
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }

            pictureBox1.Image = new Bitmap(image.ToMemoryStream());
            image.Dispose();

        }

        //识别小语种→
        private void button5_Click(object sender, EventArgs e)
        {
            //if (image_path == "")
            //{
            //    return;
            //}

            //textBox1.Text = "";
            //Application.DoEvents();

            //ocr_result_texts.Clear();
            //ocr_result_words.Clear();
            //Mat image = new Mat(image_path);
            //Stopwatch stopwatch = new Stopwatch();
            //stopwatch.Start();
            //int res = Native.ocr_other(engine, image.CvPtr, ocr_result_texts, ocr_result_words);

            //stopwatch.Stop();
            //double totalTime = stopwatch.Elapsed.TotalSeconds;
            //textBox1.Text += $"耗时: {totalTime:F2}s";
            //textBox1.Text += "\r\n-------------------\r\n";

            //if (res == 0)
            //{
            //    textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented);
            //    textBox1.Text += "\r\n-------------------\r\n";
            //    textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented);
            //}
            //else
            //{

            //    textBox1.Text = "识别失败";
            //}
        }
    }
}

  1. using HightOCRTest.Common;
  2. using Newtonsoft.Json;
  3. using OpenCvSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace HightOCRTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  20. string image_path = "";
  21. bool isDraw = false;
  22. static IntPtr engine;
  23. private void button6_Click(object sender, EventArgs e)
  24. {
  25. //授权校验 初始化引擎
  26. string key = "";
  27. string licenseKeyPath = Application.StartupPath + "\\license\\license.key";
  28. string licenseFile = Application.StartupPath + "\\license\\license.ini";
  29. int res = -1;
  30. string ini_path = "";
  31. key = File.ReadAllText(licenseKeyPath);
  32. res = Native.init_license(key, licenseFile);
  33. if (res != 0)
  34. {
  35. MessageBox.Show(res.ToString());
  36. return;
  37. }
  38. engine = Native.create();
  39. if (engine == null)
  40. {
  41. MessageBox.Show("创建引擎失败!");
  42. return;
  43. }
  44. ini_path = Application.StartupPath + "\\resource";
  45. res = Native.init(engine, "", 6);
  46. if (res != 0)
  47. {
  48. MessageBox.Show(res.ToString());
  49. return;
  50. }
  51. MessageBox.Show("初始化成功!");
  52. button1.Enabled = true;
  53. button3.Enabled = true;
  54. button4.Enabled = true;
  55. button6.Enabled = false;
  56. }
  57. private void Form1_Load(object sender, EventArgs e)
  58. {
  59. //image_path = Application.StartupPath + "\\images\\1.jpg";
  60. image_path = Application.StartupPath + "\\test2.jpg";
  61. pictureBox1.Image = new Bitmap(image_path);
  62. }
  63. private void button2_Click(object sender, EventArgs e)
  64. {
  65. OpenFileDialog ofd = new OpenFileDialog();
  66. ofd.Filter = fileFilter;
  67. if (ofd.ShowDialog() != DialogResult.OK) return;
  68. pictureBox1.Image = null;
  69. image_path = ofd.FileName;
  70. pictureBox1.Image = new Bitmap(image_path);
  71. textBox1.Text = "";
  72. }
  73. StringBuilder ocr_result_texts = new StringBuilder(1024 * 10);
  74. StringBuilder ocr_result_words = new StringBuilder(1024 * 100);
  75. private void button1_Click(object sender, EventArgs e)
  76. {
  77. if (image_path == "")
  78. {
  79. return;
  80. }
  81. textBox1.Text = "";
  82. Application.DoEvents();
  83. ocr_result_texts.Clear();
  84. ocr_result_words.Clear();
  85. Mat image = new Mat(image_path);
  86. Stopwatch stopwatch = new Stopwatch();
  87. stopwatch.Start();
  88. int res = Native.ocr(engine, image.CvPtr, ocr_result_texts, ocr_result_words);
  89. stopwatch.Stop();
  90. double totalTime = stopwatch.Elapsed.TotalSeconds;
  91. textBox1.Text += $"耗时: {totalTime:F2}s";
  92. textBox1.Text += "\r\n-------------------\r\n";
  93. if (res == 0)
  94. {
  95. textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented);
  96. textBox1.Text += "\r\n-------------------\r\n";
  97. textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented);
  98. }
  99. else
  100. {
  101. textBox1.Text = "识别失败";
  102. }
  103. }
  104. //绘制文字区域
  105. private void button3_Click(object sender, EventArgs e)
  106. {
  107. if (ocr_result_texts.Length == 0)
  108. {
  109. return;
  110. }
  111. Mat image = new Mat(image_path);
  112. List<OcrResTexts> lt = JsonConvert.DeserializeObject<List<OcrResTexts>>(ocr_result_texts.ToString());
  113. foreach (OcrResTexts item in lt)
  114. {
  115. string[] pts = item.coordinator.Split(' ');
  116. //多边形的顶点
  117. OpenCvSharp.Point[] points = new OpenCvSharp.Point[]
  118. {
  119. new OpenCvSharp.Point(Convert.ToDouble( pts[0]), Convert.ToDouble( pts[1])),
  120. new OpenCvSharp.Point(Convert.ToDouble( pts[2]), Convert.ToDouble( pts[3])),
  121. new OpenCvSharp.Point(Convert.ToDouble( pts[4]), Convert.ToDouble( pts[5])),
  122. new OpenCvSharp.Point(Convert.ToDouble( pts[6]), Convert.ToDouble( pts[7])),
  123. };
  124. // 绘制多边形
  125. Cv2.Polylines(image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 2);
  126. }
  127. if (pictureBox1.Image != null)
  128. {
  129. pictureBox1.Image.Dispose();
  130. pictureBox1.Image = null;
  131. }
  132. pictureBox1.Image = new Bitmap(image.ToMemoryStream());
  133. image.Dispose();
  134. }
  135. //绘制单字区域
  136. private void button4_Click(object sender, EventArgs e)
  137. {
  138. if (ocr_result_words.Length == 0)
  139. {
  140. return;
  141. }
  142. Mat image = new Mat(image_path);
  143. List<OcrResWords> lt = JsonConvert.DeserializeObject<List<OcrResWords>>(ocr_result_words.ToString());
  144. foreach (OcrResWords item in lt)
  145. {
  146. string[] pts = item.coordinator.Split(' ');
  147. //left top width height
  148. OpenCvSharp.Rect rect = new Rect((int)Convert.ToDouble(pts[0]), (int)Convert.ToDouble(pts[1]), (int)Convert.ToDouble(pts[2]), (int)Convert.ToDouble(pts[3]));
  149. Cv2.Rectangle(image, rect, color: new Scalar(255, 0, 0), thickness: 1);
  150. }
  151. if (pictureBox1.Image != null)
  152. {
  153. pictureBox1.Image.Dispose();
  154. pictureBox1.Image = null;
  155. }
  156. pictureBox1.Image = new Bitmap(image.ToMemoryStream());
  157. image.Dispose();
  158. }
  159. //识别小语种→
  160. private void button5_Click(object sender, EventArgs e)
  161. {
  162. //if (image_path == "")
  163. //{
  164. // return;
  165. //}
  166. //textBox1.Text = "";
  167. //Application.DoEvents();
  168. //ocr_result_texts.Clear();
  169. //ocr_result_words.Clear();
  170. //Mat image = new Mat(image_path);
  171. //Stopwatch stopwatch = new Stopwatch();
  172. //stopwatch.Start();
  173. //int res = Native.ocr_other(engine, image.CvPtr, ocr_result_texts, ocr_result_words);
  174. //stopwatch.Stop();
  175. //double totalTime = stopwatch.Elapsed.TotalSeconds;
  176. //textBox1.Text += $"耗时: {totalTime:F2}s";
  177. //textBox1.Text += "\r\n-------------------\r\n";
  178. //if (res == 0)
  179. //{
  180. // textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented);
  181. // textBox1.Text += "\r\n-------------------\r\n";
  182. // textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented);
  183. //}
  184. //else
  185. //{
  186. // textBox1.Text = "识别失败";
  187. //}
  188. }
  189. }
  190. }

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/139313333"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top