Emgu CV内存在加载超过1000个训练图像时泄漏

时间:2016-08-19 13:17:19

标签: c# emgucv

我只是不知道在这个方法LoadFaceRecognition中做什么, 当训练图像少于800个训练图像时,此方法可以平滑地加载训练图像,但是一旦我添加图像,数据加载就会停止执行并且内存消耗会扩大。请帮帮我这个

训练imga是灰度图像

这是LoadFaceRecognition,

private bool LoadFaceRecognition(string Training_Folder, string TrainedFaces) { 

  if (File.Exists(Training_Folder + TrainedFaces + "/TrainedLabels.xml"))
    {
        try
        {
            Names_List_Faces.Clear();
            trainingImages_Faces.Clear();
            FileStream filestream = File.OpenRead(Training_Folder + TrainedFaces + "/TrainedLabels.xml");

            long filelength = filestream.Length;
            byte[] xmlBytes = new byte[filelength];
            filestream.Read(xmlBytes, 0, (int)filelength);
            filestream.Close();

            MemoryStream xmlStream = new MemoryStream(xmlBytes);
            XmlReader xmlreader = XmlTextReader.Create(xmlStream);

            while (xmlreader.Read())
            {
                if (xmlreader.IsStartElement())
                {
                    switch (xmlreader.Name)
                    {
                        case "NAME":
                            if (xmlreader.Read())
                            {
                                //Names_List_ID.Add(Names_List.Count); //0, 1, 2, 3....
                                Names_List_Faces.Add(xmlreader.Value.Trim());
                                NumLabels_Faces += 1;
                            }
                            break;
                        case "FILE":
                            if (xmlreader.Read())
                            {
                                //PROBLEM HERE IF TRAININGG MOVED
                                trainingImages_Faces.Add(new Image<Gray, byte>(Training_Folder + "/TrainedFaces//" + xmlreader.Value.Trim()));

                            }
                            break;
                    }
                }
            }
            ContTrain_Faces = NumLabels_Faces;
            if (trainingImages_Faces.ToArray().Length != 0)
            {
                recognizer_Faces = new EORecognizer(trainingImages_Faces.ToArray(), Names_List_Faces.ToArray(), Eigen_Threshold, ref termCrit_Faces);
                return true;

            }
            else return false;
        }
        catch (Exception ex)
        {
            Error = ex.ToString();
            return false;
        }
    }
    else return false;
}

1 个答案:

答案 0 :(得分:0)

你在那里缺少一些Dispose或使用语句。

您需要释放一些正在使用的对象的资源,例如Strems。

尝试使用以下代码:

{{1}}