PaddleOCR文字识别C#部署-1

  • Post author:
  • Post category:其他



  1. PaddleOCR文字识别C#部署-1

  2. PaddleOCR文字识别C#部署-2

  3. PaddleOCR文字识别C#部署-3


审核人审核文章带点脑子,没办法审核人不带脑子文章突然审核不过,删去了上面配置过程自行搜索把。


全文链接:




PaddleOCR-Csharp-Deploy

在这里插入图片描述



号外 号外 最新最强更新…

在这里插入图片描述

#include "glog/logging.h"
#include "omp.h"
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <chrono>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <vector>

#include <cstring>
#include <fstream>
#include <numeric>

#include <include/config.h>
#include <include/ocr_det.h>
#include <include/ocr_rec.h>
//#include"ocr.h"
using namespace std;
using namespace cv;
using namespace PaddleOCR;

/*
1.返回框选字体;
2.返回解析字符串
*/

extern "C" __declspec(dllexport) cv::Mat * LoadModel(char* input, int width, int height);
__declspec(dllexport) cv::Mat* LoadModel(char* input, int width, int height) 
{

  OCRConfig config("D:\\Paddle\\PaddleOCR\\deploy\\cpp_infer\\tools\\config.txt");

  config.PrintConfigInfo();

  //std::string img_path(argv[2]);
  //cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR);
  cv::Mat srcimg(height, width, CV_8UC3, input);

  DBDetector det(config.det_model_dir, config.use_gpu, config.gpu_id,
                 config.gpu_mem, config.cpu_math_library_num_threads,
                 config.use_mkldnn, config.max_side_len, config.det_db_thresh,
                 config.det_db_box_thresh, config.det_db_unclip_ratio,
                 config.visualize, config.use_tensorrt, config.use_fp16);

  Classifier *cls = nullptr;
  if (config.use_angle_cls == true) {
    cls = new Classifier(config.cls_model_dir, config.use_gpu, config.gpu_id,
                         config.gpu_mem, config.cpu_math_library_num_threads,
                         config.use_mkldnn, config.cls_thresh,
                         config.use_tensorrt, config.use_fp16);
  }

  CRNNRecognizer rec(config.rec_model_dir, config.use_gpu, config.gpu_id,
                     config.gpu_mem, config.cpu_math_library_num_threads,
                     config.use_mkldnn, config.char_list_file,
                     config.use_tensorrt, config.use_fp16);

  auto start = std::chrono::system_clock::now();
  std::vector<std::vector<std::vector<int>>> boxes;
  // 检测
  cv::Mat ocrImage;
  ocrImage= det.Run(srcimg, boxes);
  // 识别
  rec.Run(boxes, srcimg, cls);
  auto end = std::chrono::system_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  std::cout << "Cost  "
            << double(duration.count()) *
                   std::chrono::microseconds::period::num /
                   std::chrono::microseconds::period::den
            << "s" << std::endl;
  cv::Mat gray;
  cv::cvtColor(srcimg, gray, COLOR_BGR2GRAY);
  return new cv::Mat(ocrImage);
}



3. 使用C#编写界面调用DLL实现文字识别

将动态链接库

libiomp5md.dll 、mkldnn.dll、mklml.dll、ocr_system.dll、opencv_world451.dll、paddle_fluid.dll

添加到c# 项目中.
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using OpenCvSharp;
using OpenCvSharp.XImgProc;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("ocr_system.dll", EntryPoint = "LoadModel", SetLastError = true, CharSet = CharSet.Ansi)]
        static extern IntPtr LoadModel(byte[] input, int height, int width);  //out IntPtr seg_res
        private void button1_Click(object sender, EventArgs e)
        {

            string image_path = "D://Paddle//PaddleOCR//doc//imgs//1.jpg";
            Bitmap bmp = new Bitmap(image_path);
            //pictureBox1.Image = bmp;
            int stride;
            byte[] source = GetBGRValues(bmp, out stride);
            IntPtr seg_img = LoadModel(source, bmp.Width, bmp.Height);  //out seg_img
            Mat img = new Mat(seg_img);
            Bitmap seg_show = new Bitmap(img.Cols, img.Rows, (int)img.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, img.Data);

            pictureBox1.Image = seg_show;
        }
        // 将Btimap类转换为byte[]类函数
        public static byte[] GetBGRValues(Bitmap bmp, out int stride)
        {
            var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            var bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
            stride = bmpData.Stride;
            var rowBytes = bmpData.Width * Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
            var imgBytes = bmp.Height * rowBytes;
            byte[] rgbValues = new byte[imgBytes];
            IntPtr ptr = bmpData.Scan0;
            for (var i = 0; i < bmp.Height; i++)
            {
                Marshal.Copy(ptr, rgbValues, i * rowBytes, rowBytes);
                ptr += bmpData.Stride;
            }
            bmp.UnlockBits(bmpData);
            return rgbValues;
        }
    }
}

在这里插入图片描述


特别注意:



若是与工业相机连接,需要注意图像采集的通道数,

PaddleOCR

不支持四通道数据输入。如需使用需要将四通道转为

3

通道

RGB。



在这里插入图片描述





号外 号外 重点来喽!!!!


  • 全部提取C++ 识别的文字;

  • 全部提取 box框的坐标点;

  • 全部提取每个字符串的score;

在这里插入图片描述


提醒:部署以及源代码等相关问题请微信咨询 …


调bug勿扰
在这里插入图片描述


参考:


  1. PaddleOCR 文字识别 c++ win10 安装使用教程

  2. Windows 下 PaddleOCR C++推理部署 cmake vs2017

在这里插入图片描述



版权声明:本文为ch_ccc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。