C# PaddleInference OCR文字识别(只识别)

news2024/9/20 22:02:08

说明

C# PaddleInference OCR文字识别(只识别),没有文字区域检测、文字方向判断

测试图片是文字区域检测裁剪出来、处理过的图片

完整的OCR识别查看 C# PaddleInference OCR识别 学习研究Demo_天天代码码天天的博客-CSDN博客

效果

项目

VS2022+.net4.8+OpenCvSharp4+Sdcb.PaddleInference

 测试图片

 代码

using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleInference.Native;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PaddleInference_OCR文字识别
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        OcrShape recShape = new OcrShape(3, 320, 48);
        PaddlePredictor rec_predictor;

        public IReadOnlyList<string> Labels;

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


        private void button1_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 = "";
        }



        private unsafe void Form1_Load(object sender, EventArgs e)
        {

            string startupPath = Application.StartupPath;
            IntPtr det_ptr = PaddleNative.PD_ConfigCreate();
            Encoding PaddleEncoding = Environment.OSVersion.Platform == PlatformID.Win32NT ? Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage) : Encoding.UTF8;

            //识别模型
            IntPtr rec_ptr = PaddleNative.PD_ConfigCreate();
            String rec_programPath = startupPath + "\\ch_PP-OCRv3_rec\\inference.pdmodel";
            String rec_paramsPath = startupPath + "\\ch_PP-OCRv3_rec\\inference.pdiparams";
            byte[] rec_programBytes = PaddleEncoding.GetBytes(rec_programPath);
            byte[] rec_paramsBytes = PaddleEncoding.GetBytes(rec_paramsPath);
            fixed (byte* rec_programPtr = rec_programBytes)
            fixed (byte* rec_paramsPtr = rec_paramsBytes)
            {
                PaddleNative.PD_ConfigSetModel(rec_ptr, (IntPtr)rec_programPtr, (IntPtr)rec_paramsPtr);
            }

            rec_predictor = new PaddlePredictor(PaddleNative.PD_PredictorCreate(rec_ptr));

            //Labels
            String labelsPath = startupPath + "\\ppocr_keys.txt";
            Stream Steam = new FileStream(labelsPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader reader = new StreamReader(Steam);
            List<string> tempList = new List<string>();
            while (!reader.EndOfStream)
            {
                tempList.Add(reader.ReadLine());
            }
            reader.Dispose();
            Steam.Dispose();
            Labels = tempList;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }
            dt1 = DateTime.Now;
            var src = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);

            int modelHeight = recShape.Height;
            int maxWidth = (int)Math.Ceiling(1.0 * src.Width / src.Height * modelHeight);

            Mat channel3 = new Mat();
            if (src.Channels() == 4)
            {
                channel3 = src.CvtColor(ColorConversionCodes.RGBA2BGR);
            }
            else if (src.Channels() == 3)
            {
                channel3 = src.Clone();
            }
            else if (src.Channels() == 1)
            {
                channel3 = src.CvtColor(ColorConversionCodes.GRAY2RGB);
            }
            else
            {
                throw new Exception("Unexpect src channel: {" + src.Channels() + "}, allow: (1/3/4)");
            }

            Mat resized = ResizePadding(channel3, modelHeight, maxWidth);
            Mat normalize = Normalize(resized);

            using (PaddleTensor input = rec_predictor.GetInputTensor(rec_predictor.InputNames[0]))
            {
                int channel = normalize.Channels();
                input.Shape = new[] { 1, channel, modelHeight, maxWidth };
                float[] data = ExtractMat(normalize, channel, modelHeight, maxWidth);
                input.SetData(data);
            }

            normalize.Dispose();
            resized.Dispose();

            if (!rec_predictor.Run())
            {
                throw new Exception($"PaddlePredictor(Recognizer) run failed.");
            }

            using (PaddleTensor output = rec_predictor.GetOutputTensor(rec_predictor.OutputNames[0]))
            {
                float[] data = output.GetData<float>();
                int[] shape = output.Shape;
                GCHandle dataHandle = default;
                try
                {
                    dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                    IntPtr dataPtr = dataHandle.AddrOfPinnedObject();

                    int labelCount = shape[2];
                    int charCount = shape[1];

                    StringBuilder sbInfo = new StringBuilder();

                    for (int i = 0; i < shape[0]; i++)
                    {
                        StringBuilder sb = new StringBuilder();
                        int lastIndex = 0;
                        float score = 0;
                        for (int n = 0; n < charCount; ++n)
                        {
                            Mat mat = new Mat(1, labelCount, MatType.CV_32FC1, dataPtr + (n + i * charCount) * labelCount * sizeof(float));
                            int[] maxIdx = new int[2];
                            mat.MinMaxIdx(out double _, out double maxVal, new int[0], maxIdx);
                            if (maxIdx[1] > 0 && (!(n > 0 && maxIdx[1] == lastIndex)))
                            {
                                score += (float)maxVal;
                                sb.Append(GetLabelByIndex(maxIdx[1]));
                            }
                            lastIndex = maxIdx[1];
                            mat.Dispose();
                        }
                        sbInfo.AppendLine("Text:" + sb.ToString());
                        sbInfo.AppendLine("Score:" + score / sb.Length);
                    }

                    dt2 = DateTime.Now;
                    sbInfo.AppendLine("-----------------------------------\n");
                    sbInfo.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    sbInfo.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms\n");
                    textBox1.Text = sbInfo.ToString();
                }
                finally
                {
                    dataHandle.Free();
                }
            }

        }

        string GetLabelByIndex(int x)
        {
            if (x > 0 && x <= Labels.Count)
            {
                return Labels[x - 1];
            }
            else if (x == Labels.Count + 1)
            {
                return "";
            }
            else
            {
                throw new Exception("Unable to GetLabelByIndex: index {" + x + "} out of range {" + Labels.Count + "}, OCR model or labels not matched?");
            }
        }

        private Mat ResizePadding(Mat src, int height, int targetWidth)
        {
            OpenCvSharp.Size size = src.Size();
            float whRatio = 1.0f * size.Width / size.Height;
            int width = (int)Math.Ceiling(height * whRatio);

            if (width == targetWidth)
            {
                return src.Resize(new OpenCvSharp.Size(width, height));
            }
            else
            {
                Mat resized = src.Resize(new OpenCvSharp.Size(width, height));
                return resized.CopyMakeBorder(0, 0, 0, targetWidth - width, BorderTypes.Constant, Scalar.Gray);
            }
        }

        private Mat Normalize(Mat src)
        {
            Mat normalized = new Mat();
            src.ConvertTo(normalized, MatType.CV_32FC3, 1.0 / 255);
            Mat[] bgr = normalized.Split();
            float[] scales = new[] { 1 / 0.229f, 1 / 0.224f, 1 / 0.225f };
            float[] means = new[] { 0.485f, 0.456f, 0.406f };
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 * scales[i], (0.0 - means[i]) * scales[i]);
            }
            normalized.Dispose();
            Mat dest = new Mat();
            Cv2.Merge(bgr, dest);
            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }
            return dest;
        }



        private float[] ExtractMat(Mat mat, int channel, int height, int width)
        {
            float[] result = new float[1 * channel * width * height];
            GCHandle resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
            IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
            try
            {

                Mat src = mat.Clone();
                if (src.Channels() != channel)
                {
                    throw new Exception($"src channel={src.Channels()}, expected {channel}");
                }
                for (int c = 0; c < channel; ++c)
                {
                    Mat dest = new Mat(height, width, MatType.CV_32FC1, resultPtr + c * height * width * sizeof(float));
                    Cv2.ExtractChannel(src, dest, c);
                    dest.Dispose();
                }
                return result;
            }
            finally
            {
                resultHandle.Free();
            }
        }



        private float[] ExtractMat(Mat[] srcs, int channel, int height, int width)
        {
            float[] result = new float[srcs.Length * channel * width * height];
            GCHandle resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
            IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
            try
            {
                for (int i = 0; i < srcs.Length; ++i)
                {
                    Mat src = srcs[i];
                    if (src.Channels() != channel)
                    {
                        throw new Exception($"src[{i}] channel={src.Channels()}, expected {channel}");
                    }
                    for (int c = 0; c < channel; ++c)
                    {
                        Mat dest = new Mat(height, width, MatType.CV_32FC1, resultPtr + (c + i * channel) * height * width * sizeof(float));
                        Cv2.ExtractChannel(src, dest, c);
                        dest.Dispose();
                    }
                }
                return result;
            }
            finally
            {
                resultHandle.Free();
            }
        }

    }
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

