1

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?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Bombastiko
  • 11
  • 1
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Apr 03 '22 at 10:11

1 Answers1

1

I presume that you are talking about this tutorial:

Advanced Tutorial-OpenCV in Unity

Change:

cascade = new CascadeClassifier(Application.dataPath+@"haarcascade_frontalface_default.xml");

to

cascade = new CascadeClassifier(System.IO.Path.Combine(Application.dataPath, "haarcascade_frontalface_default.xml"));