raoyutian / PaddleOCRSharp

PaddleOCRSarp是一个基于百度飞桨PaddleOCR的C++代码修改并封装的.NET的OCR工具类库。包含文本识别、文本检测、表格识别功能。本项目针对小图识别不准的情况下做了优化,比飞桨原代码识别准确率有所提高。 包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别、竖排文本识别、长文本识别。同时支持多种文本检测。
Apache License 2.0
606 stars 99 forks source link

System.AccessViolationException:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏 #4

Closed Rocky830 closed 1 year ago

Rocky830 commented 2 years ago

在多次调用后出现如标题所示异常提示,调试追踪到namespace PaddleOCRSharp中PaddleOCREngine类出错,出错行: int textCount = Detect(Engine, imagefile, out ptrResult);,其中监控方法变量值 Engine:0x0000023efee5e780 imagefile:"C:\Users\aaa\AppData\Local\Temp\26dc935f-5eac-4e80-aa49-e58936246a26.bmp" ptrResult:0x0000000000000000

怀疑是指针出问题,但能力有限,未能继续分析下去.......

以下是库中的部分代码 [DllImport("PaddleOCR.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] internal static extern int Detect(IntPtr engine, string imagefile, out IntPtr result);

    /// <summary>
    /// 对图像文件进行文本识别
    /// </summary>
    /// <param name="imagefile">图像文件</param>
    /// <returns></returns>
    public OCRResult DetectText(string imagefile)
    {
        if (!System.IO.File.Exists(imagefile)) throw new Exception($"文件{imagefile}不存在");
        IntPtr ptrResult;
        int textCount = Detect(Engine, imagefile, out ptrResult);
raoyutian commented 2 years ago

建议识别引擎全局初始化一次即可。不要频繁初始化和销毁重建

laosanyuan commented 2 years ago

我也遇到了这个问题,且只初始化过一次。这个有办法 解决 吗

qing130 commented 2 years ago

建议识别引擎全局初始化一次即可。不要频繁初始化和销毁重建

识别引擎全局初始化一次,还是会出现以上的情况: System.AccessViolationException:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏

woaidianqian commented 1 year ago

建议识别引擎全局初始化一次即可。不要频繁初始化和销毁重建

识别引擎全局初始化一次,还是会出现以上的情况: System.AccessViolationException:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏

` using (var engine0 = new PaddleOCREngine(config, oCRParameter)) { // Insert code here to work with the Bitmap object ocrResult = engine0.DetectText(pngFile);

                        if (ocrResult != null)

                        {

                            text2 = ocrResult.Text;

                        }
                    }

` 用这个代码就可以,不要用全局初始化,也不要用多线程。

ZGGSONG commented 9 months ago

建议识别引擎全局初始化一次即可。不要频繁初始化和销毁重建

识别引擎全局初始化一次,还是会出现以上的情况: System.AccessViolationException:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏

` using (var engine0 = new PaddleOCREngine(config, oCRParameter)) { // Insert code here to work with the Bitmap object ocrResult = engine0.DetectText(pngFile);

                        if (ocrResult != null)

                        {

                            text2 = ocrResult.Text;

                        }
                    }

` 用这个代码就可以,不要用全局初始化,也不要用多线程。

https://github.com/raoyutian/PaddleOCRSharp/issues/28#issuecomment-1813961214