PersonManager.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Assets.StreetLight.Adapters;
  2. using Assets.StreetLight.Interfaces;
  3. using Assets.StreetLight.Poco;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using UnityEngine;
  12. namespace Assets.StreetLight.Scripts
  13. {
  14. public class PersonManager : MonoBehaviour
  15. {
  16. public ObservableCollection<Person> Persons { get; private set; }
  17. public PositionCalculator PositionCalculator { get; private set; }
  18. private IPersonDetector personDetector;
  19. public event EventHandler DetectionReady;
  20. void Start()
  21. {
  22. var lines = File.ReadAllLines(Configuration.Instance.InputCalibrationFilePath);
  23. var calibrationPoints = new List<CalibrationPoint>();
  24. foreach (var line in lines.Skip(1))
  25. {
  26. var coordinates = line.Split(',').Skip(1).ToArray();
  27. calibrationPoints.Add(new CalibrationPoint(
  28. new Vector3(
  29. float.Parse(coordinates[0], CultureInfo.InvariantCulture),
  30. float.Parse(coordinates[1], CultureInfo.InvariantCulture),
  31. float.Parse(coordinates[2], CultureInfo.InvariantCulture)),
  32. new Vector3(
  33. float.Parse(coordinates[3], CultureInfo.InvariantCulture),
  34. float.Parse(coordinates[4], CultureInfo.InvariantCulture),
  35. float.Parse(coordinates[5], CultureInfo.InvariantCulture))));
  36. }
  37. PositionCalculator = new PositionCalculator(calibrationPoints);
  38. Persons = new ObservableCollection<Person>();
  39. personDetector = new ZedPersonDetector(FindObjectOfType<ZEDManager>());
  40. personDetector.PersonsDetected += InvokeDetectionReady;
  41. personDetector.PersonsDetected += PersonDetector_PersonsDetected;
  42. }
  43. private void InvokeDetectionReady(object sender, IEnumerable<Person> e)
  44. {
  45. DetectionReady?.Invoke(this, EventArgs.Empty);
  46. personDetector.PersonsDetected -= InvokeDetectionReady;
  47. }
  48. private void PersonDetector_PersonsDetected(object sender, IEnumerable<Person> e)
  49. {
  50. Persons.Clear();
  51. foreach (var person in e)
  52. {
  53. Persons.Add(person);
  54. }
  55. }
  56. void Update()
  57. {
  58. }
  59. }
  60. }