hi guys,
i am doing now face detection in unity3d and for that i followed Kenny Allau's tutorial to have face detect function in my laptop.
http://www.kennyallau.com/single-post/2017/06/05/Example-face-tracker-and-pedestrian-detector-using-Emgu-CV-with-Unity-3D-Part-I
And at the first time it seems to work quite good. But after about 3 min my laptop became quite slow and it didn't work anymore. The reason was memory leak.
This is actually my guess :
var faces = _cascadeClassifier.DetectMultiScale(grayFrame, 1.1, 4, new System.Drawing.Size(frameWidth/8, frameHeight/8));
this part of code takes memory progressively but doesn't give it back.
Maybe is there anyone who can solve this problem or knows something?
here is whole code that i used,
//Face detection using HAAR Cascade
using UnityEngine;
using System.IO;
using Emgu.CV;
using Emgu.CV.Structure;
public class faceDetect : MonoBehaviour {
private int frameWidth;
private int frameHeight;
private VideoCapture cvCapture;
private CascadeClassifier _cascadeClassifier;
private Image currentFrameBgr;
public Material mt;
void Start () {
cvCapture = new VideoCapture(0);
_cascadeClassifier = new CascadeClassifier(Application.dataPath + "/haarcascade_frontalface_alt.xml");
frameWidth = (int)cvCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth);
frameHeight = (int)cvCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight);
cvCapture.Start();
}
void Update () {
faceDetector();
}
private void faceDetector()
{
currentFrameBgr = cvCapture.QueryFrame().ToImage();
Texture2D tex = new Texture2D(640, 480);
if (currentFrameBgr != null)
{
Image grayFrame = currentFrameBgr.Convert();
var faces = _cascadeClassifier.DetectMultiScale(grayFrame, 1.1, 4, new System.Drawing.Size(frameWidth/8, frameHeight/8));
foreach (var face in faces)
{
currentFrameBgr.Draw(face, new Bgr(0, 255, 0), 3);
}
//Convert this image into Bitmap, the pixel values are copied over to the Bitmap
currentFrameBgr.ToBitmap();
MemoryStream memstream = new MemoryStream();
currentFrameBgr.Bitmap.Save(memstream, currentFrameBgr.Bitmap.RawFormat);
tex.LoadImage(memstream.ToArray());
mt.mainTexture = tex;
}
}
private void OnDestroy()
{
//release from memory
cvCapture.Dispose();
cvCapture.Stop();
}
}
↧