Skip to content

Commit

Permalink
update distance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeblackshear committed May 31, 2023
1 parent eb2b74c commit b158c16
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
7 changes: 3 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"csstools.postcss",
"blanu.vscode-styled-jsx",
"bradlc.vscode-tailwindcss",
"ms-python.isort"
"ms-python.isort",
"charliermarsh.ruff"
],
"settings": {
"remote.autoForwardPorts": false,
Expand All @@ -69,9 +70,7 @@
"python.testing.unittestArgs": ["-v", "-s", "./frigate/test"],
"files.trimTrailingWhitespace": true,
"eslint.workingDirectories": ["./web"],
"isort.args": [
"--settings-path=./pyproject.toml"
],
"isort.args": ["--settings-path=./pyproject.toml"],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
Expand Down
2 changes: 1 addition & 1 deletion frigate/track/centroid_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import numpy as np
from scipy.spatial import distance as dist
from frigate.config import DetectConfig

from frigate.config import DetectConfig
from frigate.track import ObjectTracker
from frigate.util import intersection_over_union

Expand Down
56 changes: 39 additions & 17 deletions frigate/track/norfair_tracker.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from collections import defaultdict
import random
import string

import numpy as np
from norfair import Detection, Drawable, Tracker, draw_boxes
from norfair.drawing.drawer import Drawer

from frigate.config import DetectConfig
from frigate.track import ObjectTracker
from frigate.util import intersection_over_union
from norfair import Detection, Tracker, Drawable, draw_boxes
from norfair.drawing.drawer import Drawer


# Normalizes distance from estimate relative to object size
Expand All @@ -16,17 +16,41 @@
# - could be variable based on time since last_detection
# - include estimated velocity in the distance (car driving by of a parked car)
# - include some visual similarity factor in the distance for occlusions
def frigate_distance(detection: Detection, tracked_object) -> float:
# calculate distances and normalize it by width and height of previous detection
ld = tracked_object.last_detection
width = ld.points[1][0] - ld.points[0][0]
height = ld.points[1][1] - ld.points[0][1]
difference = (detection.points - tracked_object.estimate).astype(float)
difference[:, 0] /= width
difference[:, 1] /= height
def distance(detection: np.array, estimate: np.array) -> float:
# ultimately, this should try and estimate distance in 3-dimensional space
# consider change in location, width, and height

estimate_dim = np.diff(estimate, axis=0).flatten()
detection_dim = np.diff(detection, axis=0).flatten()

# get centroid positions
detection_position = np.array(
[np.average(detection[:, 0]), np.max(detection[:, 1])]
)
estimate_position = np.array([np.average(estimate[:, 0]), np.max(estimate[:, 1])])

# calculate euclidean distance and average
return np.linalg.norm(difference, axis=1).mean()
distance = (detection_position - estimate_position).astype(float)
# change in x relative to w
distance[0] /= estimate_dim[0]
# change in y relative to h
distance[1] /= estimate_dim[1]

# get ratio of widths and heights
# normalize to 1
widths = np.sort([estimate_dim[0], detection_dim[0]])
heights = np.sort([estimate_dim[1], detection_dim[1]])
width_ratio = widths[1] / widths[0] - 1.0
height_ratio = heights[1] / heights[0] - 1.0

# change vector is relative x,y change and w,h ratio
change = np.append(distance, np.array([width_ratio, height_ratio]))

# calculate euclidean distance of the change vector
return np.linalg.norm(change)


def frigate_distance(detection: Detection, tracked_object) -> float:
return distance(detection.points, tracked_object.estimate)


class NorfairTracker(ObjectTracker):
Expand All @@ -41,9 +65,7 @@ def __init__(self, config: DetectConfig):
# was a good reason to have different distance calculations
self.tracker = Tracker(
distance_function=frigate_distance,
# distance is relative to the size of the last
# detection
distance_threshold=4.0,
distance_threshold=2.5,
initialization_delay=0,
hit_counter_max=self.max_disappeared,
)
Expand Down Expand Up @@ -210,7 +232,7 @@ def match_and_update(self, frame_time, detections):
active_ids = []
for t in tracked_objects:
active_ids.append(t.global_id)
if not t.global_id in self.track_id_map:
if t.global_id not in self.track_id_map:
self.register(t.global_id, t.last_detection.data)
# if there wasn't a detection in this frame, increment disappeared
elif t.last_detection.data["frame_time"] != frame_time:
Expand Down
8 changes: 4 additions & 4 deletions frigate/track/sort_tracker.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections import defaultdict
import random
import string

import numpy as np
from similari import BoundingBox, PositionalMetricType, Sort, SpatioTemporalConstraints

from frigate.config import DetectConfig
from frigate.track import ObjectTracker
from frigate.util import intersection_over_union
from similari import Sort, BoundingBox, SpatioTemporalConstraints, PositionalMetricType


class SortTracker(ObjectTracker):
Expand Down Expand Up @@ -156,7 +156,7 @@ def match_and_update(self, frame_time, detections):
# get the scene_id for this label or create a new one
# TODO: consider grouping frequently swapped objects in
# in the same scene
if not obj[0] in self.scene_map:
if obj[0] not in self.scene_map:
scene_id = len(self.scene_map.keys())
self.scene_map[obj[0]] = scene_id
scene_detections[scene_id] = []
Expand Down Expand Up @@ -200,7 +200,7 @@ def match_and_update(self, frame_time, detections):

# update or create new tracks
for t in tracks:
if not t.id in self.track_id_map:
if t.id not in self.track_id_map:
self.register(t.id, objs[t.custom_object_id])
else:
self.update(t.id, objs[t.custom_object_id])
Expand Down
2 changes: 0 additions & 2 deletions frigate/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
from frigate.motion import MotionDetector
from frigate.object_detection import RemoteObjectDetector
from frigate.track import ObjectTracker
from frigate.track.centroid_tracker import CentroidTracker
from frigate.track.norfair_tracker import NorfairTracker
from frigate.track.sort_tracker import SortTracker
from frigate.util import (
EventsPerSecond,
FrameManager,
Expand Down

0 comments on commit b158c16

Please sign in to comment.