Paddle OCR V4 测试Demo

 效果

项目

VS2022+.net4.8+OCRV4

代码

using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace PaddleInference.OCRServiceV4
{public partial class frmTest : Form{public frmTest(){InitializeComponent();}String startupPath;public PaddleOcrAll paddleOcr;private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";Bitmap bmp;PaddleOcrResult ocrResult;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;String imgPath;Mat mat;Pen pen = new Pen(Brushes.Red, 2);private void frmTest_Load(object sender, EventArgs e){comboBox1.SelectedIndex = 0;startupPath = Application.StartupPath;rbOnnx.Checked = true;}private void btnLoadModel_Click(object sender, EventArgs e){LoadModel();MessageBox.Show("加载完成");}void LoadModel(){string detectionModelDir;string classificationModelDir;string recognitionModelDir;string labelFilePath;if (comboBox1.SelectedIndex == 0){detectionModelDir = startupPath + "\\inference\\ch_PP-OCRv4_det_infer";classificationModelDir = startupPath + "\\inference\\ch_ppocr_mobile_v2.0_cls_infer";recognitionModelDir = startupPath + "\\inference\\ch_PP-OCRv4_rec_infer";labelFilePath = startupPath + "\\inference\\ppocr_keys.txt";}else{detectionModelDir = startupPath + "\\inference_server\\detv4_teacher_inference";classificationModelDir = startupPath + "\\inference_server\\ch_ppocr_mobile_v2.0_cls_infer";recognitionModelDir = startupPath + "\\inference_server\\ch_PP-OCRv4_rec_server_infer";labelFilePath = startupPath + "\\inference_server\\ppocr_keys.txt";}FullOcrModel model = FullOcrModel.FromDirectory(detectionModelDir, classificationModelDir, recognitionModelDir, labelFilePath, ModelVersion.V4);if (rbOnnx.Enabled == true){paddleOcr = new PaddleOcrAll(model, PaddleDevice.Onnx());}else if (rbOpenblas.Enabled == true){paddleOcr = new PaddleOcrAll(model, PaddleDevice.Openblas());}else if (rbMkldnn.Enabled == true){paddleOcr = new PaddleOcrAll(model, PaddleDevice.Mkldnn());}else{paddleOcr = new PaddleOcrAll(model);}paddleOcr.AllowRotateDetection = true;    /* 允许识别有角度的文字 */paddleOcr.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */}private void btnSelect_Click(object sender, EventArgs e){if (paddleOcr == null){MessageBox.Show("请先加载模型");return;}OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;imgPath = ofd.FileName;bmp = new Bitmap(imgPath, true);RotateImage(bmp);pictureBox1.Image = bmp;richTextBox1.Clear();mat = new Mat(ofd.FileName);//pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);dt1 = DateTime.Now;ocrResult = paddleOcr.Run(mat);dt2 = DateTime.Now;ShowOCRResult(ocrResult);}/// <summary>/// 显示结果/// </summary>private void ShowOCRResult(PaddleOcrResult ocrResult){richTextBox1.Clear();Bitmap bitmap = (Bitmap)this.pictureBox1.Image;richTextBox1.AppendText("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms\n");richTextBox1.AppendText("-----------------------------------\n");richTextBox1.AppendText(ocrResult.Text + "\n");using (Graphics g = Graphics.FromImage(bitmap)){foreach (var item in ocrResult.Regions){g.DrawRectangle(pen, item.Rect.BoundingRect().Left, item.Rect.BoundingRect().Top, item.Rect.BoundingRect().Width, item.Rect.BoundingRect().Height);}}pictureBox1.Image = null;pictureBox1.Image = bitmap;}private void button1_Click(object sender, EventArgs e){if (paddleOcr == null){MessageBox.Show("请先加载模型");return;}if (pictureBox1.Image == null){return;}richTextBox1.Clear();Application.DoEvents();dt1 = DateTime.Now;ocrResult = paddleOcr.Run(mat);dt2 = DateTime.Now;ShowOCRResult(ocrResult);}/// <summary>/// 根据图片exif调整方向/// </summary>/// <param name="img"></param>public void RotateImage(Bitmap img){var exif = img.PropertyItems;byte orien = 0;var item = exif.Where(m => m.Id == 274).ToArray();if (item.Length > 0)orien = item[0].Value[0];switch (orien){case 2:img.RotateFlip(RotateFlipType.RotateNoneFlipX);//horizontal flipbreak;case 3:img.RotateFlip(RotateFlipType.Rotate180FlipNone);//right-topbreak;case 4:img.RotateFlip(RotateFlipType.RotateNoneFlipY);//vertical flipbreak;case 5:img.RotateFlip(RotateFlipType.Rotate90FlipX);break;case 6:img.RotateFlip(RotateFlipType.Rotate90FlipNone);//right-topbreak;case 7:img.RotateFlip(RotateFlipType.Rotate270FlipX);break;case 8:img.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottombreak;default:break;}}}
}

Demo下载

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/67787.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一文教你学会Termux+SFTP+远程文件传输

文章目录 1. 安装openSSH2. 安装cpolar3. 远程SFTP连接配置4. 远程SFTP访问4. 配置固定远程连接地址 SFTP&#xff08;SSH File Transfer Protocol&#xff09;是一种基于SSH&#xff08;Secure Shell&#xff09;安全协议的文件传输协议。与FTP协议相比&#xff0c;SFTP使用了…

智慧工地源码,PC+APP源码,项目管理端+企业监管端+数据大屏端

智慧工地概念 智慧工地是一种崭新的工程全生命周期管理理念&#xff0c;是指运用信息化手段&#xff0c;通过对工程项目进行精确设计和施工模拟&#xff0c;围绕施工过程管理&#xff0c;建立互联协同、智能生产、科学管理的施工项目信息化生态圈&#xff0c;并将此数据在虚拟…

【Linux命令行与Shell脚本编程】第十九章 正则表达式

Linux命令行与Shell脚本编程 第十九章 正则表达式 文章目录 Linux命令行与Shell脚本编程 第十九章 正则表达式九.正则表达式9.1.正则表达式基础9.1.1.正则表达式的类型9.2.定义BRE模式9.2.1.普通文本9.2.2.特殊字符 9.2.3.锚点字符锚定行首^锚定行尾$组合锚点 9.2.4.点号字符\.…

【Spring专题】Spring之Bean的生命周期源码解析——阶段二(IOC之实例化)

目录 前言阅读准备阅读指引阅读建议 课程内容一、SpringIOC之实例化1.1 简单回顾1.2 概念回顾1.3 核心方法讲解 二、方法讲解2.1 AbstractBeanFactory#getMergedLocalBeanDefinition&#xff1a;合并BeanDefinition2.2 AbstractAutowireCapableBeanFactory#createBean&#xff…

USB协议精读

1.1.枚举 1.1.1.上电连接过程 1.1.2.设备识别过程 1.1.3.枚举过程 1.1.3.1.获取设备描述符&#xff08;只关注设备描述符中最大数据包长度&#xff09; 1.1.3.2.设置设备针对主机的唯一从机地址 1.1.3.3.获取完整设备描述符 1.1.3.4.获取配置描述符 先获取标准配置描述符&a…

【数据库基础】Mysql下载安装及配置

下载 下载地址&#xff1a;https://downloads.mysql.com/archives/community/ 当前最新版本为 8.0版本&#xff0c;可以在Product Version中选择指定版本&#xff0c;在Operating System中选择安装平台&#xff0c;如下 安装 MySQL安装文件分两种 .msi和.zip [外链图片转存失…

JVM运行时五大数据区域详解

前言&#xff1a; java虚拟机再执行Java程序的时候把它所拥有的内存区域划分了若干个数据区域。这些区域有着不同的功能&#xff0c;各司其职。这些区域不但功能不同&#xff0c;创建、销毁时间也不同。有些区域为线程私有&#xff0c;如&#xff1a;每个线程都有自己的程序计数…

openGauss学习笔记-39 openGauss 高级数据管理-分区表

文章目录 openGauss学习笔记-39 openGauss 高级数据管理-分区表39.1 范围分区表的分类39.2 创建范围分区39.2.1 创建VALUES LESS THAN范围分区表语法格式39.2.2 创建VALUES LESS THAN范围分区表参数说明39.2.3 创建VALUES LESS THAN范围分区表示例 39.3 询分区表39.3.1 查询分区…

【JPCS出版】第五届能源、电力与电网国际学术会议(ICEPG 2023)

第五届能源、电力与电网国际学术会议&#xff08;ICEPG 2023&#xff09; 2023 5th International Conference on Energy, Power and Grid 最近几年&#xff0c;不少代表委员把目光投向能源电力领域&#xff0c;对促进新能源发电产业健康发展、电力绿色低碳发展&#xff0c;提…

基于Yolov5与LabelMe训练自己数据的图像分割完整流程

基于Yolov5与LabelMe训练自己数据的实例分割完整流程 1. Yolov5配置2. 创建labelme虚拟环境4. 接下来开始使用labelme绘制分割数据集4.1 json to txt4.2 划分数据集(可分可不分) 5. 训练 1. Yolov5配置 参照这边文章&#xff1a; https://blog.csdn.net/ruotianxia/article/de…

常见分辨率时序信息

分辨率列表 分辨率一:640x480(逐行) 分辨率二:800x600(逐行) 分辨率三:1024x768(逐行) 分辨率四:大名鼎鼎720P(逐行) 注:选择720P@30帧的,需拉长HOR TOTAL TIME 分辨率五:1280x800(逐行) 分辨率六:1280x960(逐行

Nginx网站服务(安装nginx、平滑升级nginx、nginx各种访问配置)

一、Nginx概述 1、什么是nginx&#xff1f; 稳定性高、系统资源消耗低、对HTTP并发连接的处理能力高&#xff08;单台物理器可支持30000-50000个并发请求&#xff09; NG并发连接能力有2个因素的影响 ①CPU的个数 ②本地吴立琪系统的最大文件打开数2、Nginx应用场景 静态服…