|
using UnityEngine; |
|
using Unity.Sentis; |
|
using UnityEngine.Video; |
|
using UnityEngine.UI; |
|
using System.IO; |
|
using Lays = Unity.Sentis.Layers; |
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class RunHandLandmark : MonoBehaviour |
|
{ |
|
|
|
public ModelAsset asset; |
|
string modelName = "hand_landmark.sentis"; |
|
|
|
public RawImage previewUI = null; |
|
|
|
|
|
public Sprite boxSprite; |
|
|
|
|
|
public Sprite[] markerTextures; |
|
|
|
public string videoName = "chatting.mp4"; |
|
|
|
public Texture2D inputImage; |
|
|
|
public InputType inputType = InputType.Video; |
|
|
|
|
|
Vector2Int resolution = new Vector2Int(640, 640); |
|
WebCamTexture webcam; |
|
VideoPlayer video; |
|
|
|
const BackendType backend = BackendType.GPUCompute; |
|
|
|
RenderTexture targetTexture; |
|
public enum InputType { Image, Video, Webcam }; |
|
|
|
IWorker worker; |
|
|
|
|
|
const int size = 224; |
|
|
|
Model model; |
|
|
|
|
|
const string deviceName = ""; |
|
|
|
bool closing = false; |
|
|
|
public struct BoundingBox |
|
{ |
|
public float centerX; |
|
public float centerY; |
|
public float width; |
|
public float height; |
|
} |
|
|
|
List<GameObject> boxPool = new(); |
|
void Start() |
|
{ |
|
|
|
|
|
targetTexture = new RenderTexture(resolution.x, resolution.y, 0); |
|
previewUI.texture = targetTexture; |
|
|
|
SetupInput(); |
|
SetupModel(); |
|
SetupEngine(); |
|
} |
|
|
|
void SetupModel() |
|
{ |
|
model = ModelLoader.Load(asset); |
|
|
|
} |
|
public void SetupEngine() |
|
{ |
|
worker = WorkerFactory.CreateWorker(backend, model); |
|
} |
|
|
|
void SetupInput() |
|
{ |
|
switch (inputType) |
|
{ |
|
case InputType.Webcam: |
|
{ |
|
webcam = new WebCamTexture(deviceName, resolution.x, resolution.y); |
|
webcam.requestedFPS = 30; |
|
webcam.Play(); |
|
break; |
|
} |
|
case InputType.Video: |
|
{ |
|
video = gameObject.AddComponent<VideoPlayer>(); |
|
video.renderMode = VideoRenderMode.APIOnly; |
|
video.source = VideoSource.Url; |
|
video.url = Application.streamingAssetsPath + "/"+videoName; |
|
video.isLooping = true; |
|
video.Play(); |
|
break; |
|
} |
|
default: |
|
{ |
|
Graphics.Blit(inputImage, targetTexture); |
|
} |
|
break; |
|
} |
|
} |
|
|
|
void Update() |
|
{ |
|
if (inputType == InputType.Webcam) |
|
{ |
|
|
|
if (!webcam.didUpdateThisFrame) return; |
|
|
|
var aspect1 = (float)webcam.width / webcam.height; |
|
var aspect2 = (float)resolution.x / resolution.y; |
|
var gap = aspect2 / aspect1; |
|
|
|
var vflip = webcam.videoVerticallyMirrored; |
|
var scale = new Vector2(gap, vflip ? -1 : 1); |
|
var offset = new Vector2((1 - gap) / 2, vflip ? 1 : 0); |
|
|
|
Graphics.Blit(webcam, targetTexture, scale, offset); |
|
} |
|
if (inputType == InputType.Video) |
|
{ |
|
var aspect1 = (float)video.width / video.height; |
|
var aspect2 = (float)resolution.x / resolution.y; |
|
var gap = aspect2 / aspect1; |
|
|
|
var vflip = false; |
|
var scale = new Vector2(gap, vflip ? -1 : 1); |
|
var offset = new Vector2((1 - gap) / 2, vflip ? 1 : 0); |
|
Graphics.Blit(video.texture, targetTexture, scale, offset); |
|
} |
|
if (inputType == InputType.Image) |
|
{ |
|
Graphics.Blit(inputImage, targetTexture); |
|
} |
|
|
|
if (Input.GetKeyDown(KeyCode.Escape)) |
|
{ |
|
closing = true; |
|
Application.Quit(); |
|
} |
|
|
|
if (Input.GetKeyDown(KeyCode.P)) |
|
{ |
|
previewUI.enabled = !previewUI.enabled; |
|
} |
|
} |
|
|
|
void LateUpdate() |
|
{ |
|
if (!closing) |
|
{ |
|
RunInference(targetTexture); |
|
} |
|
} |
|
|
|
void DrawLandmarks(TensorFloat landmarks, Vector2 scale) |
|
{ |
|
|
|
for (int j = 0; j < 21; j++) |
|
{ |
|
var marker = new BoundingBox |
|
{ |
|
centerX = landmarks[0, j * 3] * scale.x - (size / 2) * scale.x, |
|
centerY = landmarks[0, j * 3 + 1] * scale.y - (size/2) * scale.y, |
|
width = 8f * scale.x, |
|
height = 8f * scale.y, |
|
}; |
|
DrawBox(marker, j < markerTextures.Length ? markerTextures[j] : boxSprite, j); |
|
} |
|
} |
|
|
|
void RunInference(Texture source) |
|
{ |
|
var transform = new TextureTransform(); |
|
transform.SetDimensions(size, size, 3); |
|
transform.SetTensorLayout(0, 1, 2, 3); |
|
using var image = TextureConverter.ToTensor(source, transform); |
|
|
|
worker.Execute(image); |
|
|
|
using var landmarks = worker.PeekOutput("Identity") as TensorFloat; |
|
|
|
ClearAnnotations(); |
|
|
|
Vector2 markerScale = previewUI.rectTransform.rect.size/ size; |
|
landmarks.CompleteOperationsAndDownload(); |
|
DrawLandmarks(landmarks, markerScale); |
|
|
|
bool showExtraInformation = false; |
|
if (showExtraInformation) |
|
{ |
|
using var A = worker.PeekOutput("Identity_1") as TensorFloat; |
|
using var B = worker.PeekOutput("Identity_2") as TensorFloat; |
|
A.CompleteOperationsAndDownload(); |
|
B.CompleteOperationsAndDownload(); |
|
Debug.Log("A,B=" + A[0, 0] + "," + B[0, 0]); |
|
} |
|
} |
|
|
|
public void DrawBox(BoundingBox box, Sprite sprite, int ID) |
|
{ |
|
GameObject panel = null; |
|
if (ID >= boxPool.Count) |
|
{ |
|
panel = new GameObject("landmark"); |
|
panel.AddComponent<CanvasRenderer>(); |
|
panel.AddComponent<Image>(); |
|
panel.transform.SetParent(previewUI.transform, false); |
|
boxPool.Add(panel); |
|
} |
|
else |
|
{ |
|
panel = boxPool[ID]; |
|
panel.SetActive(true); |
|
} |
|
|
|
var img = panel.GetComponent<Image>(); |
|
img.color = Color.white; |
|
img.sprite = sprite; |
|
img.type = Image.Type.Sliced; |
|
|
|
panel.transform.localPosition = new Vector3(box.centerX, -box.centerY); |
|
RectTransform rt = panel.GetComponent<RectTransform>(); |
|
rt.sizeDelta = new Vector2(box.width, box.height); |
|
} |
|
|
|
public void ClearAnnotations() |
|
{ |
|
for (int i = 0; i < boxPool.Count; i++) |
|
{ |
|
boxPool[i].SetActive(false); |
|
} |
|
} |
|
|
|
void CleanUp() |
|
{ |
|
closing = true; |
|
if (webcam) Destroy(webcam); |
|
if (video) Destroy(video); |
|
RenderTexture.active = null; |
|
targetTexture.Release(); |
|
worker?.Dispose(); |
|
worker = null; |
|
} |
|
|
|
void OnDestroy() |
|
{ |
|
CleanUp(); |
|
} |
|
|
|
} |
|
|
|
|