[英]Face tracker with Unity
I would like to program a face tracker and recognizer with OpenCV plus Unity.我想用 OpenCV 和 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? ”。有没有人有解决这个问题的办法?
I presume that you are talking about this tutorial:我假设您正在谈论本教程:
Advanced Tutorial-OpenCV in Unity进阶教程-Unity中的OpenCV
Change:改变:
cascade = new CascadeClassifier(Application.dataPath+@"haarcascade_frontalface_default.xml");
to至
cascade = new CascadeClassifier(System.IO.Path.Combine(Application.dataPath, "haarcascade_frontalface_default.xml"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.