热门标签 | 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();
}
}




推荐阅读
  • ASP.NET MVC中Area机制的实现与优化
    本文探讨了在ASP.NET MVC框架中,如何通过Area机制有效地组织和管理大规模应用程序的不同功能模块。通过合理的文件夹结构和命名规则,开发人员可以更高效地管理和扩展项目。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 微软Exchange服务器遭遇2022年版“千年虫”漏洞
    微软Exchange服务器在新年伊始遭遇了一个类似于‘千年虫’的日期处理漏洞,导致邮件传输受阻。该问题主要影响配置了FIP-FS恶意软件引擎的Exchange 2016和2019版本。 ... [详细]
  • 探讨如何真正掌握Java EE,包括所需技能、工具和实践经验。资深软件教学总监李刚分享了对毕业生简历中常见问题的看法,并提供了详尽的标准。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 如何高效创建和使用字体图标
    在Web和移动开发中,为什么选择字体图标?主要原因是其卓越的性能,可以显著减少HTTP请求并优化页面加载速度。本文详细介绍了从设计到应用的字体图标制作流程,并提供了专业建议。 ... [详细]
  • MySQL 数据库迁移指南:从本地到远程及磁盘间迁移
    本文详细介绍了如何在不同场景下进行 MySQL 数据库的迁移,包括从一个硬盘迁移到另一个硬盘、从一台计算机迁移到另一台计算机,以及解决迁移过程中可能遇到的问题。 ... [详细]
  • 本文探讨了领域驱动设计(DDD)的核心概念、应用场景及其实现方式,详细介绍了其在企业级软件开发中的优势和挑战。通过对比事务脚本与领域模型,展示了DDD如何提升系统的可维护性和扩展性。 ... [详细]
  • 深入解析 Spring Security 用户认证机制
    本文将详细介绍 Spring Security 中用户登录认证的核心流程,重点分析 AbstractAuthenticationProcessingFilter 和 AuthenticationManager 的工作原理。通过理解这些组件的实现,读者可以更好地掌握 Spring Security 的认证机制。 ... [详细]
  • 本文介绍如何在现有网络中部署基于Linux系统的透明防火墙(网桥模式),以实现灵活的时间段控制、流量限制等功能。通过详细的步骤和配置说明,确保内部网络的安全性和稳定性。 ... [详细]
  • 本文介绍了一种在Win10 UWP应用中实现根据数值动态改变颜色的控件的方法。通过将椭圆的颜色与整数绑定,并利用值转换器来实现颜色的渐变效果。 ... [详细]
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社区 版权所有