PP-OCRv5

目录

PP-OCRv5官方效果如下

C++封装、C#调用效果

项目

代码

下载 


PP-OCRv5官方效果如下

C++封装、C#调用效果

项目

代码

using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

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

        const string DllName = "lw.PPOCRSharp.dll";

        //初始化
        [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.StdCall)]
        public extern static int init(ref IntPtr engine
            , int cpu_threads
            , bool enable_mkldnn

            , string det_model_dir
            , int limit_side_len
            , double det_db_thresh
            , double det_db_box_thresh
            , double det_db_unclip_ratio
            , bool use_dilation

            , bool cls
            , bool use_angle_cls
            , string cls_model_dir
            , double cls_thresh
            , double cls_batch_num

            , string rec_model_dir
            , string rec_char_dict_path
            , int rec_batch_num
            , int rec_img_h
            , int rec_img_w

            , StringBuilder msg);

        //识别
        [DllImport(DllName, EntryPoint = "ocr", CallingConvention = CallingConvention.StdCall)]
        public extern static int ocr(IntPtr engine, IntPtr image, StringBuilder msg, out IntPtr ocr_result, out int ocr_result_len);

        //释放
        [DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.StdCall)]
        public extern static int destroy(IntPtr engine, StringBuilder msg);

        static IntPtr OCREngine;

        private Bitmap bmp;

        private String imgPath = null;

        private List<OCRResult> ltOCRResult;

        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tif;*.png";

        private StringBuilder OCRResultInfo = new StringBuilder();
        private StringBuilder OCRResultAllInfo = new StringBuilder();

        Pen pen = new Pen(Brushes.Red, 2f);

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                imgPath = ofd.FileName;
                bmp = new Bitmap(imgPath);
                pictureBox1.Image = bmp;
                richTextBox1.Clear();

                button2_Click(null, null);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (imgPath == null)
            {
                return;
            }
            button1.Enabled = false;
            button2.Enabled = false;
            richTextBox1.Clear();
            OCRResultInfo.Clear();
            OCRResultAllInfo.Clear();

            Application.DoEvents();

            Mat img = new Mat(imgPath);
            StringBuilder msgTemp = new StringBuilder(128);
            StringBuilder ocrResultStr = new StringBuilder(1024 * 100);

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            IntPtr strPtr;
            int ocr_result_len = 0;

            int res = ocr(OCREngine, img.CvPtr, msgTemp, out strPtr, out ocr_result_len);
            byte[] buffer = new byte[ocr_result_len];
            Marshal.Copy(strPtr, buffer, 0, ocr_result_len);
            string ocr_result = Encoding.UTF8.GetString(buffer);
            Marshal.FreeCoTaskMem(strPtr);
            //Console.WriteLine(ocr_result);
            stopwatch.Stop();
            double totalTime = stopwatch.Elapsed.TotalSeconds;

            OCRResultAllInfo.AppendLine($"耗时: {totalTime:F2}s");
            OCRResultAllInfo.AppendLine("---------------------------");

            OCRResultInfo.AppendLine($"耗时: {totalTime:F2}s");
            OCRResultInfo.AppendLine("---------------------------");

            if (res == 0)
            {

                ltOCRResult = Newtonsoft.Json.JsonConvert.DeserializeObject<List<OCRResult>>(ocr_result);
                OCRResultAllInfo.Append(JsonConvert.SerializeObject(ltOCRResult, Newtonsoft.Json.Formatting.Indented));
                Graphics graphics = Graphics.FromImage(bmp);

                foreach (OCRResult item in ltOCRResult)
                {
                    OCRResultInfo.AppendLine(item.text);
                    System.Drawing.Point[] pt = new System.Drawing.Point[] {
                              new System.Drawing.Point(item.x1, item.y1)
                            , new System.Drawing.Point(item.x2, item.y2)
                            , new System.Drawing.Point(item.x3, item.y3)
                            , new System.Drawing.Point(item.x4, item.y4)
                        };
                    graphics.DrawPolygon(pen, pt);
                }
                graphics.Dispose();

                if (checkBox1.Checked)
                {
                    richTextBox1.Text = OCRResultAllInfo.ToString();
                }
                else
                {
                    richTextBox1.Text = OCRResultInfo.ToString();
                }

                pictureBox1.Image = null;
                pictureBox1.Image = bmp;
            }
            else
            {
                MessageBox.Show("识别失败," + msgTemp.ToString());
            }

            img.Release();
            button1.Enabled = true;
            button2.Enabled = true;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        void LoadModel()
        {

            StringBuilder msgTemp = new StringBuilder(128);
            string root_dir = Application.StartupPath + @"\inference";

            int cpu_threads = Environment.ProcessorCount + 2;
            bool enable_mkldnn = true;

            string det_model_dir = "";
            int limit_side_len = 960;
            double det_db_thresh = 0.3;
            double det_db_box_thresh = 0.6;
            double det_db_unclip_ratio = 1.2;
            bool use_dilation = false;

            bool cls = false;
            bool use_angle_cls = true;
            string cls_model_dir = root_dir + @"\ch_ppocr_mobile_v2.0_cls_infer\";
            double cls_thresh = 0.9;
            int cls_batch_num = 1;

            string rec_model_dir = "";
            string rec_char_dict_path = root_dir + @"\ppocrv5_dict.txt";
            int rec_batch_num = Environment.ProcessorCount;
            int rec_img_h = 48;
            int rec_img_w = 320;

            if (radioButton1.Checked)
            {
                det_model_dir = root_dir + @"\PP-OCRv5_mobile_det_infer\";
                rec_model_dir = root_dir + @"\PP-OCRv5_mobile_rec_infer\";
            }
            else
            {
                det_model_dir = root_dir + @"\PP-OCRv5_server_det_infer\";
                rec_model_dir = root_dir + @"\PP-OCRv5_server_rec_infer\";
            }
            int res = init(ref OCREngine
                        , cpu_threads
                        , enable_mkldnn

                        , det_model_dir
                        , limit_side_len
                        , det_db_thresh
                        , det_db_box_thresh
                        , det_db_unclip_ratio
                        , use_dilation

                        , cls
                        , use_angle_cls
                        , cls_model_dir
                        , cls_thresh
                        , cls_batch_num

                        , rec_model_dir
                        , rec_char_dict_path
                        , rec_batch_num
                        , rec_img_h
                        , rec_img_w

                        , msgTemp);

            if (res == 0)
            {
                MessageBox.Show("模型加载成功!");
            }
            else
            {
                string msg = msgTemp.ToString();
                MessageBox.Show("模型加载失败," + msg);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            if (checkBox1.Checked)
            {
                richTextBox1.Text = OCRResultAllInfo.ToString();
            }
            else
            {
                richTextBox1.Text = OCRResultInfo.ToString();
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb != null && rb.Checked)
            {
                //MessageBox.Show("选中的是:" + rb.Text);
                LoadModel();
            }
        }
    }


}
 

