Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Double event processing for "unzoned: allow" and having zones defined in Frigate #66

Open
freefd opened this issue May 23, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@freefd
Copy link
Contributor

freefd commented May 23, 2024

Description

The system duplicates event processing for zoned and unzoned areas while the configuration has default unzoned: allow.
This bug is floating (heisenbug).

Scene

image

Notification

ZoneA is colored by pink.
Path for ObjectA is colored by green.
image

ZoneA is colored by pink.
Path for ObjectB is colored by blue.
image

Frigate-Notify Version:

v0.2.8

Configuration

frigate:
  server: http://<frigate-instance>:5000
  ignoressl: false

  mqtt:
    enabled: true
    server: <mqtt-instance>
    port: 1883
    clientid: <frigate-notify-clientid>
    username: <frigate-notifiy-username>
    password: <frigate-notifiy-password>

alerts:
  telegram:
    enabled: true
    chatid: <telegram-chatid>
    token: <telegram-token>

Logs

ObjectA path

2024/05/23 12:40:09 Event ID 1716457201.716703-zcul86 - New event received.
2024/05/23 12:40:09 Event ID 1716457201.716703-zcul86 - Camera <camera-name> detected person in zone(s): []
2024/05/23 12:40:09 Event ID 1716457201.716703-zcul86 - Start time: 2024-05-23 12:40:01 +0000 UTC
2024/05/23 12:40:10 Event ID 1716457201.716703-zcul86 - Telegram alert sent
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Event updated from Frigate.
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Camera <camera-name> detected person in zone(s): [<zoneA-name>]
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Start time: 2024-05-23 12:40:01 +0000 UTC
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Entered new zone: <zoneA-name>
2024/05/23 12:40:13 Event ID 1716457201.716703-zcul86 - Telegram alert sent

ObjectB path

2024/05/27 15:06:23 Event ID 1716811576.873335-lltf2e - New event received.
2024/05/27 15:06:23 Event ID 1716811576.873335-lltf2e - Camera <camera-name> detected person in zone(s): []
2024/05/27 15:06:23 Event ID 1716811576.873335-lltf2e - Start time: 2024-05-27 15:06:16 +0000 UTC
2024/05/27 15:06:24 Event ID 1716811576.873335-lltf2e - Telegram alert sent
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Event updated from Frigate.
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Camera <camera-name> detected person in zone(s): [zoneA-name]
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Start time: 2024-05-27 15:06:16 +0000 UTC
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Entered new zone: zoneA-name
2024/05/27 15:06:26 Event ID 1716811576.873335-lltf2e - Telegram alert sent

Expected behavior

The system processes a particular event only once and regardless of existing zones if configuration unzoned: allow is set.

@freefd freefd changed the title Bug: Double event processing for "unzoned: allow" Bug: Double event processing for "unzoned: allow" and having zones defined in Frigate May 23, 2024
@0x2142 0x2142 added the bug Something isn't working label Jun 3, 2024
@0x2142
Copy link
Owner

0x2142 commented Jun 3, 2024

Hmm - So based on the logs, it seems like an initial notification is sent out when the object is outside a zone - then a follow up once the object enters the zone. Is the issue that the snapshots are identical between them? It looks like it for me, but I want to confirm since they've been pixelated for the issue upload.

If so, I wonder if this might be an issue of grabbing the snapshot from Frigate too quickly? Maybe Frigate hasn't updated the snapshot yet by the time we grab it for the notification.

@freefd
Copy link
Contributor Author

freefd commented Jun 4, 2024

Hi @0x2142,

Is the issue that the snapshots are identical between them?

That's the thing, they're not identical, but only slightly different, as expected.

It looks like it for me, but I want to confirm since they've been pixelated for the issue upload.

