TrackedObject.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Utility;
  7. namespace bbiwarg.Recognition.Tracking
  8. {
  9. public enum TrackingState
  10. {
  11. Undefined = 0,
  12. Detected = 1,
  13. Tracked = 2,
  14. Lost = 3,
  15. Deleted = 4
  16. }
  17. public abstract class TrackedObject<T> where T : TrackableObject
  18. {
  19. private int numFramesDetectedUntilTracked;
  20. private int numFramesLostUntilDeleted;
  21. private bool wasTrackedBefore;
  22. public int ID { get; private set; }
  23. public T CurrentObject { get; private set; }
  24. public T LastObject { get; private set; }
  25. public TrackingState CurrentState { get; private set; }
  26. public TrackingState PreviousState { get; private set; }
  27. public int NumFramesInCurrentState { get; private set; }
  28. public TrackedObject(int id, T detectedObject, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  29. {
  30. ID = id;
  31. this.numFramesDetectedUntilTracked = numFramesDetectedUntilTracked;
  32. this.numFramesLostUntilDeleted = numFramesLostUntilDeleted;
  33. wasTrackedBefore = false;
  34. CurrentObject = detectedObject;
  35. LastObject = detectedObject;
  36. CurrentState = TrackingState.Detected;
  37. PreviousState = TrackingState.Undefined;
  38. NumFramesInCurrentState = 1;
  39. }
  40. public virtual void updateFrame(T detectedObject)
  41. {
  42. if (detectedObject != null)
  43. {
  44. detectedObject.setTracked(ID);
  45. LastObject = detectedObject;
  46. }
  47. TrackingState newState = getNewState(detectedObject);
  48. if (!wasTrackedBefore && newState == TrackingState.Tracked)
  49. wasTrackedBefore = true;
  50. PreviousState = CurrentState;
  51. CurrentState = newState;
  52. CurrentObject = detectedObject;
  53. if (PreviousState == newState)
  54. NumFramesInCurrentState++;
  55. else
  56. NumFramesInCurrentState = 1;
  57. }
  58. private TrackingState getNewState(T detectedObject)
  59. {
  60. TrackingState newState = TrackingState.Undefined;
  61. if (detectedObject != null)
  62. {
  63. switch (CurrentState)
  64. {
  65. case TrackingState.Lost:
  66. if (wasTrackedBefore)
  67. newState = TrackingState.Tracked;
  68. else
  69. newState = TrackingState.Detected;
  70. break;
  71. case TrackingState.Tracked:
  72. newState = TrackingState.Tracked;
  73. break;
  74. case TrackingState.Detected:
  75. if (NumFramesInCurrentState >= numFramesDetectedUntilTracked)
  76. newState = TrackingState.Tracked;
  77. else
  78. newState = TrackingState.Detected;
  79. break;
  80. }
  81. }
  82. else if (CurrentState == TrackingState.Lost && NumFramesInCurrentState >= numFramesLostUntilDeleted)
  83. newState = TrackingState.Deleted;
  84. else
  85. newState = TrackingState.Lost;
  86. return newState;
  87. }
  88. }
  89. }