using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

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

        const string DllName = "lw.PPOCRSharp.dll";

        //初始化
        [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.StdCall)]
        public extern static int init(ref IntPtr engine
            , int cpu_threads
            , bool enable_mkldnn

            , string det_model_dir
            , int limit_side_len
            , double det_db_thresh
            , double det_db_box_thresh
            , double det_db_unclip_ratio
            , bool use_dilation

            , bool cls
            , bool use_angle_cls
            , string cls_model_dir
            , double cls_thresh
            , double cls_batch_num

            , string rec_model_dir
            , string rec_char_dict_path
            , int rec_batch_num
            , int rec_img_h
            , int rec_img_w

            , StringBuilder msg);

        //识别
        [DllImport(DllName, EntryPoint = "ocr", CallingConvention = CallingConvention.StdCall)]
        public extern static int ocr(IntPtr engine, IntPtr image, StringBuilder msg, out IntPtr ocr_result, out int ocr_result_len);

        //释放
        [DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.StdCall)]
        public extern static int destroy(IntPtr engine, StringBuilder msg);

        static IntPtr OCREngine;

        private Bitmap bmp;

        private String imgPath = null;

        private List<OCRResult> ltOCRResult;

        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tif;*.png";

        private StringBuilder OCRResultInfo = new StringBuilder();
        private StringBuilder OCRResultAllInfo = new StringBuilder();

        Pen pen = new Pen(Brushes.Red, 2f);

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                imgPath = ofd.FileName;
                bmp = new Bitmap(imgPath);
                pictureBox1.Image = bmp;
                richTextBox1.Clear();

                button2_Click(null, null);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (imgPath == null)
            {
                return;
            }
            button1.Enabled = false;
            button2.Enabled = false;
            richTextBox1.Clear();
            OCRResultInfo.Clear();
            OCRResultAllInfo.Clear();

            Application.DoEvents();

            Mat img = new Mat(imgPath);
            StringBuilder msgTemp = new StringBuilder(128);
            StringBuilder ocrResultStr = new StringBuilder(1024 * 100);

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            IntPtr strPtr;
            int ocr_result_len = 0;

            int res = ocr(OCREngine, img.CvPtr, msgTemp, out strPtr, out ocr_result_len);
            byte[] buffer = new byte[ocr_result_len];
            Marshal.Copy(strPtr, buffer, 0, ocr_result_len);
            string ocr_result = Encoding.UTF8.GetString(buffer);
            Marshal.FreeCoTaskMem(strPtr);
            //Console.WriteLine(ocr_result);
            stopwatch.Stop();
            double totalTime = stopwatch.Elapsed.TotalSeconds;

            OCRResultAllInfo.AppendLine($"耗时: {totalTime:F2}s");
            OCRResultAllInfo.AppendLine("---------------------------");

            OCRResultInfo.AppendLine($"耗时: {totalTime:F2}s");
            OCRResultInfo.AppendLine("---------------------------");

            if (res == 0)
            {

                ltOCRResult = Newtonsoft.Json.JsonConvert.DeserializeObject<List<OCRResult>>(ocr_result);
                OCRResultAllInfo.Append(JsonConvert.SerializeObject(ltOCRResult, Newtonsoft.Json.Formatting.Indented));
                Graphics graphics = Graphics.FromImage(bmp);

                foreach (OCRResult item in ltOCRResult)
                {
                    OCRResultInfo.AppendLine(item.text);
                    System.Drawing.Point[] pt = new System.Drawing.Point[] {
                              new System.Drawing.Point(item.x1, item.y1)
                            , new System.Drawing.Point(item.x2, item.y2)
                            , new System.Drawing.Point(item.x3, item.y3)
                            , new System.Drawing.Point(item.x4, item.y4)
                        };
                    graphics.DrawPolygon(pen, pt);
                }
                graphics.Dispose();

                if (checkBox1.Checked)
                {
                    richTextBox1.Text = OCRResultAllInfo.ToString();
                }
                else
                {
                    richTextBox1.Text = OCRResultInfo.ToString();
                }

                pictureBox1.Image = null;
                pictureBox1.Image = bmp;
            }
            else
            {
                MessageBox.Show("识别失败," + msgTemp.ToString());
            }

            img.Release();
            button1.Enabled = true;
            button2.Enabled = true;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        void LoadModel()
        {

            StringBuilder msgTemp = new StringBuilder(128);
            string root_dir = Application.StartupPath + @"\inference";

            int cpu_threads = Environment.ProcessorCount + 2;
            bool enable_mkldnn = true;

            string det_model_dir = "";
            int limit_side_len = 960;
            double det_db_thresh = 0.3;
            double det_db_box_thresh = 0.6;
            double det_db_unclip_ratio = 1.2;
            bool use_dilation = false;

            bool cls = false;
            bool use_angle_cls = true;
            string cls_model_dir = root_dir + @"\ch_ppocr_mobile_v2.0_cls_infer\";
            double cls_thresh = 0.9;
            int cls_batch_num = 1;

            string rec_model_dir = "";
            string rec_char_dict_path = root_dir + @"\ppocrv5_dict.txt";
            int rec_batch_num = Environment.ProcessorCount;
            int rec_img_h = 48;
            int rec_img_w = 320;

            if (radioButton1.Checked)
            {
                det_model_dir = root_dir + @"\PP-OCRv5_mobile_det_infer\";
                rec_model_dir = root_dir + @"\PP-OCRv5_mobile_rec_infer\";
            }
            else
            {
                det_model_dir = root_dir + @"\PP-OCRv5_server_det_infer\";
                rec_model_dir = root_dir + @"\PP-OCRv5_server_rec_infer\";
            }
            int res = init(ref OCREngine
                        , cpu_threads
                        , enable_mkldnn

                        , det_model_dir
                        , limit_side_len
                        , det_db_thresh
                        , det_db_box_thresh
                        , det_db_unclip_ratio
                        , use_dilation

                        , cls
                        , use_angle_cls
                        , cls_model_dir
                        , cls_thresh
                        , cls_batch_num

                        , rec_model_dir
                        , rec_char_dict_path
                        , rec_batch_num
                        , rec_img_h
                        , rec_img_w

                        , msgTemp);

            if (res == 0)
            {
                MessageBox.Show("模型加载成功!");
            }
            else
            {
                string msg = msgTemp.ToString();
                MessageBox.Show("模型加载失败," + msg);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            if (checkBox1.Checked)
            {
                richTextBox1.Text = OCRResultAllInfo.ToString();
            }
            else
            {
                richTextBox1.Text = OCRResultInfo.ToString();
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb != null && rb.Checked)
            {
                //MessageBox.Show("选中的是:" + rb.Text);
                LoadModel();
            }
        }


    }


}

