这是最近做的一个工作,由于要调用电脑摄像头实时检测人眼主要是检测闭眼睁眼
**
1.工具
**
1.1unity(作者用的是2020.3.25 大部分版本都ok
1.2opencv for unity(可从这个链接下载链接:https://pan.baidu.com/s/1Wti9m57-pe1et4U8LNgoCA 提取码:b0un
1.3haar模型
2.实现
2.1首先新建一个项目
然后将opencv for unity导入
2.2
新建一个脚本这里命名为EyeDetect
using OpenCVForUnity;
using OpenCVForUnityExample;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EyeDetect : MonoBehaviour
{
//WebCamTextureToMatExample webcamTexToMat = new WebCamTextureToMatExample();//获取摄像头画面的辅助类
//PoseEstimator poseEstimator = new PoseEstimator();
string face_Xml_path;//人脸识别训练数据文件XML的路径
string eye_Xml_path;//人眼识别训练数据文件XML的路径
Mat gray;//灰度图
MatOfRect faceRect;//识别到的人脸区域
CascadeClassifier classifier;//人脸识别分类器
public static int EyesChecked=0;//是否检测到眼睛
void Start()
{
//webcamTexToMat = transform.GetComponent<WebCamTexToMat>();//获取Quad上挂载的WebCamTextureToMatExample脚本组件
//poseEstimator = transform.GetComponent<PoseEstimator>();
face_Xml_path = Application.streamingAssetsPath + "/haarcascade_frontalface_alt2.xml";//获取人脸识别训练数据XML
eye_Xml_path = Application.streamingAssetsPath + "/haarcascade_eye.xml"