gpt4 book ai didi

c# - 无法在OpenCV中分配字节

转载 作者:行者123 更新时间:2023-12-02 16:44:33 25 4
gpt4 key购买 nike

我正在使用Emgu.CV从视频文件中提取帧并从每个帧中提取人脸,问题是该应用程序在一段时间后耗尽了内存。我想我释放内存并处理我创建的每个对象。这里是方法,任何优化,想法将不胜感激。

public List<FileItem> ExtractFacesFromImage(FileItem selectedFrame, string outputFolderPath, int minNeighbors, double scaleFactor, int widthIncrement = 80, int heightIncrement = 102)
{
lock (lockObj)
{
_currentFrame = new Image<Bgr, byte>(selectedFrame.Path);
_gray = _currentFrame.Convert<Gray, Byte>();
var result = new List<FileItem>();

using (_face = new HaarCascade(_xmlFilePath))
{
if (_gray != null)
{
var facesDetected = _gray.DetectHaarCascade(_face, scaleFactor, minNeighbors,Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,new Size(20, 20));

for (int index = 0; index < facesDetected[0].Length; index++)
{
var imgName = SaveExtractedFace(outputFolderPath, facesDetected,
index, widthIncrement, heightIncrement);
result.Add(new FileItem
{
IsSelected = true,
Path = imgName,
Name = Path.GetFileNameWithoutExtension(imgName)
});
}
DisposeObject(_gray);
DisposeObject(_currentFrame);
}
return result;
}
}
}

private void DisposeObject(IDisposable disposable)
{
if (disposable != null)
{
disposable.Dispose();
disposable = null;
}
}

private string SaveExtractedFace(string outputFolderPath, MCvAvgComp[][] facesDetected, int index, int widthIncrement, int heightIncrement)
{
var f = facesDetected[0][index];

var cropRectangle = GetNewRectangle(f.rect, _currentFrame.Width, _currentFrame.Height, widthIncrement, heightIncrement);

_tempImage = _currentFrame.Copy(cropRectangle);
string imgName = outputFolderPath + "\\" + UniqueNameManager.Generate() + ".jpg";
_tempImage.Save(imgName);
DisposeObject(_tempImage);
return imgName;
}

最佳答案

请尝试运行此代码,而不要使用全局变量或常规变量。

public List<FileItem> ExtractFacesFromImage(FileItem selectedFrame, string outputFolderPath,
int minNeighbors, double scaleFactor, int widthIncrement = 80, int heightIncrement = 102)
{
lock (lockObj)
{
var currentFrame = new Image<Bgr, byte>(selectedFrame.Path);
var gray = _currentFrame.Convert<Gray, Byte>();
var result = new List<FileItem>();

using (_face = new HaarCascade(_xmlFilePath))
{
if (gray != null)
{
var facesDetected = gray.DetectHaarCascade(_face, scaleFactor, minNeighbors,Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,new Size(20, 20));

for (int index = 0; index < facesDetected[0].Length; index++)
{
var imgName = SaveExtractedFace(outputFolderPath, facesDetected, index, widthIncrement,
heightIncrement);
result.Add(new FileItem
{
IsSelected = true,
Path = imgName,
Name = Path.GetFileNameWithoutExtension(imgName)
});
}

}
return result;
}
}
}

private string SaveExtractedFace(string outputFolderPath, MCvAvgComp[][] facesDetected, int index, int widthIncrement, int heightIncrement)
{
var f = facesDetected[0][index];

var cropRectangle = GetNewRectangle(f.rect, _currentFrame.Width, _currentFrame.Height, widthIncrement, heightIncrement);

_tempImage = _currentFrame.Copy(cropRectangle);
string imgName = outputFolderPath + "\\" + UniqueNameManager.Generate() + ".jpg";
_tempImage.Save(imgName);
DisposeObject(_tempImage);
return imgName;
}

关于c# - 无法在OpenCV中分配字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com