mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-05 10:45:21 +03:00
update distance calculation
This commit is contained in:
parent
eb2b74c23b
commit
b158c16c13
@ -53,7 +53,8 @@
|
|||||||
"csstools.postcss",
|
"csstools.postcss",
|
||||||
"blanu.vscode-styled-jsx",
|
"blanu.vscode-styled-jsx",
|
||||||
"bradlc.vscode-tailwindcss",
|
"bradlc.vscode-tailwindcss",
|
||||||
"ms-python.isort"
|
"ms-python.isort",
|
||||||
|
"charliermarsh.ruff"
|
||||||
],
|
],
|
||||||
"settings": {
|
"settings": {
|
||||||
"remote.autoForwardPorts": false,
|
"remote.autoForwardPorts": false,
|
||||||
@ -69,9 +70,7 @@
|
|||||||
"python.testing.unittestArgs": ["-v", "-s", "./frigate/test"],
|
"python.testing.unittestArgs": ["-v", "-s", "./frigate/test"],
|
||||||
"files.trimTrailingWhitespace": true,
|
"files.trimTrailingWhitespace": true,
|
||||||
"eslint.workingDirectories": ["./web"],
|
"eslint.workingDirectories": ["./web"],
|
||||||
"isort.args": [
|
"isort.args": ["--settings-path=./pyproject.toml"],
|
||||||
"--settings-path=./pyproject.toml"
|
|
||||||
],
|
|
||||||
"[python]": {
|
"[python]": {
|
||||||
"editor.defaultFormatter": "ms-python.black-formatter",
|
"editor.defaultFormatter": "ms-python.black-formatter",
|
||||||
"editor.formatOnSave": true
|
"editor.formatOnSave": true
|
||||||
|
|||||||
@ -4,8 +4,8 @@ from collections import defaultdict
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy.spatial import distance as dist
|
from scipy.spatial import distance as dist
|
||||||
from frigate.config import DetectConfig
|
|
||||||
|
|
||||||
|
from frigate.config import DetectConfig
|
||||||
from frigate.track import ObjectTracker
|
from frigate.track import ObjectTracker
|
||||||
from frigate.util import intersection_over_union
|
from frigate.util import intersection_over_union
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
from collections import defaultdict
|
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
|
||||||
import numpy as np
|
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.config import DetectConfig
|
||||||
from frigate.track import ObjectTracker
|
from frigate.track import ObjectTracker
|
||||||
from frigate.util import intersection_over_union
|
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
|
# Normalizes distance from estimate relative to object size
|
||||||
@ -16,17 +16,41 @@ from norfair.drawing.drawer import Drawer
|
|||||||
# - could be variable based on time since last_detection
|
# - could be variable based on time since last_detection
|
||||||
# - include estimated velocity in the distance (car driving by of a parked car)
|
# - include estimated velocity in the distance (car driving by of a parked car)
|
||||||
# - include some visual similarity factor in the distance for occlusions
|
# - include some visual similarity factor in the distance for occlusions
|
||||||
def frigate_distance(detection: Detection, tracked_object) -> float:
|
def distance(detection: np.array, estimate: np.array) -> float:
|
||||||
# calculate distances and normalize it by width and height of previous detection
|
# ultimately, this should try and estimate distance in 3-dimensional space
|
||||||
ld = tracked_object.last_detection
|
# consider change in location, width, and height
|
||||||
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
|
|
||||||
|
|
||||||
# calculate euclidean distance and average
|
estimate_dim = np.diff(estimate, axis=0).flatten()
|
||||||
return np.linalg.norm(difference, axis=1).mean()
|
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])])
|
||||||
|
|
||||||
|
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):
|
class NorfairTracker(ObjectTracker):
|
||||||
@ -41,9 +65,7 @@ class NorfairTracker(ObjectTracker):
|
|||||||
# was a good reason to have different distance calculations
|
# was a good reason to have different distance calculations
|
||||||
self.tracker = Tracker(
|
self.tracker = Tracker(
|
||||||
distance_function=frigate_distance,
|
distance_function=frigate_distance,
|
||||||
# distance is relative to the size of the last
|
distance_threshold=2.5,
|
||||||
# detection
|
|
||||||
distance_threshold=4.0,
|
|
||||||
initialization_delay=0,
|
initialization_delay=0,
|
||||||
hit_counter_max=self.max_disappeared,
|
hit_counter_max=self.max_disappeared,
|
||||||
)
|
)
|
||||||
@ -210,7 +232,7 @@ class NorfairTracker(ObjectTracker):
|
|||||||
active_ids = []
|
active_ids = []
|
||||||
for t in tracked_objects:
|
for t in tracked_objects:
|
||||||
active_ids.append(t.global_id)
|
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)
|
self.register(t.global_id, t.last_detection.data)
|
||||||
# if there wasn't a detection in this frame, increment disappeared
|
# if there wasn't a detection in this frame, increment disappeared
|
||||||
elif t.last_detection.data["frame_time"] != frame_time:
|
elif t.last_detection.data["frame_time"] != frame_time:
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
from collections import defaultdict
|
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from similari import BoundingBox, PositionalMetricType, Sort, SpatioTemporalConstraints
|
||||||
|
|
||||||
from frigate.config import DetectConfig
|
from frigate.config import DetectConfig
|
||||||
from frigate.track import ObjectTracker
|
from frigate.track import ObjectTracker
|
||||||
from frigate.util import intersection_over_union
|
from frigate.util import intersection_over_union
|
||||||
from similari import Sort, BoundingBox, SpatioTemporalConstraints, PositionalMetricType
|
|
||||||
|
|
||||||
|
|
||||||
class SortTracker(ObjectTracker):
|
class SortTracker(ObjectTracker):
|
||||||
@ -156,7 +156,7 @@ class SortTracker(ObjectTracker):
|
|||||||
# get the scene_id for this label or create a new one
|
# get the scene_id for this label or create a new one
|
||||||
# TODO: consider grouping frequently swapped objects in
|
# TODO: consider grouping frequently swapped objects in
|
||||||
# in the same scene
|
# 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())
|
scene_id = len(self.scene_map.keys())
|
||||||
self.scene_map[obj[0]] = scene_id
|
self.scene_map[obj[0]] = scene_id
|
||||||
scene_detections[scene_id] = []
|
scene_detections[scene_id] = []
|
||||||
@ -200,7 +200,7 @@ class SortTracker(ObjectTracker):
|
|||||||
|
|
||||||
# update or create new tracks
|
# update or create new tracks
|
||||||
for t in 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])
|
self.register(t.id, objs[t.custom_object_id])
|
||||||
else:
|
else:
|
||||||
self.update(t.id, objs[t.custom_object_id])
|
self.update(t.id, objs[t.custom_object_id])
|
||||||
|
|||||||
@ -20,9 +20,7 @@ from frigate.log import LogPipe
|
|||||||
from frigate.motion import MotionDetector
|
from frigate.motion import MotionDetector
|
||||||
from frigate.object_detection import RemoteObjectDetector
|
from frigate.object_detection import RemoteObjectDetector
|
||||||
from frigate.track import ObjectTracker
|
from frigate.track import ObjectTracker
|
||||||
from frigate.track.centroid_tracker import CentroidTracker
|
|
||||||
from frigate.track.norfair_tracker import NorfairTracker
|
from frigate.track.norfair_tracker import NorfairTracker
|
||||||
from frigate.track.sort_tracker import SortTracker
|
|
||||||
from frigate.util import (
|
from frigate.util import (
|
||||||
EventsPerSecond,
|
EventsPerSecond,
|
||||||
FrameManager,
|
FrameManager,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user