下载 

源码下载

链接: https://pan.baidu.com/s/1g2sjgtWt9LvBcRonJpByhA?pwd=fnqh 提取码: fnqh

机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集,个人经导师指导并认可通过的高分设计项目,评审分99分,代码完整确保可以运行,小白也可以亲自搞定,主要针对计算机相关专业的正在做大作业的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业。 机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+文档报告+数据集机器学习作业,机器学习和深度学习方法实现的入侵检测源代码+
<think>好的,我现在需要帮助用户了解PP-OCRv3的安装配置和使用说明。首先,用户可能对PP-OCRv3不太熟悉,所以应该先简要介绍一下这个工具,比如它的用途和优势。根据用户提供的引用内容,PP-OCRv3是PaddleOCR的一个版本,专注于文档识别,特别是优化了识别模型。引用[1]提到PP-OCRv4是在v3基础上的升级,说明v3是一个稳定且有效的版本,适合用户使用。 接下来,用户需要安装配置教程。引用[2]和[3]提供了部分安装步骤和资源链接。我需要整理这些信息,确保步骤清晰。首先,安装前的准备工作,比如环境要求和依赖项。Python环境是必须的,可能还需要安装Git来克隆仓库。然后,按照引用[2]的步骤,克隆仓库或下载解压,安装依赖项,安装PaddlePaddle,最后下载模型。 需要注意,用户可能对Python环境和虚拟机的操作不太熟悉,所以需要给出具体的命令,比如使用pip安装requirements.txt,并指定清华镜像源加速下载。安装PaddlePaddle时,要根据是否使用GPU选择不同的版本,这里可能需要进一步询问用户的环境,但考虑到用户可能不清楚,给出两种选项更合适。 下载模型部分,引用[3]提供了v3的检测模型链接,但用户可能需要其他模型,如识别模型,所以需要补充其他模型的下载链接,并说明存放路径。这部分需要确保用户知道如何放置模型文件,避免路径错误。 验证安装的部分,给出一个简单的Python代码示例,让用户测试是否安装成功。同时,常见问题需要涵盖典型错误,比如路径错误、依赖缺失,以及GPU支持的问题。这些是用户在安装过程中容易遇到的,提前说明可以帮助他们自行排查。 使用示例部分,展示如何用命令行和Python代码进行OCR,让用户了解基本用法。应用场景可以列举实际例子,帮助用户理解PP-OCRv3的实用性。 最后,生成的相关问题需要覆盖用户可能进一步的需求,比如优化、模型训练、与其他版本比较、处理中文文档等,这样用户可以根据自己的情况继续探索。 检查整个流程是否覆盖了用户的需求,是否有遗漏的步骤,比如环境准备、依赖安装、模型下载、验证和使用示例。同时,确保引用正确,比如引用[1]用于说明PP-OCRv3的优化,引用[2]和[3]用于安装步骤和模型下载。最后,语言要简洁明了,步骤清晰,方便用户跟随操作。</think>以下是PP-OCRv3的安装配置教程和使用说明,基于官方文档整理: ### 一、环境准备 1. **操作系统**:支持Linux/Windows/macOS 2. **Python环境**:推荐Python 3.7+ 3. **安装依赖库**: ```bash # 使用清华镜像加速 pip install paddleocr==2.7.0 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install -U paddlepaddle==2.5.1 # CPU版本 # 若需GPU支持: pip install paddlepaddle-gpu==2.5.1.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html ``` ### 二、模型下载 PP-OCRv3提供两种预训练模型: 1. **检测模型**: ```bash wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar tar xf en_PP-OCRv3_det_infer.tar ``` 2. **识别模型**: ```bash wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar tar xf en_PP-OCRv3_rec_infer.tar ``` ### 三、验证安装 ```python from paddleocr import PaddleOCR ocr = PaddleOCR(use_angle_cls=True, lang='en') result = ocr.ocr('test.jpg', cls=True) print(result) ``` ### 四、使用示例 **命令行方式**: ```bash paddleocr --image_dir test.jpg --use_angle_cls true --lang en --use_gpu false ``` **Python API**: ```python from paddleocr import PaddleOCR ocr = PaddleOCR( det_model_dir='./en_PP-OCRv3_det_infer', rec_model_dir='./en_PP-OCRv3_rec_infer', use_angle_cls=True, lang='en' ) ``` ### 五、常见问题 1. **路径错误**:确保模型路径正确,推荐使用绝对路径[^2] 2. **依赖缺失**:若报错缺库,执行`pip install -r requirements.txt` 3. **GPU支持**:需安装CUDA 10.2/11.2和对应cuDNN版本 ### 六、性能优化 PP-OCRv3通过以下优化提升性能: $$ \text{精度提升} = \frac{\text{v3指标}-\text{v2指标}}{\text{v2指标}} \times 100\% $$ 在英文场景下,相比v2识别准确率提升8%[^1]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天代码码天天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值