• 如何使用Sdcb.PaddleOCR实现图片表格识别功能
  • 发布于 2个月前
  • 349 热度
    0 评论
效果
左图是表格图片识别后将其位置标注出的效果,右图是利用webBrowser1呈现的HTML效果,文本框中是表格识别后生成的HTML内容

开发环境
1.VS2022
2..net4.8
3.OpenCvSharp4
4.Sdcb.PaddleInference
5.Sdcb.PaddleOCR

代码
using OpenCvSharp.Extensions;
using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR.Models.Details;
using System;
using System.Collections.Generic;
using System.Drawing;
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 = "";
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            // 堆代码 duidaima.com
            pictureBox1.Image = null;
 
            img = ofd.FileName;
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);
        }
 
        TableRecognitionModel tableModel;
        PaddleOcrTableRecognizer tableRec;
 
        FullOcrModel model;
        PaddleOcrAll paddleOcr;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            string table_directoryPath = startupPath + "\\ch_ppstructure_mobile_v2.0_SLANet";
            string table_labelFilePath = startupPath + "\\table_structure_dict_ch.txt";
 
            tableModel = new FileTableRecognizationModel(table_directoryPath, table_labelFilePath);
            tableRec = new PaddleOcrTableRecognizer(tableModel);
 
            string detectionModelDir = startupPath + "\\ch_PP-OCRv3_det";
            string classificationModelDir = startupPath + "\\ch_ppocr_mobile_v2.0_cls";
            string recognitionModelDir = startupPath + "\\ch_PP-OCRv3_rec";
            string labelFilePath = startupPath + "\\ppocr_keys_v1.txt";
 
            model = FullOcrModel.FromDirectory(detectionModelDir, classificationModelDir, recognitionModelDir, labelFilePath, ModelVersion.V3);
 
            paddleOcr = new PaddleOcrAll(model, PaddleDevice.Mkldnn());
            paddleOcr.AllowRotateDetection = true; /* 允许识别有角度的文字 */
            paddleOcr.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
        }
 
        TableDetectionResult tableResult;
        private void button2_Click(object sender, EventArgs e)
        {
 
            if (pictureBox1.Image == null)
            {
                return;
            }
 
            Mat src = Cv2.ImRead(img);
            tableResult = tableRec.Run(src);
 
            List<TableCellBox> ltCellBox = tableResult.StructureBoxes;
 
            foreach (TableCellBox item in ltCellBox)
            {
                Scalar scalar = Scalar.RandomColor();
                Cv2.Rectangle(src, item.Rect, scalar);
            }
            //Cv2.ImShow("src", src);
            //Cv2.ImWrite("src.jpg", src);
            pictureBox1.Image = BitmapConverter.ToBitmap(src);
 
            //List<string> ltTags = tableResult.HtmlTags;
            //float score = tableResult.Score;
 
            paddleOcr.Detector.UnclipRatio = 1.2f;
            PaddleOcrResult result = paddleOcr.Run(src);
 
            src.Dispose();
 
            string html = tableResult.RebuildTable(result);
            textBox1.Text = html;
            System.IO.File.WriteAllText("table.html", html);
 
            webBrowser1.DocumentText = html;
        }
    }
}

用户评论