Those snapshots were pixelated intentionally (today's internet is not about privacy at all).

I see the behavior as follows:


  1. ObjectA appears in ZoneA.
  2. Frigate detects ObjectA and sends MQTT message with zone included.
  3. ObjectA has left ZoneA but is still in the camera's FOV.
  4. Frigate detects ObjectA again and sends MQTT message without a zone.

  1. ObjectB appears outside ZoneA.
  2. Frigate detects ObjectB and sends MQTT message without a zone.
  3. ObjectB appears in ZoneA.
  4. Frigate detects ObjectB and sends MQTT message with zone included.

The trouble may be hiding under https://github.com/0x2142/frigate-notify/blob/main/events/mqtt.go#L98-L100 where transitions Zone -> Unzoned and Unzoned -> Zone causing zoneChanged = true and dropping the https://github.com/0x2142/frigate-notify/blob/main/events/mqtt.go#L109 logic due to !zoneChanged didn't happen.

Firstly, good question here: is this Zone <-> Unzoned logic also treated as a zone change or is it just a small design gap, especially since the unzoned: allow parameter is set by default.

Secondly, realizing the difficulty of how to properly calculate zoned and unzoned events that are too close to each other, even if they come as two different event records, I feel that the key answer may be stored in event.After.Region[] coordinates.
Some algorithm for calculating the distance between polygons may be needed here: smaller numbers may mean that the object is simply moving from a zoned region to an unzoned region and vice versa.

@bobemoe
Copy link

bobemoe commented Oct 11, 2024

I don't fully understand the issue here, is it that two notifications are being sent for each object? I think with unzoned: allow that is expected? When they are first seen zoned, and first seen unzoned? Did you try unzoned: drop which should suppress the notification in the unzoned area giving you just the one notification you desire?

What version of frigate was/is this issue with? Lots has changed in 0.14 although I'm not familiar with pre 0.14

Why object A and object B? This added an element of confusion for me, I first thought they were happening simultaneously and the tracking is mixing them up, but looking at your timestamps they are days apart. I assume it is just to show that the issue occurs in either direction? Or is the issue just in one direction?

This matches what I would expect from your scene diagram:

ObjectA appears in ZoneA.
Frigate detects ObjectA and sends MQTT message with zone included.
ObjectA has left ZoneA but is still in the camera's FOV.
Frigate detects ObjectA again and sends MQTT message without a zone.

ObjectB appears outside ZoneA.
Frigate detects ObjectB and sends MQTT message without a zone.
ObjectB appears in ZoneA.
Frigate detects ObjectB and sends MQTT message with zone included.

However this doesnt:

ObjectA path

2024/05/23 12:40:09 Event ID 1716457201.716703-zcul86 - New event received.
2024/05/23 12:40:09 Event ID 1716457201.716703-zcul86 - Camera detected person in zone(s): []
2024/05/23 12:40:09 Event ID 1716457201.716703-zcul86 - Start time: 2024-05-23 12:40:01 +0000 UTC
2024/05/23 12:40:10 Event ID 1716457201.716703-zcul86 - Telegram alert sent
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Event updated from Frigate.
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Camera detected person in zone(s): [zoneA-name]
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Start time: 2024-05-23 12:40:01 +0000 UTC
2024/05/23 12:40:12 Event ID 1716457201.716703-zcul86 - Entered new zone: zoneA-name
2024/05/23 12:40:13 Event ID 1716457201.716703-zcul86 - Telegram alert sent

ObjectB path

2024/05/27 15:06:23 Event ID 1716811576.873335-lltf2e - New event received.
2024/05/27 15:06:23 Event ID 1716811576.873335-lltf2e - Camera detected person in zone(s): []
2024/05/27 15:06:23 Event ID 1716811576.873335-lltf2e - Start time: 2024-05-27 15:06:16 +0000 UTC
2024/05/27 15:06:24 Event ID 1716811576.873335-lltf2e - Telegram alert sent
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Event updated from Frigate.
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Camera detected person in zone(s): [zoneA-name]
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Start time: 2024-05-27 15:06:16 +0000 UTC
2024/05/27 15:06:25 Event ID 1716811576.873335-lltf2e - Entered new zone: zoneA-name
2024/05/27 15:06:26 Event ID 1716811576.873335-lltf2e - Telegram alert sent

Why the mismatch here? From the scene diagram and MQTT description ObjectA is meant to appear first in ZoneA, but from this log it seems reversed, appearing in no zones and then leaving through ZoneA - the same path as ObjectB. Is this the issue you are experiencing?

@freefd
Copy link
Contributor Author

freefd commented Oct 12, 2024

@bobemoe,

The occurrences of object A and object B do not happen at the same time, they are separated in time.
The problem is duplicate events if the zone is present, but it is allowed to send notifications ignoring the zone.
Essentially, Frigate will issue two events in a row, one with a zone and one without. And there will be two notifications from frigate-notify as well.

But from a physics pov, it's just one object moving in and out of the zone.

Perhaps you're right, and it's the time to try Frigate 0.14+ version again (even though its webui has become even more uncomfortable for me).

@bobemoe
Copy link

bobemoe commented Oct 12, 2024

Yeah I think two notifications is expected behaviour. Its what I am seeing and expected anyway.

To get just one notification per object, ignoring zones even though you have one set, I think you either need to:

alerts:
  zones:
    unzoned: drop

this would not notify the unzoned event and only notify when object enters the zoneA

or

alerts:
  zones:
    unzoned: allow
    block:
     - zoneA

this would notify the unzoned event but not notify when object is in zoneA

or

alerts:
  general:
    notify_once: true
  zones:
    unzoned: allow

this would notify when the object enters FOV at all, zoned or unzoned, but only once per object

I have not fully tested these but this is what I understood as expected behaviour from reading the docs (@0x2142 correct me if I'm wrong)

@freefd
Copy link
Contributor Author

freefd commented Oct 12, 2024

@bobemoe,

what I understood as expected behaviour from reading the docs

The issue is not how it works now, the issue is how to achieve the expected behavior when an object is tracked from zoned to unzoned (or from unzoned to zoned) with unzoned: allow and only get notified once.

Of course, we could try to create a complex template and try to reach the specified behavior there, but there's a “no coding in configurations” rule, so I treat this as a workaround rather than the main approach.

@bobemoe
Copy link

bobemoe commented Oct 12, 2024

Don't all of the configurations I suggested above notify you only once? And give you the choice to configure the behaviour when an object is tracked from zoned to unzoned (or from unzoned to zoned) in all possible ways? (Other than specifically determining which direction it is going in)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants