1.模型下载

https://gitee.com/paddlepaddle/PaddleOCR/blob/dygraph/doc/doc_ch/models_list.md

2.PaddleOCRSDK+PaddleOCRRuntime_x64

这两个组合比较原生,免费版不支持调动GPU

 string path = "模型路径";
 string rec = Path.Combine(path, "rec");
 string det = Path.Combine(path, "det");
 string cls = Path.Combine(path, "cls");
 string key = Path.Combine(path, "ppocr_keys.txt");
 var config = new PaddleOCRSDK.OCRParameter()
 {
     use_gpu = true,
     gpu_id = 0,
 };
 PaddleOCRSDK.InitParamater initParamater = new PaddleOCRSDK.InitParamater()
 {
     cls_infer = cls,
     det_infer = det,
     keyFile = key,
     rec_infer = rec,
     paraType = PaddleOCRSDK.EnumParaType.Class,
     ocrpara = config,
 };
 PaddleOCRSDK.OCRService oCRService = new PaddleOCRSDK.OCRService();
 oCRService.Init(initParamater);

 byte[] sampleImageData = File.ReadAllBytes("图片路径");

 var result = oCRService.Detect(sampleImageData);

3.Sdcb.PaddleOCR

这个包只试了一下文字识别,表格没进行测试

在我使用官方模型的时候识别出来是乱码,不知怎么回事不想深究,这个库自带的够我的应用场景

传送门:https://www.cnblogs.com/sdcb/p/20230724-paddlesharp-in-a-year.html

【需要先安装以下包】

(1) OpenCvSharp4.runtime.win

(2) Sdcb.PaddleInference

(3) Sdcb.PaddleInference.runtime.win64.mkl 或Sdcb.PaddleInference.runtime.win64.cuda(具体根据自身版本安装)

(4) Sdcb.PaddleOCR

(5) Sdcb.PaddleOCR.Models.Local

(5) Sdcb.PaddleOCR.Models.LocalV4

FullOcrModel model = LocalFullModels.ChineseV4;
byte[] sampleImageData = File.ReadAllBytes("图片路径");
//  PaddleDevice.Mkldnn()
// GPU换成这个 PaddleDevice.Gpu()  // 使用GPU的时候识别不出东西,原因未知(可能是我配置的环境有问题)
using (PaddleOcrAll all = new PaddleOcrAll(model, PaddleDevice.Mkldnn())
{
    AllowRotateDetection = true,
    Enable180Classification = false,
})
{
    // opencv还有其他读取图片的方式,根据场景替换
    using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color))
    {
        PaddleOcrResult result = all.Run(src);
        Trace.WriteLine("Detected all texts: \n" + result.Text);
        foreach (PaddleOcrResultRegion region in result.Regions)
        {
            // region 有矩形框信息和文本信息,不过矩形框信息有点不准;文字识别测试的时候很准
        }
    }
}

4.PaddleOCRSharp+Paddle.Runtime.win_x64

【传送门】PaddleOCRSharp: PaddleOCRSharp是一个基于百度飞桨PaddleOCR的C++代码修改并封装的.NET的OCR工具本地类库,可离线使用。包含文本识别、文本检测、表格识别功能。本项目针对小图识别不准的情况下做了优化,比飞桨原代码识别准确率有所提高。 包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别、竖排文本识别、长文本识别。同时支持多种文本检测。https://gitee.com/raoyutian/PaddleOCRSharp

NET框架下如何使用PaddleOCRSharp - 英田科技-明月心 - 博客园

【文本识别】默认的模型识别的结果不怎么准确,建议使用官方的server模型(CPU贼久)

// 配置
OCRModelConfig config = null!;
//使用自定义模型(在官网下载的模型)
//config = new OCRModelConfig();
//string modelPathroot = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ocrv4AImodel");
//config.rec_infer = Path.Combine(modelPathroot, "rec");    
//config.det_infer = Path.Combine(modelPathroot, "det");
//config.cls_infer = Path.Combine(modelPathroot, "cls");
//config.keys = Path.Combine(modelPathroot, "ppocr_keys.txt");
OCRParameter oCRParameter = new OCRParameter();
// 根据作者的介绍全局初始化一次就好,设置成静态好点,虽然我测试的时候遇到异常
using PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);
// 识别的结果
var res = engine.DetectStructure(image);

【表格识别】识别的不准,所以我用 通义的写了一个(识别效果嘎嘎好)

//模型配置
StructureModelConfig config = null!;
//config = new StructureModelConfig();
//string modelPathroot = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ocrv4AImodel");
//config.rec_infer = Path.Combine(modelPathroot, "rec");
//config.det_infer = Path.Combine(modelPathroot, "det");
//config.cls_infer = Path.Combine(modelPathroot, "cls");
//config.keys = Path.Combine(modelPathroot, "ppocr_keys.txt");
//config.table_char_dict_path = Path.Combine(modelPathroot, "table_structure_dict_ch.txt");
//config.table_model_dir = Path.Combine(modelPathroot, "table_LANet_infer");
//表格识别参数配置
StructureParameter structureParameter = new StructureParameter();
structureParameter.use_gpu = true;
//初始化表格识别引擎
PaddleStructureEngine engine = new PaddleStructureEngine(config, structureParameter);
//表格识别,返回结果是html格式的表格形式
string result = engine.StructureDetect(img);
//添加边框线
string css = "<style>table{ border-spacing: 0pt;} td { border: 1px solid black;}</style>";
result = result.Replace("<html>", "<html>" + css);

//保存到本地
File.WriteAllText("C:\\Users\\MaxSys\\Desktop\\test\\index.html", result);

【 通义有线表格识别】 

里面还有很多环境没写上去,根据报错提示,缺哪个安装哪个就行了

另外打包的时候modelscope这个包可能会打包不进去用这个命令可以打包进去

pyinstaller --noconfirm --onedir --console --collect-all "modelscope"  "main.py"

或者用auto-py-to-exe 这个界面化的包,在--collect-all填上modelscope也行

    # 表格扫描
    # pip install modelscope
    # 模型下载modelscope download --model iic/cv_dla34_table-structure-recognition_cycle-centernet --local_dir ./dir
    import json
    import sys
    from modelscope.pipelines import pipeline
    from modelscope.utils.constant import Tasks


    def converttable(imagepath, outfile):
        print(imagepath, outfile)
        table_recognition = pipeline(Tasks.table_recognition, model=r'./table_line_model', device='cuda')
        result = table_recognition(imagepath)
        rects = []
        for rect in result.values():
            for i in range(len(rect)):
                arr = []
                point = {'x': 0, 'y': 0}
                for j in range(len(rect[i])):
                    if j % 2 == 0:
                        point['x'] = float(rect[i][j])
                    else:
                        point['y'] = float(rect[i][j])
                        arr.append(point)
                        point = {'x': 0, 'y': 0}
                rects.append({'rect': arr})
        json_str = json.dumps(rects)
        with open(outfile, 'w') as f:
            f.write(json_str)


    if __name__ == '__main__':
        # 参数传递0 程序名;1 图片路径;2输出文件
        arguments = sys.argv
        converttable(arguments[1], arguments[2])

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