找回密码
 立即注册

QQ登录

只需一步,快速开始

13573156105

注册会员

6

主题

20

帖子

190

积分

注册会员

积分
190
13573156105
注册会员   /  发表于:2019-7-24 16:51  /   查看:3321  /  回复:6
请教版主,我在进行批量OCR识别时,内存耗尽程序自动退出,监控计算机内存到5G多,大概处理了100多张图片,请您看看我的程序有什么问题,非常感谢!!!

            try
            {
                IOcrEngine _ocrEngine;
                // 初始化OCR引擎
                _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, false);
                // 启动OCR引擎
                _ocrEngine.Startup(null, null, null, Application.StartupPath.ToString() + "\\License\\OcrProfessionalRuntime");
                //将语言设置为中文简体
                _ocrEngine.LanguageManager.EnableLanguages(new string[] { "zh-Hans", "en" });//zh-Hans   en

                DirectoryInfo directoryInfo = new DirectoryInfo(this.textBox1.Text);
                string[] myDirectory = System.IO.Directory.GetDirectories(this.textBox1.Text);
                int count = myDirectory.Length; int now = 0;

                // 加载图像
                RasterCodecs codecs = new RasterCodecs();
                codecs.ThrowExceptionsOnInvalidImages = true;
                RasterImage image;
                string mydanghao = ""; string filepath = ""; string content = "";
                DirectoryInfo folder; StreamWriter sr;  
                OcrZone ocrZone = new OcrZone();
                ocrZone.ZoneType = OcrZoneType.Text;
                foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories())
                {
                    now = now + 1;
                    mydanghao = childDirectoryInfo.Name.ToString();
                    filepath = this.textBox2.Text.Trim() + "\\" + mydanghao + ".txt";
                    // 创建文件
                    content = "";
                    folder = new DirectoryInfo(this.textBox1.Text.Trim() + "\\" + mydanghao);                    
                    foreach (FileInfo file in folder.GetFiles("*.*"))
                    {
                        string filepath1 = this.textBox1.Text.Trim() + "\\" + mydanghao + "\\" + file.Name;
                        image = codecs.Load(filepath1);
                        using (IOcrPage ocrPage = _ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose))
                        {
                            // Clear all the zones in the page
                            ocrPage.Zones.Clear();
                            // Add our field zone
                            //Leadtools.Forms.LogicalRectangle
                            ocrZone.Bounds = new Leadtools.Forms.LogicalRectangle(new LeadRect(0, 0, image.Width, image.Height));
                            ocrPage.Zones.Add(ocrZone);
                            // Recognize the page. This will only recognize the zone we added
                            ocrPage.Recognize(null);
                            string result = ocrPage.GetText(0).Trim();
                            if (result.Length > 0)
                            {
                                result = result.Replace("\n", "").Replace("\r", "");
                                content = content + "\r\n" + result;
                            }                           
                            ocrPage.Dispose();
                        }                        
                        image.Dispose();image = null;
                    }                     
                    if (File.Exists(filepath)) File.Delete(filepath);            
                    sr = File.CreateText(filepath);
                    sr.WriteLine(content);//写入文本文件
                    sr.Dispose();
                    sr.Close();
                }
                MessageBox.Show("已成功完成OCR识别!\r\n应识别文件夹 " + count.ToString() + " 个,实际识别 " + now.ToString() + " 个", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // 关闭OCR引擎
                _ocrEngine.Shutdown();
                Process.Start(this.textBox2.Text.Trim());
            }
            catch (Exception ex)//如果出现错误则执行错误处理语句
            {
                MessageBox.Show(ex.ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

6 个回复

倒序浏览
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-7-24 17:23:39
沙发
你好,只看这个代码没法判断问题,我建议你将项目上传上来,同时附上重现问题的步骤,我帮你验证一下问题的原因
回复 使用道具 举报
13573156105
注册会员   /  发表于:2019-7-25 09:10:24
板凳
谢谢版主,项目内容也是那些代码,感觉下面图像相关内存释放没起作用。
ocrPage.Dispose();
image.Dispose();image = null;

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-7-25 14:24:28
地板
好的,我先测试一下给你结果
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-7-25 17:48:12
5#
目前我这边只有V20把版本的开发包,没有测试出内存增高的问题。
从代码上来看并没有什么问题,如果19有问题的话建议只能是升级开发包
回复 使用道具 举报
13573156105
注册会员   /  发表于:2019-7-26 10:24:27
6#
感谢版主!!
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-7-26 11:54:36
7#
不客气,因为你这边引用的dll和识别引擎库都没有包含在里面,我没办法完全重现你的问题。

如果还需要我这边协助的话,请将所用东西都放到项目中(包括可以重现问题的待识别图片),用以我这边重现问题。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部