TrackedObject.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace bbiwarg.Recognition.Tracking
  7. {
  8. public enum TrackingState
  9. {
  10. Undefined = 0,
  11. Detected = 1,
  12. Tracked = 2,
  13. Lost = 3,
  14. Deleted = 4
  15. }
  16. public abstract class TrackedObject<T> where T : TrackableObject
  17. {
  18. private int numFramesDetectedUntilTracked;
  19. private int numFramesLostUntilDeleted;
  20. private bool wasTrackedBefore;
  21. public int ID { get; private set; }
  22. public T CurrentObject { get; private set; }
  23. public T LastObject { get; private set; }
  24. public TrackingState CurrentState { get; private set; }
  25. public TrackingState PreviousState { get; private set; }
  26. public int NumFramesInCurrentState { get; private set; }
  27. public TrackedObject(int id, T detectedObject, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  28. {
  29. ID = id;
  30. this.numFramesDetectedUntilTracked = numFramesDetectedUntilTracked;
  31. this.numFramesLostUntilDeleted = numFramesLostUntilDeleted;
  32. wasTrackedBefore = false;
  33. CurrentObject = detectedObject;
  34. LastObject = detectedObject;
  35. CurrentState = TrackingState.Detected;
  36. PreviousState = TrackingState.Undefined;
  37. NumFramesInCurrentState = 1;
  38. }
  39. public abstract float calculateSimilarity(T detectedObject);
  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. }