I'm working on face recognition using a web camera. The system will continuously process the data each time a face is detected. I'm using EmguCV
, an OpenCV
wrapper. The code is running and able to process the data. But at one time, the system is throwing out System.AccessViolationException
exception when executing Emgu.CV.Face.FaceRecognizer.Predict(IInputArray image)
function, and
it always stops there.
Here's a part of the code that causing the problem:
public void recognizeUser(Bitmap userImage, out string label, out int confidentLevel)
{
Image<Gray, byte> imgToRecognize = new Image<Gray, byte>(userImage).Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);
Image<Gray, byte> grayFrame = imgToRecognize.Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);
var lbphResult = _lbphRecognizer.Predict(imgToRecognize); // Exception occurs here.
label = lbphResult.Label.ToString();
confidentLevel = (int)lbphResult.Distance;
}
Here's the error I got:
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
Here's the stacktrace:
at Emgu.CV.ContribInvoke.CvFaceRecognizerPredict(IntPtr recognizer, IntPtr image, Int32& label, Double& distance)
at Emgu.CV.Face.FaceRecognizer.Predict(IInputArray image)
Is this a kind of bug or am I missing something? Or is it because of so many data are being proccessed at one time? Any help would be greatly appreciated.
Updated 19/12/2017: Here's how I capture the data:
private void PerformDetection()
{
while (true)
{
DetectionResult[] detectionResult = detection.DoDetection(detectionEngineId); // Capture the face
foreach (var detResult in detectionResult)
{
string label = "0";
int confidentLevel = 0;
lock (detResult)
{
if (!IsTrainRecognition)
{
Bitmap imgToRecognize = (Bitmap)detResult.FaceImage.Clone();
_recognition.recognizeUser(imgToRecognize, out label, out confidentLevel); // Call the function above.
if (confidentLevel < 40)
{
IsTrainRecognition = true;
// Start training new face.
}
}
else
{
// Keep training until 10 image are trained and updated in YAML file.
}
// Saving the detection data in local database.
}
}
}
}