热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UnityOpencv+DlibFaceLandmarkDetector人脸识别

相比叫Opencv自带识别Dlib识别更稳定usingDlibFaceLandmarkDetector;usingDlibFaceLandmarkDetectorExample;u

相比较Opencv自带识别  Dlib识别更稳定 

using DlibFaceLandmarkDetector;
using DlibFaceLandmarkDetectorExample;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.UnityUtils;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Rect = UnityEngine.Rect;
public class WebcamHumanBeauty : MonoBehaviour
{
#region webcam params
[SerializeField, TooltipAttribute("Set the name of the device to use.")]
public string requestedDeviceName = null;
[SerializeField, TooltipAttribute("Set the width of WebCamTexture.")]
public int requestedWidth = 320;
[SerializeField, TooltipAttribute("Set the height of WebCamTexture.")]
public int requestedHeight = 240;
public RawImage WebcamHumanBeautyRaw;
private WebCamTexture webCamTexture;
private WebCamDevice webCamDevice;
#endregion
#region fps
[SerializeField, TooltipAttribute("Set FPS of WebCamTexture.")]
public int requestedFPS = 30;
private FpsMonitor fpsMonitor;
#endregion
#region dlib plugin params
private FaceLandmarkDetector faceLandmarkDetector; // dlib 实例
private string dlibShapePredictorFileName = "sp_human_face_68.dat"; // 训练模型
private string dlibShapePredictorFilePath; // 训练模型文件
private Color32[] colors;
private Texture2D texture;
private Mat webcamMat; // 摄像头 Mat
private Mat faceRectMat; // 脸部区域 Mat
public RawImage TestRaw;
private Texture2D testRawT2D;
private OpenCVForUnity.CoreModule.Rect faceRect;
#endregion
private void Start()
{
// fps 监视器
fpsMOnitor= gameObject.AddComponent();
dlibShapePredictorFilePath = Utils.getFilePath(dlibShapePredictorFileName);
Run();
//webcamMat = new Mat(texture.height, texture.width, CvType.CV_8UC4);
}
private void Update()
{
Color32[] colors = GetColors();
if (colors != null)
{
// Todo : 这个函数直接赋值texture 或者 webcameTexture
// Todo: WebcamHumanBeautyRaw.texture
faceLandmarkDetector.SetImage(colors, texture.width, texture.height, 4, true);
List detectResult = faceLandmarkDetector.Detect();
foreach (var rect in detectResult)
{
//Debug.Log("face : " + rect);
//detect landmark points
faceLandmarkDetector.DetectLandmark(rect);
//draw landmark points
faceLandmarkDetector.DrawDetectLandmarkResult(colors, texture.width, texture.height, 4, true, 0, 255, 0, 255);
}
//draw face rect
faceLandmarkDetector.DrawDetectResult(colors, texture.width, texture.height, 4, true, 255, 0, 0, 255, 2);
texture.SetPixels32(colors);
texture.Apply(false);
//if (detectResult.Count > 0)
//{
// faceRect = new OpenCVForUnity.CoreModule.Rect(0, 0, 600, 500);
// faceRect.x = Convert.ToInt32(detectResult[0].x);
// faceRect.y = Convert.ToInt32(detectResult[0].y);
// faceRect.width = Convert.ToInt32(detectResult[0].width);
// faceRect.height = Convert.ToInt32(detectResult[0].height);
// Debug.Log("face : " + faceRect);
// testRawT2D = new Texture2D(600, 500);
// //testRawT2D = new Texture2D(faceRect.width, faceRect.height);
// //faceRectMat = new Mat(webcamMat, faceRect);
// //Utils.matToTexture2D(faceRectMat, testRawT2D);
// //TestRaw.texture = testRawT2D;
// //TestRaw.SetNativeSize();
//}
//Utils.texture2DToMat(texture, webcamMat);
}
}
// 创建dlib 实例 初始化
private void Run()
{
faceLandmarkDetector = new FaceLandmarkDetector(dlibShapePredictorFilePath);
Initialize();
}
// 携程打开摄像头
private void Initialize()
{
StartCoroutine(_Initialize());
}
// 摄像头携程初始化
private IEnumerator _Initialize()
{
// Creates the camera
if (!String.IsNullOrEmpty(requestedDeviceName))
{
int requestedDeviceIndex = -1;
if (Int32.TryParse(requestedDeviceName, out requestedDeviceIndex))
{
if (requestedDeviceIndex >= 0 && requestedDeviceIndex {
webCamDevice = WebCamTexture.devices[requestedDeviceIndex];
webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
}
}
else
{
for (int cameraIndex = 0; cameraIndex {
if (WebCamTexture.devices[cameraIndex].name == requestedDeviceName)
{
webCamDevice = WebCamTexture.devices[cameraIndex];
webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
break;
}
}
}
if (webCamTexture == null)
Debug.Log("Cannot find camera device " + requestedDeviceName + ".");
}
if (webCamTexture == null)
{
if (WebCamTexture.devices.Length > 0)
{
webCamDevice = WebCamTexture.devices[0];
webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
}
else
{
Debug.LogError("Camera device does not exist.");
yield break;
}
}
//WebcamHumanBeautyRaw.texture = webCamTexture;
// Starts the camera
webCamTexture.Play();
//webCamTexture.GetPixels32 创建Color32要放在Play() 后面
//https://docs.unity3d.com/ScriptReference/WebCamTexture.GetPixels32.html
colors = new Color32[webCamTexture.width * webCamTexture.height];
texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
WebcamHumanBeautyRaw.texture = texture;
}
// 返回摄像头像素值
private Color32[] GetColors()
{
webCamTexture.GetPixels32(colors);
return colors;
}
// 销毁释放
private void OnDestroy()
{
if (webCamTexture != null)
{
webCamTexture.Stop();
WebCamTexture.Destroy(webCamTexture);
webCamTexture = null;
}
if (faceLandmarkDetector != null)
faceLandmarkDetector.Dispose();
}
}




推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文概述了JNI的原理以及常用方法。JNI提供了一种Java字节码调用C/C++的解决方案,但引用类型不能直接在Native层使用,需要进行类型转化。多维数组(包括二维数组)都是引用类型,需要使用jobjectArray类型来存取其值。此外,由于Java支持函数重载,根据函数名无法找到对应的JNI函数,因此介绍了JNI函数签名信息的解决方案。 ... [详细]
  • 本文介绍了在实现了System.Collections.Generic.IDictionary接口的泛型字典类中如何使用foreach循环来枚举字典中的键值对。同时还讨论了非泛型字典类和泛型字典类在foreach循环中使用的不同类型,以及使用KeyValuePair类型在foreach循环中枚举泛型字典类的优势。阅读本文可以帮助您更好地理解泛型字典类的使用和性能优化。 ... [详细]
  • 花瓣|目标值_Compose 动画边学边做夏日彩虹
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Compose动画边学边做-夏日彩虹相关的知识,希望对你有一定的参考价值。引言Comp ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
  • 可空类型可空类型主要用于参数类型声明和函数返回值声明。主要的两种形式如下: ... [详细]
  • 【CTF 攻略】第三届 SSCTF 全国网络安全大赛—线上赛 Writeup
    【CTF 攻略】第三届 SSCTF 全国网络安全大赛—线上赛 Writeup ... [详细]
  • ListBox.SelectedItem.Value可以获取当前被选中的一个值.但如果ListBox同时有多个值被选中应该如何获取这些值呢? ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 如何用JNI技术调用Java接口以及提高Java性能的详解
    本文介绍了如何使用JNI技术调用Java接口,并详细解析了如何通过JNI技术提高Java的性能。同时还讨论了JNI调用Java的private方法、Java开发中使用JNI技术的情况以及使用Java的JNI技术调用C++时的运行效率问题。文章还介绍了JNIEnv类型的使用方法,包括创建Java对象、调用Java对象的方法、获取Java对象的属性等操作。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
author-avatar
0鞋包控0
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有