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




推荐阅读
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • ASP.NET MVC中Area机制的实现与优化
    本文探讨了在ASP.NET MVC框架中,如何通过Area机制有效地组织和管理大规模应用程序的不同功能模块。通过合理的文件夹结构和命名规则,开发人员可以更高效地管理和扩展项目。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 如何高效创建和使用字体图标
    在Web和移动开发中,为什么选择字体图标?主要原因是其卓越的性能,可以显著减少HTTP请求并优化页面加载速度。本文详细介绍了从设计到应用的字体图标制作流程,并提供了专业建议。 ... [详细]
  • 本文详细介绍了如何在ECharts中使用线性渐变色,通过echarts.graphic.LinearGradient方法实现。文章不仅提供了完整的代码示例,还解释了各个参数的具体含义及其应用场景。 ... [详细]
  • 微软Exchange服务器遭遇2022年版“千年虫”漏洞
    微软Exchange服务器在新年伊始遭遇了一个类似于‘千年虫’的日期处理漏洞,导致邮件传输受阻。该问题主要影响配置了FIP-FS恶意软件引擎的Exchange 2016和2019版本。 ... [详细]
  • 社交网络中的级联行为 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文介绍如何使用Python进行文本处理,包括分词和生成词云图。通过整合多个文本文件、去除停用词并生成词云图,展示文本数据的可视化分析方法。 ... [详细]
  • 反向投影技术主要用于在大型输入图像中定位特定的小型模板图像。通过直方图对比,它能够识别出最匹配的区域或点,从而确定模板图像在输入图像中的位置。 ... [详细]
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社区 版权所有