-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ObjectTracker] some cleaning and refactoring of motion detection.
- Loading branch information
1 parent
c85fdb9
commit d89ed4b
Showing
4 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from enum import Enum, auto | ||
|
||
|
||
class DistanceFilter(Enum): | ||
MOVING_AVERAGE = auto() | ||
LOW_PASS = auto() | ||
|
||
|
||
class MotionDetectionMethod(Enum): | ||
CONSISTENT_GRADIENT = auto() | ||
DISTANCE = auto() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from abc import abstractmethod, ABC | ||
from math import ceil, floor | ||
|
||
import numpy as np | ||
from typing_extensions import List, Tuple | ||
|
||
|
||
class MotionDetectionMethod(ABC): | ||
""" | ||
Interface for motion detection methods. | ||
""" | ||
|
||
@abstractmethod | ||
def is_moving(self, latest_distances: List[List[float]]) -> Tuple[bool, int]: | ||
""" | ||
Check if the object is moving. | ||
:param latest_distances: List of the latest distances. | ||
:return: True if the object is moving, False if it is not moving, | ||
and return the index in the given list of distances where the object started moving. | ||
""" | ||
pass | ||
|
||
|
||
class ConsistentGradient(MotionDetectionMethod): | ||
|
||
def __init__(self, threshold: float = 1e-4): | ||
self.threshold = threshold | ||
|
||
def is_moving(self, latest_distances: List[List[float]]) -> Tuple[bool, int]: | ||
""" | ||
Check if the object is moving by checking if the distance between the current and the previous position is | ||
consistently positive or negative in at least one axis during the latest steps (the number of latest distances). | ||
""" | ||
distance_arr = np.array(latest_distances) | ||
n_axes = distance_arr.shape[1] | ||
return any(np.all(distance_arr[:, i] > self.threshold) or np.all(distance_arr[:, i] < -self.threshold) | ||
for i in range(n_axes)), 0 | ||
|
||
|
||
class Displacement(MotionDetectionMethod): | ||
|
||
def __init__(self, threshold: float): | ||
self.threshold = threshold | ||
|
||
def is_moving(self, latest_distances: List[List[float]]) -> Tuple[bool, int]: | ||
""" | ||
Check if the object is moving by checking if the displacement between latest position and the start position is | ||
above a certain threshold. | ||
""" | ||
avg_distance = np.linalg.norm(np.sum(np.array(latest_distances))) | ||
return avg_distance > self.threshold, floor((len(latest_distances) / 2) - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from unittest import TestCase | ||
|
||
import numpy as np | ||
|
||
from episode_segmenter.motion_detection_helpers import ConsistentGradient, Displacement | ||
|
||
|
||
class TestEventDetectors(TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
pass | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
pass | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_consistent_gradient_motion_detection_method(self): | ||
for i in range(3): | ||
a = np.zeros((3, 3)) | ||
a[:, i] = 1 | ||
cg = ConsistentGradient() | ||
self.assertTrue(cg.is_moving(a.tolist()) == (True, 0)) | ||
a = np.zeros((3, 3)) | ||
a[:, i] = -1 | ||
self.assertTrue(cg.is_moving(a.tolist()) == (True, 0)) | ||
a = np.zeros((3, 3)) | ||
a[:, i] = -1 | ||
a[1, i] = 1 | ||
self.assertFalse(cg.is_moving(a.tolist()) == (True, 0)) | ||
|
||
def test_displacement_motion_detection_method(self): | ||
for i in range(3): | ||
a = np.zeros((3, 3)) | ||
a[:, i] = 1 | ||
disp = Displacement(1.5) | ||
self.assertTrue(disp.is_moving(a.tolist()) == (True, 0)) | ||
a = np.zeros((3, 3)) | ||
a[:, i] = -1 | ||
self.assertTrue(disp.is_moving(a.tolist()) == (True, 0)) | ||
a = np.zeros((3, 3)) | ||
a[:, i] = -1 | ||
a[1, i] = 1 | ||
self.assertTrue(disp.is_moving(a.tolist()) == (False, 0)) | ||
|