<div style="font-size:16px;">
<p>一、框架视图</p>
<p align="center"><img alt="" src=""></p>
<p>Main.png</p>
<p>二、关键代码</p>
<p>WebCamTextureARSample</p>
<p>using UnityEngine;</p>
<p>using System.Collections;</p>
<p>using System.Collections.Generic;</p>
<p>using UnityEngine.UI;</p>
<p>#if UNITY_5_3 || UNITY_5_3_OR_NEWER</p>
<p>using UnityEngine.SceneManagement;</p>
<p>#endif</p>
<p>using OpenCVForUnity;</p>
<p>using DlibFaceLandmarkDetector;</p>
<p>namespace DlibFaceLandmarkDetectorSample</p>
<p>{<!-- --></p>
<p>/// </p>
<p>/// Face tracker AR from WebCamTexture Sample.</p>
<p>/// This sample was referring to http://www.morethantechnical.com/2012/10/17/head-pose-estimation-with-opencv-opengl-revisited-w-code/</p>
<p>/// and use effect asset from http://ktk-kumamoto.hatenablog.com/entry/2014/09/14/092400</p>
<p>/// </p>
<p>[RequireComponent(typeof(WebCamTextureToMatHelper))]</p>
<p>public class WebCamTextureARSample : MonoBehaviour</p>
<p>{<!-- --></p>
<p>/// </p>
<p>/// The is showing face points.</p>
<p>/// </p>
<p>public bool isShowingFacePoints;</p>
<p>/// </p>
<p>/// The is showing face points toggle.</p>
<p>/// </p>
<p>public Toggle isShowingFacePointsToggle;</p>
<p>/// </p>
<p>/// The is showing axes.</p>
<p>/// </p>
<p>public bool isShowingAxes;</p>
<p>/// </p>
<p>/// The is showing axes toggle.</p>
<p>/// </p>
<p>public Toggle isShowingAxesToggle;</p>
<p>/// </p>
<p>/// The is showing head.</p>
<p>/// </p>
<p>public bool isShowingHead;</p>
<p>/// </p>
<p>/// The is showing head toggle.</p>
<p>/// </p>
<p>public Toggle isShowingHeadToggle;</p>
<p>/// </p>
<p>/// The is showing effects.</p>
<p>/// </p>
<p>public bool isShowingEffects;</p>
<p>/// </p>
<p>/// The is showing effects toggle.</p>
<p>/// </p>
<p>public Toggle isShowingEffectsToggle;</p>
<p>/// </p>
<p>/// The axes. 轴</p>
<p>/// </p>
<p>public GameObject axes;</p>
<p>/// </p>
<p>/// The head. 头部</p>
<p>/// </p>
<p>public GameObject head;</p>
<p>/// </p>
<p>/// The right eye.右眼</p>
<p>/// </p>
<p>public GameObject rightEye;</p>
<p>/// </p>
<p>/// The left eye.左眼</p>
<p>/// </p>
<p>public GameObject leftEye;</p>
<p>/// </p>
<p>/// The mouth. 嘴巴</p>
<p>/// </p>
<p>public GameObject mouth;</p>
<p>/// </p>
<p>/// The mouth particle system. 嘴部特效</p>
<p>/// </p>
<p>ParticleSystem[] mouthParticleSystem;</p>
<p>/// </p>
<p>/// The texture. 贴图</p>
<p>/// </p>
<p>Texture2D texture;</p>
<p>/// </p>
<p>/// The face landmark detector. 脸部识别</p>
<p>/// </p>
<p>FaceLandmarkDetector faceLandmarkDetector;</p>
<p>/// </p>
<p>/// The AR camera.</p>
<p>/// </p>
<p>public Camera ARCamera;</p>
<p>/// </p>
<p>/// The cam matrix.</p>
<p>/// </p>
<p>Mat camMatrix;</p>
<p>/// </p>
<p>/// The dist coeffs.</p>
<p>/// </p>
<p>MatOfDouble distCoeffs;</p>
<p>/// </p>
<p>/// The invert Y.</p>
<p>/// </p>
<p>Matrix4x4 invertYM;</p>
<p>/// </p>
<p>/// The transformation m.</p>
<p>/// </p>
<p>Matrix4x4 transformationM = new Matrix4x4 ();</p>
<p>/// </p>
<p>/// The invert Z.</p>
<p>/// </p>
<p>Matrix4x4 invertZM;</p>
<p>/// </p>
<p>/// The ar m.</p>
<p>/// </p>
<p>Matrix4x4 ARM;</p>
<p>/// </p>
<p>/// The ar game object.</p>
<p>/// </p>
<p>public GameObject ARGameObject;</p>
<p>/// </p>
<p>/// The should move AR camera.</p>
<p>/// </p>
<p>public bool shouldMoveARCamera;</p>
<p>/// </p>
<p>/// The 3d face object points.</p>
<p>/// </p>
<p>MatOfPoint3f objectPoints;</p>
<p>/// </p>
<p>/// The image points.</p>
<p>/// </p>
<p>MatOfPoint2f imagePoints;</p>
<p>/// </p>
<p>/// The rvec.</p>
<p>/// </p>
<p>Mat rvec;</p>
<p>/// </p>
<p>/// The tvec.</p>
<p>/// </p>
<p>Mat tvec;</p>
<p>/// </p>
<p>/// The rot m.</p>
<p>/// </p>
<p>Mat rotM;</p>
<p>/// </p>
<p>/// The web cam texture to mat helper.</p>
<p>/// </p>
<p>WebCamTextureToMatHelper webCamTextureToMatHelper;</p>
<p>/// </p>
<p>/// The shape_predictor_68_face_landmarks_dat_filepath.</p>
<p>/// </p>
<p>private string shape_predictor_68_face_landmarks_dat_filepath;</p>
<p>// Use this for initialization</p>
<p>void Start ()</p>
<p>{<!-- --></p>
<p>isShowingFacePointsToggle.isOn = isShowingFacePoints;</p>
<p>isShowingAxesToggle.isOn = isShowingAxes;</p>
<p>isShowingHeadToggle.isOn = isShowingHead;</p>
<p>isShowingEffectsToggle.isOn = isShowingEffects;</p>
<p>#if UNITY_WEBGL && !UNITY_EDITOR</p>
<p>StartCoroutine(DlibFaceLandmarkDetector.Utils.getFilePathAsync("shape_predictor_68_face_landmarks.dat", (result) => {<!-- --></p>
<p>shape_predictor_68_face_landmarks_dat_filepath = result;</p>
<p>Run ();</p>
<p>}));</p>
<p>#else</p>
<p>shape_predictor_68_face_landmarks_dat_filepath = DlibFaceLandmarkDetector.Utils.getFilePath ("shape_predictor_68_face_landmarks.dat");</p>
<p>Run ();</p>
<p>#endif</p>
<p>}</p>
<p>private void Run ()</p>
<p>{<!-- --></p>
<p>//set 3d face object points.</p>
<p>objectPoints = new MatOfPoint3f (</p>
<p>new Point3 (-31, 72, 86),//l eye</p>
<p>new Point3 (31, 72, 86),//r eye</p>
<p>new Point3 (0, 40, 114),//nose</p>
<p>new Point3 (-20, 15, 90),//l mouse</p>
<p>new Point3 (20, 15, 90),//r mouse</p>
<p>new Point3 (-69, 76, -2),//l ear</p>
<p>new Point3 (69, 76, -2)//r ear</p>
<p>);</p>
|
|