Health/Assets/_VoiceAssistant/Scripts/FaceExpression/UVCTexture.cs

257 lines
6.1 KiB
C#

using OpenCVForUnity.UnityUtils;
using Serenegiant.UVC;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UVCTexture : MonoBehaviour, IUVCDrawer
{
/// <summary>
/// 图片尺寸裁剪
/// </summary>
public float textureClipArea
{
get
{
if(_textureClipArea<=0)
{
_textureClipArea = PlayerPrefs.GetFloat("clipArea", 0.5f);
}
return _textureClipArea;
}
}
float _textureClipArea = -1;
/// <summary>
/// 图片裁剪位置X
/// </summary>
public float clipOffsetX
{
get
{
if(_clipOffsetX < -1)
{
_clipOffsetX = PlayerPrefs.GetFloat("clipOffsetX", 0);
}
return _clipOffsetX;
}
}
float _clipOffsetX = -2;
/// <summary>
/// 图片裁剪位置
/// </summary>
public float clipOffsetY
{
get
{
if (_clipOffsetY < -1)
{
_clipOffsetY = PlayerPrefs.GetFloat("clipOffsetY", 0);
}
return _clipOffsetY;
}
}
float _clipOffsetY = -2;
public int height
{
get
{
return texture.height;
}
}
public int width
{
get
{
return texture.width;
}
}
public float requestedFPS
{
get
{
return _requestedFPS;
}
set
{
_requestedFPS = value;
}
}
float _requestedFPS;
public bool didUpdateThisFrame
{
get
{
return _didUpdateThisFrame;
}
}
bool _didUpdateThisFrame = false;
/// <summary>
/// 图片缩放,最好不要缩放,影响识别效果
/// </summary>
[Range(0.3f,1)]
public float sizeScale = 0.5f;
public bool isPlaying
{
get
{
return _isPlaying;
}
}
bool _isPlaying;
/// <summary>
/// 是否为前置相机
/// </summary>
public bool isFrontFacing = false;
public UVCFilter[] UVCFilters;
private const string TAG = "UVCDrawer#";
UVCManager uvcManager;
public Texture2D texture
{
get
{
return camTexture;
}
}
Texture2D camTexture;
public bool CanDraw(UVCManager manager, UVCDevice device)
{
return UVCFilter.Match(device, UVCFilters);
}
public bool OnUVCAttachEvent(UVCManager manager, UVCDevice device)
{
var result = !device.IsRicoh || device.IsTHETA;
result &= UVCFilter.Match(device, UVCFilters);
return result;
}
public void OnUVCDetachEvent(UVCManager manager, UVCDevice device)
{
Debug.Log("uvc detach event");
}
public void OnUVCStartEvent(UVCManager manager, UVCDevice device, Texture tex)
{
uvcManager = manager;
HandleOnStartPreview(tex);
}
public void OnUVCStopEvent(UVCManager manager, UVCDevice device)
{
HandleOnStopPreview();
}
private void HandleOnStartPreview(Texture tex)
{
int width = (int)(tex.width * sizeScale * textureClipArea);
int height = (int)(tex.height * sizeScale * textureClipArea);
camTexture = new Texture2D(width, height, TextureFormat.RGB24, false);// (Texture2D)tex;
Debug.Log("uvc texture start");
_isPlaying = true;
_didUpdateThisFrame = true;
}
void Update()
{
if (uvcManager != null)
{
var devices = uvcManager.GetAttachedDevices();
if(devices.Count > 0)
{
textureToTexture2D(devices[0].previewTexture, camTexture);
}
}
//if (uvcManager != null)
//{
// camTexture = (Texture2D)uvcManager.GetAttachedDevices()[0].previewTexture;
// FindObjectOfType<RawImage>().texture = camTexture;
//}
//if (camTexture != null)
//{
// frameTimer += Time.deltaTime;
// float fTime = 1 / FPS;
// if (frameTimer >= fTime)
// {
// _didUpdateThisFrame = true;
// frameTimer -= fTime;
// }
// else
// {
// _didUpdateThisFrame = false;
// }
//}
}
private void HandleOnStopPreview()
{
Debug.Log("uvc stop preview");
}
public void Play()
{
}
public void Pause()
{
}
public void Stop()
{
Debug.Log("uvc texture stop");
}
public void textureToTexture2D(Texture texture,Texture2D texture2D)
{
if (texture == null)
throw new ArgumentNullException("texture");
if (texture2D == null)
throw new ArgumentNullException("texture2D");
//if (texture.width != texture2D.width || texture.height != texture2D.height)
// throw new ArgumentException("texture and texture2D need to be the same size.");
RenderTexture prevRT = RenderTexture.active;
if (texture is RenderTexture)
{
RenderTexture.active = (RenderTexture)texture;
texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
texture2D.Apply(false, false);
}
else
{
int width = (int)(texture.width * sizeScale);
int height = (int)(texture.height * sizeScale);
RenderTexture tempRT = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(texture, tempRT);
RenderTexture.active = tempRT;
Rect rect = new Rect(0, 0, width * textureClipArea, height * textureClipArea);
rect.x = (width - rect.width) * 0.5f * (1 + clipOffsetX);
rect.y = (height - rect.height) * 0.5f * (1 + clipOffsetY);
texture2D.ReadPixels(rect, 0, 0, false);
texture2D.Apply(false, false);
RenderTexture.ReleaseTemporary(tempRT);
}
RenderTexture.active = prevRT;
}
}