Unity使用OpenCvSharp进行人脸识别
来源:互联网 发布:智研数据研究中心整理 编辑:程序博客网 时间:2024/05/16 01:59
在这里我们使用的是OpenCvSharp的2.4.10版本,对应OpenCV的2.4.10版本
首先将opencv_*2410.dll和OpenCvSharpExtern.dll复制到Unity工程根目录,再将OpenCvSharp.Blob.dll,OpenCvSharp.CPlusPlus.dll,OpenCvSharp.dll复制到Unity的Assets/Plugins目录
using UnityEngine;using System.Collections;using OpenCvSharp.CPlusPlus;using OpenCvSharp;using System;public class FaceDetection : MonoBehaviour{ public int Width = 640; public int Height = 480; public int FPS = 30; public bool Mirror = false; public Camera Camera; Camera _Camera; VideoCapture video; public int VideoIndex = 1; Texture2D texture; CascadeClassifier cascade; public GameObject Object; // Use this for initialization void Start() { // カメラを列挙する // 使いたいカメラのインデックスをVideoIndexに入れる // 列挙はUnityで使うのはOpenCVだけど、インデックスは同じらしい var devices = WebCamTexture.devices; for (int i = 0; i < devices.Length; i++) { print(string.Format("index {0}:{1}", i, devices[i].name)); } // ビデオの設定 video = new VideoCapture(VideoIndex); video.Set(CaptureProperty.FrameWidth, Width); video.Set(CaptureProperty.FrameHeight, Height); print(string.Format("{0},{1}", Width, Height)); // 顔検出器の作成 cascade = new CascadeClassifier(Application.dataPath + @"/haarcascade_frontalface_alt.xml"); // テクスチャの作成 texture = new Texture2D(Width, Height, TextureFormat.RGB24, false); renderer.material.mainTexture = texture; // 変換用のカメラの作成 _Camera = GameObject.Find(Camera.name).camera; print(string.Format("({0},{1})({2},{3})", Screen.width, Screen.height, _Camera.pixelWidth, _Camera.pixelHeight)); } // Update is called once per frame void Update() { using (Mat image = new Mat()) { // Webカメラから画像を取得する video.Read(image); // 顔を検出する var faces = cascade.DetectMultiScale(image); if (faces.Length > 0) { var face = faces[0]; // 顔の矩形を描画する image.Rectangle(face, new Scalar(255, 0, 0), 2); // 中心の座標を計算する var x = face.TopLeft.X + (face.Size.Width / 2); var y = face.TopLeft.Y + (face.Size.Height / 2); // オブジェクトを移動する if (Object != null) { Object.transform.localPosition = Vector2ToVector3(new Vector2(x, y)); } } // OpenCVのデータがBGRなのでRGBに変える // Bitmap形式に変えてテクスチャに流し込む //using(var cvtImage = image.CvtColor( ColorConversion.BgrToRgb )){ texture.LoadImage(image.ImEncode(".png")); texture.Apply(); //} } } void OnApplicationQuit() { if (video != null) { video.Dispose(); video = null; } } /// <summary> /// OpenCVの2次元座標をUnityの3次元座標に変換する /// </summary> /// <param name="vector2"></param> /// <returns></returns> private Vector3 Vector2ToVector3(Vector2 vector2) { if (Camera == null) { throw new Exception(""); } // スクリーンサイズで調整(WebCamera->Unity) vector2.x = vector2.x * Screen.width / Width; vector2.y = vector2.y * Screen.height / Height; // Unityのワールド座標系(3次元)に変換 var vector3 = _Camera.ScreenToWorldPoint(vector2); // 座標の調整 // Y座標は逆、Z座標は0にする(Xもミラー状態によって逆にする必要あり) vector3.y *= -1; vector3.z = 0; return vector3; }}
1 0
- Unity使用OpenCvSharp进行人脸识别
- Unity使用OpenCvSharp人脸识别
- Unity使用OpenCvSharp人脸识别
- opencvsharp + unity进行轮廓凸出(ConvexityHull2)
- unity 使用百度语音进行语音识别
- 使用opencv进行人脸识别
- 使用Haar特征进行人脸识别
- 使用opencv进行人脸识别
- Java使用OpenCV进行人脸识别
- 怎样使用OpenCV进行人脸识别
- 怎样使用OpenCV进行人脸识别
- 使用FaceDetection进行人脸识别
- 使用CoreImage进行人脸识别
- 怎样使用OpenCV进行人脸识别
- 使用CoreImage进行人脸识别
- 怎样使用OpenCV进行人脸识别
- 怎样使用OpenCV进行人脸识别
- 怎样使用OpenCV进行人脸识别
- Activating a Performance Trace
- 设置session时长,超时间需重新登录
- (hibernate之三)session的delete方法(转的学习用)
- string类的构造函数、析构函数、拷贝构造函数和赋值函数
- WebRtc 的VAD算法解析
- Unity使用OpenCvSharp进行人脸识别
- linux shell随机数获取的方法
- iOS开发ASIHTTPRequest身份验证【5】
- std::vector的reserve和resize的区别
- 高并发解决方案
- Memcached Java Client API 详解
- Android中的Fragment使用详解
- android ActionBar
- 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置