Skip to content

Commit

Permalink
Ability to configure min frames for zone presence (blakeblackshear#6680)
Browse files Browse the repository at this point in the history
* Objects need to be in zones multiple times to be considered present in the zone

* Add a field to configure inertia per zone

* Formatting

* Use correct default method

* Clarify zone presence behavior

Co-authored-by: Blake Blackshear <[email protected]>

---------

Co-authored-by: Blake Blackshear <[email protected]>
  • Loading branch information
NickM-27 and blakeblackshear committed Jun 11, 2023
1 parent fd6eb78 commit b160aba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ cameras:
# Required: List of x,y coordinates to define the polygon of the zone.
# NOTE: Presence in a zone is evaluated only based on the bottom center of the objects bounding box.
coordinates: 545,1077,747,939,788,805
# Optional: Number of consecutive frames required for object to be considered present in the zone. Allowed values are 1-10 (default: shown below)
inertia: 3
# Optional: List of objects that can trigger this zone (default: all tracked objects)
objects:
- person
Expand Down
6 changes: 6 additions & 0 deletions frigate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ class ZoneConfig(BaseModel):
coordinates: Union[str, List[str]] = Field(
title="Coordinates polygon for the defined zone."
)
inertia: int = Field(
default=3,
title="Number of consecutive frames required for object to be considered present in the zone.",
gt=0,
le=10,
)
objects: List[str] = Field(
default_factory=list,
title="List of objects that can trigger the zone.",
Expand Down
22 changes: 17 additions & 5 deletions frigate/object_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(
self.colormap = colormap
self.camera_config = camera_config
self.frame_cache = frame_cache
self.zone_presence = {}
self.current_zones = []
self.entered_zones = []
self.false_positive = True
Expand Down Expand Up @@ -144,13 +145,24 @@ def update(self, current_frame_time, obj_data):
if len(zone.objects) > 0 and obj_data["label"] not in zone.objects:
continue
contour = zone.contour
zone_score = self.zone_presence.get(name, 0)
# check if the object is in the zone
if cv2.pointPolygonTest(contour, bottom_center, False) >= 0:
# if the object passed the filters once, dont apply again
if name in self.current_zones or not zone_filtered(self, zone.filters):
current_zones.append(name)
if name not in self.entered_zones:
self.entered_zones.append(name)
self.zone_presence[name] = zone_score + 1

# an object is only considered present in a zone if it has a zone inertia of 3+
if zone_score >= zone.inertia:
# if the object passed the filters once, dont apply again
if name in self.current_zones or not zone_filtered(
self, zone.filters
):
current_zones.append(name)
if name not in self.entered_zones:
self.entered_zones.append(name)
else:
# once an object has a zone inertia of 3+ it is not checked anymore
if 0 < zone_score < zone.inertia:
self.zone_presence[name] = zone_score - 1

if not self.false_positive:
# if the zones changed, signal an update
Expand Down

0 comments on commit b160aba

Please sign in to comment.