add DOTween / add start settings
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
using UltraFace;
|
||||
|
||||
public class ApplicationInstaller : MonoInstaller
|
||||
{
|
||||
public override void InstallBindings()
|
||||
{
|
||||
Container.Bind<DetectionSetup>().AsSingle();
|
||||
Container.Bind<Visualizer>().FromComponentInHierarchy().AsSingle();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c1fb5f7a26e419498d4df05deca6b67
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class DetectionSetup
|
||||
{
|
||||
public bool isValidate;
|
||||
public float minFaceHeight { get; private set; }
|
||||
public void SetMinFaceHeight(float minValue)
|
||||
{
|
||||
minFaceHeight = minValue;
|
||||
isValidate = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f4b79cf6ddccb34abd627c99d18f0b3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e1804eb575387b42b6592b0c7465508
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
using UltraFace;
|
||||
using DG.Tweening;
|
||||
|
||||
public class ImageCanvasHandler : MonoBehaviour
|
||||
{
|
||||
[Inject] private Visualizer _visualizer;
|
||||
[SerializeField] private RawImage _rawImage;
|
||||
private RectTransform _rectTransform;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_rectTransform = GetComponent<RectTransform>();
|
||||
_visualizer.OnDetectionStatusChanged += ChangeState;
|
||||
}
|
||||
|
||||
private void ChangeState(bool isActive)
|
||||
{
|
||||
if(isActive)
|
||||
{
|
||||
_rawImage.DOFade(1, .25f);
|
||||
}
|
||||
else
|
||||
{
|
||||
_rawImage.DOFade(0, .25f);
|
||||
}
|
||||
|
||||
_rectTransform.DORotate(_rectTransform.eulerAngles + Vector3.up * 360, .5f);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_visualizer.OnDetectionStatusChanged -= ChangeState;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fdf61e6e388c35409860bbb41d0804a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Zenject;
|
||||
using UltraFace;
|
||||
using TMPro;
|
||||
|
||||
public class SettingsUI : MonoBehaviour
|
||||
{
|
||||
[Inject] private Visualizer _visualizer;
|
||||
[Inject] private DetectionSetup _detectionSetup;
|
||||
|
||||
[SerializeField] private TMP_Text _faceCounterText;
|
||||
[SerializeField] private TMP_Text _minFaceHeight;
|
||||
[SerializeField] private TMP_Text _maxFaceHeight;
|
||||
[SerializeField] private Button _acceptButton;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_acceptButton.onClick.AddListener(() => OnAcceptButtonClick());
|
||||
}
|
||||
|
||||
private void OnAcceptButtonClick()
|
||||
{
|
||||
_detectionSetup.SetMinFaceHeight(float.Parse(_maxFaceHeight.text));
|
||||
Debug.LogWarning($"минимальная дистанция - {_maxFaceHeight.text}");
|
||||
_visualizer.Init();
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var faceCounter = _visualizer.faceCounter;
|
||||
var minFaceHeight = _visualizer.minFaceHeight;
|
||||
var maxFaceHeight = _visualizer.maxFaceHeight;
|
||||
|
||||
_faceCounterText.text = faceCounter.ToString();
|
||||
_faceCounterText.color = faceCounter != 0 ? Color.green : Color.red;
|
||||
|
||||
_minFaceHeight.text = minFaceHeight.ToString();
|
||||
_maxFaceHeight.text = maxFaceHeight.ToString();
|
||||
}
|
||||
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_acceptButton.onClick.RemoveAllListeners();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9031c3a3c7150049a10dd2075aed468
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UI = UnityEngine.UI;
|
||||
using Klak.TestTools;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Zenject;
|
||||
|
||||
namespace UltraFace
|
||||
{
|
||||
public sealed class Visualizer : MonoBehaviour
|
||||
{
|
||||
#region Editable attributes
|
||||
|
||||
[SerializeField] ImageSource _source = null;
|
||||
[SerializeField, Range(0, 1)] float _threshold = 0.5f;
|
||||
[SerializeField] ResourceSet _resources = null;
|
||||
[SerializeField] Shader _visualizer = null;
|
||||
[SerializeField] Texture2D _texture = null;
|
||||
[SerializeField] UI.RawImage _previewUI = null;
|
||||
|
||||
[Inject] private DetectionSetup _detectionSetup;
|
||||
|
||||
public int faceCounter => _detector.Detections.Count();
|
||||
public float minFaceHeight
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_detector.Detections.Count() == 0)
|
||||
return 0f;
|
||||
else
|
||||
return _detector.Detections.Min(face => face.y2 - face.y1);
|
||||
}
|
||||
}
|
||||
public float maxFaceHeight
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_detector.Detections.Count() == 0)
|
||||
return 0f;
|
||||
else
|
||||
return _detector.Detections.Max(face => face.y2 - face.y1);
|
||||
}
|
||||
}
|
||||
|
||||
public Action<bool> OnDetectionStatusChanged;
|
||||
private List<bool> _detectedCounter;
|
||||
private bool _isDetected;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private objects
|
||||
|
||||
FaceDetector _detector;
|
||||
Material _material;
|
||||
ComputeBuffer _drawArgs;
|
||||
|
||||
#endregion
|
||||
|
||||
#region MonoBehaviour implementation
|
||||
|
||||
void Start()
|
||||
{
|
||||
_detector = new FaceDetector(_resources);
|
||||
_material = new Material(_visualizer);
|
||||
_drawArgs = new ComputeBuffer(4, sizeof(uint), ComputeBufferType.IndirectArguments);
|
||||
_drawArgs.SetData(new [] {6, 0, 0, 0});
|
||||
_detectedCounter = new List<bool>();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
OnDetectionStatusChanged?.Invoke(false);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
_detector.ProcessImage(_source.Texture, _threshold);
|
||||
_previewUI.texture = _source.Texture;
|
||||
|
||||
if(!_detectionSetup.isValidate) return;
|
||||
Debug.Log(_detectedCounter.Count);
|
||||
|
||||
_detectedCounter.Add(maxFaceHeight > _detectionSetup.minFaceHeight ? true : false);
|
||||
if(_detectedCounter.Count == 128)
|
||||
{
|
||||
var trueCounter = _detectedCounter.Count(x => x == true);
|
||||
|
||||
if(!_isDetected && trueCounter >= 64)
|
||||
{
|
||||
if(maxFaceHeight < _detectionSetup.minFaceHeight) return;
|
||||
_isDetected = true;
|
||||
Debug.LogWarning("лицо определено");
|
||||
}
|
||||
else if(_isDetected && trueCounter < 64)
|
||||
{
|
||||
_isDetected = false;
|
||||
Debug.LogWarning("лицо потеряно");
|
||||
}
|
||||
|
||||
OnDetectionStatusChanged?.Invoke(_isDetected);
|
||||
_detectedCounter = new List<bool>();
|
||||
}
|
||||
}
|
||||
|
||||
void OnRenderObject()
|
||||
{
|
||||
return;
|
||||
|
||||
_detector.SetIndirectDrawCount(_drawArgs);
|
||||
_material.SetFloat("_Threshold", _threshold);
|
||||
_material.SetTexture("_Texture", _texture);
|
||||
_material.SetBuffer("_Detections", _detector.DetectionBuffer);
|
||||
_material.SetPass(_texture == null ? 0 : 1);
|
||||
Graphics.DrawProceduralIndirectNow(MeshTopology.Triangles, _drawArgs, 0);
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
_detector?.Dispose();
|
||||
Destroy(_material);
|
||||
_drawArgs?.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e679627c6c78c0045931944c9f0ca6ee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user