-1在内存中的存储及打印问题。

首先先看看代码&#xff1a; #include"stdio.h" int main() { char a -1; signed char b -1; unsigned char c -1; printf("a%d b%d c%d", a, b, c); return 0; } 代码很简单&#xff0c;问打印结果是什么&#xff1f; 下面我…

Java 比对两张图片的差异

1.基本介绍 Github上的“https://github.com/akullpp/awesome-java”页整理了非常多的各类Java组件的实现&#xff0c;前面一篇从它的图片处理篇找到了《image-comparison》进行了动手实践&#xff0c;关于图片处理的二维码组件《ZXing》本站曾有实践&#xff1b;关于图片识别…

CUDA+CUDNN+torch+torchvision安装

弄了好久&#xff0c;终于弄好了&#xff01;&#xff01;&#xff01; 原因&#xff1a;其实之前我是已经配置好pytorch的相关环境的了。但是这段时间&#xff0c;在跑GNN相关论文中的代码时&#xff0c;发现代码中的某个函数要求torch必须得是1.8 而我之前安装的是torch1.1…

leetcode-209.长度最小的子数组

leetcode-209.长度最小的子数组 文章目录 leetcode-209.长度最小的子数组题目描述代码提交(快慢指针-滑动窗口) 题目描述 代码提交(快慢指针-滑动窗口) 代码 class Solution {public:int minSubArrayLen(int target, vector<int> &nums) {int slow 0;int fast 0;i…

Spring中事务传播机制的理解与简单试用

目录 一&#xff0c;前言 二&#xff0c;Spring框架中的事务传播行为 三&#xff0c;事务的传播行为测试 Propagation.REQUIRED Propagation.SUPPORTS Propagation.MANDATORY Propagation.REQUIRES_NEW Propagation.NOT_SUPPORTED Propagation.NEVER Propagation.NES…

c++11 标准模板(STL)(std::basic_istream)(三)

定义于头文件 <istream> template< class CharT, class Traits std::char_traits<CharT> > class basic_istream : virtual public std::basic_ios<CharT, Traits> 类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的…

从零配置 linux 开发环境

文章目录 目的效果图配置本地 Windows 主机好用工具WSLSSH 连接远程 Linux 开发机配置本机字体【in-prog】配置 vscode 远程连接 配置远程 Linux 主机zsh & oh-my-zsh配置 github 的 SSHneovimvundleinit.vim 文件 vim-plug.lua 文件 tmuxclangcpplint 目的 记录下我的开发…

Go语言开发者的Apache Arrow使用指南:高级数据结构

经过对前面两篇文章《Arrow数据类型》[1]和《Arrow Go实现的内存管理》[2]的学习&#xff0c;我们知道了各种Arrow array type以及它们在内存中的layout&#xff0c;我们了解了Go arrow实现在内存管理上的一些机制和使用原则。 Arrow的array type只是一个定长的、同类型的值序列…

[SWPUCTF 2021 新生赛]jicao

点进去后是一段php代码 <?php highlight_file(index.php); include("flag.php"); $id$_POST[id]; $jsonjson_decode($_GET[json],true); if ($id"wllmNB"&&$json[x]"wllm") {echo $flag;} ?> 包含了flag.php文件&#xff0c;设定…

数据结构关键路径问题:下面是一个有10个活动的AOE图,时间余量最大的活动是()

关键路径问题 名人说&#xff1a;莫听穿林打叶声&#xff0c;何妨吟啸且徐行。—— 苏轼《定风波莫听穿林打叶声》 本篇笔记整理&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 目录 关键路径问题〇、概念说明1、AOE网2、关键路…

4-软件错误(BUG)

目录 1.什么是bug? 2.如何描述一个bug? ①发现问题的版本 ②问题出现的环境 ③错误重现的步骤 ④预期行为的描述 ⑤错误行为的描述 ⑥其他 ⑦不要把多个bug放到一起 PS&#xff1a;案例1 PS&#xff1a;案例2 3.如何定义bug的级别&#xff1f; ①Blocker&#x…

FFmpeg5.0源码阅读—— avcodec_send_packetavcodec_receive_frame

摘要&#xff1a;本文主要描述了FFmpeg中用于解码的接口的具体调用流程&#xff0c;详细描述了该接口被调用时所作的具体工作。   关键字&#xff1a;ffmpeg、avcodec_send_packet、avcodec_receive_frame   读者须知&#xff1a;读者需要了解FFmpeg的基本使用流程&#xf…

CUDA C编程及GPU基本知识【二】

文章目录 1、CPU和GPU的架构2、CPUs: 延迟导向设计和GPUs: 吞吐导向设计2.1 CPUs: 延迟导向设计2.2 GPUs: 吞吐导向设计2.3 GPU&CPU特点2.4 GPU编程&#xff1a;什么样的问题适合GPU 3、GPU编程与CUDA3.1 CUDA编程并行计算整体流程3.2 CUDA编程术语3.2.1 硬件3.2.2 内存模型…

Hive分桶

分桶的概述 为什么要分桶 数据分区可能导致有些分区数据过多&#xff0c;有些分区数据极少。分桶是将数据集分解为若干部分(数据文件)的另一种技术。分区和分桶其实都是对数据更细粒度的管理。当单个分区或者表中的数据越来越大&#xff0c;分区不能细粒度的划分数据时&#x…

阐述说明NLP发展历史,以及 NLP与chatgpt的关系

自然语言处理&#xff08;Natural Language Processing&#xff0c;NLP&#xff09;是人工智能&#xff08;AI&#xff09;领域的一个重要分支&#xff0c;关注计算机与人类&#xff08;自然&#xff09;语言之间的交互。NLP的目标是使计算机能够理解、生成和解释自然语言&…

心法利器[89] | 实用文本生成中的解码方法

心法利器 本栏目主要和大家一起讨论近期自己学习的心得和体会&#xff0c;与大家一起成长。具体介绍&#xff1a;仓颉专项&#xff1a;飞机大炮我都会&#xff0c;利器心法我还有。 2022年新一版的文章合集已经发布&#xff0c;累计已经60w字了&#xff0c;获取方式看这里&…

[CVPR‘23] PanoHead: Geometry-Aware 3D Full-Head Synthesis in 360 deg

论文&#xff5c;项目 总结&#xff1a; 任务&#xff1a;3D human head synthesis现有问题&#xff1a;GANs无法在「in-the-wild」「single-view」的图片情况下&#xff0c;生成360度人像解决方案&#xff1a;1&#xff09;提出了two-stage self-adaptive image alignment&am…

C++ 设计模式之策略模式

文章目录 一、简介二、场景三、举个栗子四、小结参考资料 一、简介 策略模式的定义很简单&#xff1a;即创建一系列的算法,把它们一个个封装起来 , 并且使它们可相互替换&#xff08;用扩展的方式来面对未来变化&#xff09;。在GoF一书中将其定位为一种“对象行为式模式”&…

vs code insiders 配置c语言

vs code insiders 配置c语言 1.下载插件 2.再配置代码 &#xff08;1&#xff09;launch.json {// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/…

操作系统的可扩展访问控制

访问控制是操作系统安全的基石&#xff0c;当前的操作系统已部署了很多访问控制的模型&#xff1a;Unix和Windows NT多用户安全&#xff1b;SELinux中的类型执行&#xff1b;反恶意软件产品&#xff1b;Apple OS X&#xff0c;Apple iOS和Google Android中的应用沙盒&#xff1…