无法从“ Emgu.CV.Image <Emgu.CV.Structure.Gray,字节> []”转换为“ Emgu.CV.IInputArray”

时间:2019-07-12 07:01:43

标签: c# emgucv face-recognition

我正在尝试构建一个简单的面部/眼睛检测Windows窗体应用程序。我正在使用最新的EmguCV(4.1.0),但是当我尝试运行下面的代码时,出现标题错误。第二个错误涉及图像ID。

// Declaration at the beginning of the program
FaceRecognition = new EigenFaceRecognizer(80, double.PositiveInfinity); 
FaceDetection = new CascadeClassifier(//Path to .xml haarcascade file);
Faces = new List<Image<Gray, byte>>();
IDs = new List<int>();
Webcam = new VideoCapture(0);
Frame = new Mat();

// Some code and some code

// Image detection and feeding it into the FaceRecognizer

Webcam.Retrieve(Frame);
var imageFrame = Frame.toImage<Gray, byte>();

// Some code (checking if imageFrame might be null etc.)
if (faces.Count() > 0){
   var faces = FaceDetection.DetectMultiScale(imageFrame, 1.3, 5);
   var processedImage = imageFrame.Copy(faces[0]).Resize(128, 150, 
   Emgu.CV.CvEnum.Inter.Cubic);
   Faces.Add(processedImage);
   IDs.Add(Convert.ToInt32(tbID.Text)); // ID for photo recognition (not really 
   neccesary)
}

else
{
   // First method
   var trainingFaces = (IInputArray) Faces; // Can't cast from List<Image<Gray, byte>> to IInputArray
   var trainingIDs = (IInputArray) IDs; // Can't cast from int[] to IInputArray
   FaceRecognition.Train(trainingFaces, trainingIDs);

   // Second method
   var trainingFaces = Faces.toArray();
   var trainingIDs = IDs.toArray();
   FaceRecognition.Train(trainingFaces, trainingIDs); // Can't convert from List<Image<Gray, byte>> to Emgu.CV.IInputArray.

这两种方法都出错,我真的不知道如何解决。

2 个答案:

答案 0 :(得分:1)

将训练面部图像保留为Mat列表

List<Mat> TrainingFaces = new List<Mat>();
List<int> FaceIDs = new List<int>();

由于IInputArray可以构造为VectorOfMat(Mat [])或VectorOfInt(Int32) 然后可以将其作为火车参数传递:

FaceRecognition.Train( new VectorOfMat(TrainingFaces.ToArray()) , new VectorOfInt( FaceIDs.ToArray()) );

答案 1 :(得分:-1)

升级或降级您的 Emgu Lib

<package id="Emgu.CV" version="3.2.0.2721" targetFramework="net48" />

我的代码现在和你的代码一样工作。