当前位置:   article > 正文

C# PaddleOCR ch_PP-OCRv3 ch_PP-OCRv4测试

pp-ocrv4

目录

效果

未开启Mkldnn,V3

未开启Mkldnn,V4​

开启Mkldnn,V3

开启Mkldnn,V4

项目

代码

下载 


效果

未开启Mkldnn,V3

未开启Mkldnn,V4

开启Mkldnn,V3

开启Mkldnn,V4

项目

VS2022+.net 4.8+OpenCvSharp4

Sdcb.PaddleInference/2.5.0-preview.1

Sdcb.PaddleOCR/2.6.0.6-preview.1

代码

using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR.Models.Online;
using System;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            img = "test.jpg";
            pictureBox1.Image = new Bitmap(img);
            src = Cv2.ImRead(img);
        }

        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        StringBuilder sb = new StringBuilder();

        Mat src;

        FullOcrModel model_v3;
        PaddleOcrAll all_v3;

        FullOcrModel model_v4;
        PaddleOcrAll all_v4;

        bool Mkldnn = false;

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;

            img = ofd.FileName;
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);
            textBox1.Text = "";

            src = Cv2.ImRead(img);
        }

        private void TestV4()
        {
            sb.Clear();
            dt1 = DateTime.Now;
            PaddleOcrResult result = all_v4.Run(src);
            dt2 = DateTime.Now;
            int index = 1;
            foreach (PaddleOcrResultRegion region in result.Regions)
            {
                sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
            }
            sb.AppendLine("------------------------------------");
            sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            sb.AppendLine("Open Mkldnn:" + Mkldnn);
            sb.AppendLine("V4耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            textBox1.Text = sb.ToString();
        }

        private void TestV3()
        {
            sb.Clear();
            dt1 = DateTime.Now;
            PaddleOcrResult result = all_v3.Run(src);
            dt2 = DateTime.Now;
            int index = 1;
            foreach (PaddleOcrResultRegion region in result.Regions)
            {
                sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
            }
            sb.AppendLine("------------------------------------");
            sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            sb.AppendLine("Open Mkldnn:" + Mkldnn);
            sb.AppendLine("V3耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            textBox1.Text = sb.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (all_v3 == null)
            {
                MessageBox.Show("请先初始化");
                return;
            }

            if (src == null)
            {
                MessageBox.Show("请先选择图片");
                return;
            }

            TestV3();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (all_v4 == null)
            {
                MessageBox.Show("请先初始化");
                return;
            }

            if (src == null)
            {
                MessageBox.Show("请先选择图片");
                return;
            }
            TestV4();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Mkldnn = false;
            Task t= Init();
            t.Wait();
            MessageBox.Show("初始化成功");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Mkldnn = true;
            Task t = Init_Mkldnn();
            t.Wait();
            MessageBox.Show("初始化成功");
        }

        async Task Init()
        {
            model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
            all_v3 = new PaddleOcrAll(model_v3, PaddleDevice.Mkldnn());
            all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
            all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */

            //model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
            //all_v4 = new PaddleOcrAll(model_v4, PaddleDevice.Mkldnn());
            //all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
            //all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */

            model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
            all_v4 = new PaddleOcrAll(model_v4,
                    PaddleDevice.Mkldnn(),
                    PaddleDevice.Mkldnn(),
                    c =>
                    {
                        Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle());
                        Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableORTOptimization(c.UnsafeGetHandle());
                        // c.CpuMathThreadCount = 8;
                    });

            all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
            all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
        }

        async Task Init_Mkldnn()
        {
            model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
            all_v3 = new PaddleOcrAll(model_v3,
                    c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
                    PaddleDevice.Mkldnn(),
                    c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));

            all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
            all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */

            model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
            all_v4 = new PaddleOcrAll(model_v4,
                    c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
                    PaddleDevice.Mkldnn(),
                    c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));

            all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
            all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
        }
    }
}
 

  1. using OpenCvSharp;
  2. using Sdcb.PaddleInference;
  3. using Sdcb.PaddleOCR;
  4. using Sdcb.PaddleOCR.Models;
  5. using Sdcb.PaddleOCR.Models.Online;
  6. using System;
  7. using System.Drawing;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace OCRTest_V4
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. img = "test.jpg";
  22. pictureBox1.Image = new Bitmap(img);
  23. src = Cv2.ImRead(img);
  24. }
  25. Bitmap bmp;
  26. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  27. string img = "";
  28. DateTime dt1 = DateTime.Now;
  29. DateTime dt2 = DateTime.Now;
  30. StringBuilder sb = new StringBuilder();
  31. Mat src;
  32. FullOcrModel model_v3;
  33. PaddleOcrAll all_v3;
  34. FullOcrModel model_v4;
  35. PaddleOcrAll all_v4;
  36. bool Mkldnn = false;
  37. private void button2_Click(object sender, EventArgs e)
  38. {
  39. OpenFileDialog ofd = new OpenFileDialog();
  40. ofd.Filter = fileFilter;
  41. if (ofd.ShowDialog() != DialogResult.OK) return;
  42. pictureBox1.Image = null;
  43. img = ofd.FileName;
  44. bmp = new Bitmap(img);
  45. pictureBox1.Image = new Bitmap(img);
  46. textBox1.Text = "";
  47. src = Cv2.ImRead(img);
  48. }
  49. private void TestV4()
  50. {
  51. sb.Clear();
  52. dt1 = DateTime.Now;
  53. PaddleOcrResult result = all_v4.Run(src);
  54. dt2 = DateTime.Now;
  55. int index = 1;
  56. foreach (PaddleOcrResultRegion region in result.Regions)
  57. {
  58. sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
  59. }
  60. sb.AppendLine("------------------------------------");
  61. sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  62. sb.AppendLine("Open Mkldnn:" + Mkldnn);
  63. sb.AppendLine("V4耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
  64. textBox1.Text = sb.ToString();
  65. }
  66. private void TestV3()
  67. {
  68. sb.Clear();
  69. dt1 = DateTime.Now;
  70. PaddleOcrResult result = all_v3.Run(src);
  71. dt2 = DateTime.Now;
  72. int index = 1;
  73. foreach (PaddleOcrResultRegion region in result.Regions)
  74. {
  75. sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
  76. }
  77. sb.AppendLine("------------------------------------");
  78. sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  79. sb.AppendLine("Open Mkldnn:" + Mkldnn);
  80. sb.AppendLine("V3耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
  81. textBox1.Text = sb.ToString();
  82. }
  83. private void button1_Click(object sender, EventArgs e)
  84. {
  85. if (all_v3 == null)
  86. {
  87. MessageBox.Show("请先初始化");
  88. return;
  89. }
  90. if (src == null)
  91. {
  92. MessageBox.Show("请先选择图片");
  93. return;
  94. }
  95. TestV3();
  96. }
  97. private void button3_Click(object sender, EventArgs e)
  98. {
  99. if (all_v4 == null)
  100. {
  101. MessageBox.Show("请先初始化");
  102. return;
  103. }
  104. if (src == null)
  105. {
  106. MessageBox.Show("请先选择图片");
  107. return;
  108. }
  109. TestV4();
  110. }
  111. private void button4_Click(object sender, EventArgs e)
  112. {
  113. Mkldnn = false;
  114. Task t= Init();
  115. t.Wait();
  116. MessageBox.Show("初始化成功");
  117. }
  118. private void button5_Click(object sender, EventArgs e)
  119. {
  120. Mkldnn = true;
  121. Task t = Init_Mkldnn();
  122. t.Wait();
  123. MessageBox.Show("初始化成功");
  124. }
  125. async Task Init()
  126. {
  127. model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
  128. all_v3 = new PaddleOcrAll(model_v3, PaddleDevice.Mkldnn());
  129. all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
  130. all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
  131. //model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
  132. //all_v4 = new PaddleOcrAll(model_v4, PaddleDevice.Mkldnn());
  133. //all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
  134. //all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
  135. model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
  136. all_v4 = new PaddleOcrAll(model_v4,
  137. PaddleDevice.Mkldnn(),
  138. PaddleDevice.Mkldnn(),
  139. c =>
  140. {
  141. Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle());
  142. Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableORTOptimization(c.UnsafeGetHandle());
  143. // c.CpuMathThreadCount = 8;
  144. });
  145. all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
  146. all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
  147. }
  148. async Task Init_Mkldnn()
  149. {
  150. model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
  151. all_v3 = new PaddleOcrAll(model_v3,
  152. c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
  153. PaddleDevice.Mkldnn(),
  154. c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));
  155. all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
  156. all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
  157. model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
  158. all_v4 = new PaddleOcrAll(model_v4,
  159. c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
  160. PaddleDevice.Mkldnn(),
  161. c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));
  162. all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
  163. all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
  164. }
  165. }
  166. }

下载 

Demo下载

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

闽ICP备14008679号