I would like to program a face tracker and recognizer with OpenCV plus Unity. This is the code I am using for face tracking
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OpenCvSharp;
using OpenCvSharp.Tracking;
using OpenCvSharp.Aruco;
using OpenCvSharp.Detail;
using OpenCvSharp.Face;
using OpenCvSharp.Flann;
using OpenCvSharp.ML;
using OpenCvSharp.Util;
using OpenCvSharp.XFeatures2D;
public class FaceDector : MonoBehaviour
{
WebCamTexture _webCamTexture;
CascadeClassifier cascade;
void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
_webCamTexture = new WebCamTexture(devices[0].name);
_webCamTexture.Play();
cascade = new CascadeClassifier(Application.dataPath+@"haarcascade_frontalface_default.xml");
}
voidUpdate()
{
GetComponent<Renderer>() .material.mainTexture = _webCamTexture;
Mat frame = OpenCvSharp.Unity.TextureToMat(_webCamTexture);
findNewFace(frame);
}
void findNewFace(Mat frame)
{
var faces = cascade.DetectMultiScale(frame, 1.1, 2, HaarDetectionType.ScaleImage);
if(faces.Length >= 1)
{
Debug.Log(faces [0].Location);
}
}
}
but there is always the error message
FileNotFoundException: "C:/Users/bomba/Documents/Eigene Spiele/I.B.I.S/Assetshaarcascade_frontalface_default.xml"not found
OpenCvSharp.CascadeClassifier..ctor (System.String fileName) (at Assets/OpenCV+Unity/Assets/Scripts/OpenCvSharp/modules/objdetect/CascadeClassifier.cs:40)
FaceDector.Start () (at Assets/FaceDector.cs:24)
and then every frame
NullReferenceException: Object reference not set to an instance of an object
FaceDector.findNewFace (OpenCvSharp.Mat frame) (at Assets/FaceDector.cs:36)
FaceDector.Update () (at Assets/FaceDector.cs:31)
". Does anyone have a solution for the problem?