blakeblackshear / frigate

NVR with realtime local object detection for IP cameras
https://frigate.video
MIT License
18.23k stars 1.66k forks source link

[Support]: Lighting automation causes infinite detection loop #2521

Closed Tuckie closed 2 years ago

Tuckie commented 2 years ago

Describe the problem you are having

The situation:

  1. Camera pointed on the driveway
  2. Home assistant automation that turns on driveway lights for 1 minute that triggers on vehicle detection

The Problem:

A vehicle sitting on the driveway at night starts an infinite detection loop, as the lights turning off triggers a new detection event.

Any ideas on how to tune my install to prevent this from happening?

Version

0.9.4

Frigate config file

mqtt:
  host: thehost
  user: frigate
  password: thepassword

detectors:
  coral:
    type: edgetpu
    device: pci:0

cameras:
  Front Door:
    ffmpeg:
      inputs:
        - path: rtsp://cam1:554/live
          roles:
            - detect
        - path: rtsp://cam1:554/live
          roles:
            - rtmp
            - record
    detect:
      width: 1920 #704
      height: 1080 #480
    motion:
        mask:
          - 1263,104,1855,105,1855,35,1262,38 # timestamp
    objects:
      track:
        - person
        - cat
  Driveway:
    ffmpeg:
      inputs:
        - path: rtsp://cam2:554/live
          roles:
            - detect
        - path: rtsp://cam2:554/live
          roles:
            - rtmp
            - record
    detect:
      width: 1920 #704
      height: 1080 #480
    motion:
        mask:
          - 789,328,377,401,376,122,794,132
          - 205,217,269,209,262,47,204,44
          - 1920,145,1920,375,1750,374,1063,321,1089,182
          - 1263,104,1855,105,1855,35,1262,38 #timestamp
    objects:
      track:
        - person
        - car
        - cat
  Backyard:
    ffmpeg:
      inputs:
        - path: rtsp://cam3:554/live
          roles:
            - detect
        - path: rtsp://cam3:554/live
          roles:
            - rtmp
            - record
    detect:
      width: 1920 #704
      height: 1080 #480
    motion:
        mask:
          - 1263,104,1855,105,1855,35,1262,38 #timestamp
    objects:
      track:
        - person
        - cat
        - dog
        - bird
        - bear
snapshots:
      # Optional: Enable writing jpg snapshot to /media/frigate/clips (default: shown below)
      # This value can be set via MQTT and will be updated in startup based on retained value
      enabled: True
      # Optional: print a timestamp on the snapshots (default: shown below)
      timestamp: False
      # Optional: draw bounding box on the snapshots (default: shown below)
      bounding_box: True
      # Optional: crop the snapshot (default: shown below)
      crop: False
      # Optional: height to resize the snapshot to (default: original size)
      #height: 175
      # Optional: Restrict snapshots to objects that entered any of the listed zones (default: no required zones)
      required_zones: []
      # Optional: Camera override for retention settings (default: global values)
      retain:
        # Required: Default retention days (default: shown below)
        default: 10
birdseye:
  # Optional: Enable birdseye view (default: shown below)
  enabled: True
  # Optional: Width of the output resolution (default: shown below)
  width: 1920
  # Optional: Height of the output resolution (default: shown below)
  height: 1080
  # Optional: Encoding quality of the mpeg1 feed (default: shown below)
  # 1 is the highest quality, and 31 is the lowest. Lower quality feeds utilize less CPU resources.
  quality: 8
  # Optional: Mode of the view. Available options are: objects, motion, and continuous
  #   objects - cameras are included if they have had a tracked object within the last 30 seconds
  #   motion - cameras are included if motion was detected in the last 30 seconds
  #   continuous - all cameras are included always
  mode: objects

# Optional: Record configuration
# NOTE: Can be overridden at the camera level
record:
  # Optional: Enable recording (default: shown below)
  enabled: True
  # Optional: Number of days to retain recordings regardless of events (default: shown below)
  # NOTE: This should be set to 0 and retention should be defined in events section below
  #       if you only want to retain recordings of events.
  retain_days: 0
  # Optional: Event recording settings
  events:
    # Optional: Maximum length of time to retain video during long events. (default: shown below)
    # NOTE: If an object is being tracked for longer than this amount of time, the retained recordings
    #       will be the last x seconds of the event unless retain_days under record is > 0.
    max_seconds: 300
    # Optional: Number of seconds before the event to include (default: shown below)
    pre_capture: 5
    # Optional: Number of seconds after the event to include (default: shown below)
    post_capture: 5
    # Optional: Objects to save recordings for. (default: all tracked objects)

    # Optional: Restrict recordings to objects that entered any of the listed zones (default: no required zones)
    required_zones: []
    # Optional: Retention settings for recordings of events
    retain:
      # Required: Default retention days (default: shown below)
      default: 3
      # Optional: Per object retention days
      objects:
        person: 3

Relevant log output

n/a

FFprobe output from your camera

n/a

Frigate stats

n/a

Operating system

HassOS

Install method

HassOS Addon

Coral version

PCIe

Network connection

Wired

Camera make and model

Dahua

Any other information that may be helpful

No response

blakeblackshear commented 2 years ago

Require a zone or multiple zones in the conditions. https://docs.frigate.video/guides/stationary_objects

Note that 0.10.0 will track the car continuously.

Tuckie commented 2 years ago

Thank you for the super quick response! I started playing with zones, although I'm not sure how well it's going to work due to the downward slope of my drive. I'm currently using the frigate-hass-integration, is there a way to replicate the mqtt entered_zones concept? (I haven't really done much with MQTT events)

blakeblackshear commented 2 years ago

I would recommend using the mqtt topic for notifications rather than the sensors provided in the integration: https://docs.frigate.video/guides/ha_notifications

Tuckie commented 2 years ago

Unfortunately it looks like I ran into a snag unmasking the street in order to get the bottom half of the driveway (again, the bottom half is basically a few pixels tall as it slopes down to the street). Now, frigate captures a video of every car that drives past and it fills up my disk.

Basically I would need the same exact type of entered_zones concept for recordings and snapshots as well.

Any other ideas on tackling this? Is there a motion setting I should tweak?

blakeblackshear commented 2 years ago

Motion settings won't help with object detection. You need to use required_zones for events and snapshots to prevent cars on the street from creating events.

Tuckie commented 2 years ago

required_zones appears to be an or instead of and.

  Driveway:
    ffmpeg:
      inputs:
        - path: rtsp://cam:554/live
          roles:
            - detect
        - path: rtsp://cam:554/live
          roles:
            - rtmp
            - record
    detect:
      width: 1920 #704
      height: 1080 #480
    motion:
        mask:
          - 789,328,377,401,376,122,794,132 # garage door
          - 205,217,269,209,262,47,204,44 # window
          #- 1920,145,1920,375,1750,374,1063,321,1089,182 # street
          - 1263,104,1855,105,1855,35,1262,38 #timestamp
    zones:
      main_drive:
        coordinates: 1920,652,402,348,376,148,797,132,1244,167,1260,343,1920,398
      street:
        coordinates: 1225,185,1920,224,1920,390,1213,343
      detached_garage:
        coordinates: 0,508,482,404,1920,541,1920,1080,0,1080
    objects:
      track:
        - person
        - car
        - cat
    record:
      events:
        required_zones:
          - street
          - main_drive
    snapshots:
      required_zones:
        - street
        - main_drive

image

blakeblackshear commented 2 years ago

That's right. Only list the zones where you want snapshots and recordings. If you don't want cars that only drive on the street, then don't list street. Split your driveway into two zones and make them both required.

Don't forget that the zone is evaluated based on the bottom center of the bounding box. It doesn't matter if any other part of the bounding box overlaps with the zone.

yury-sannikov commented 2 years ago

I had the same issue before Frigate. Any light action was detected by the ONVIF camera as a motion and turned on lights again. What you could do is to check the last light on event time and block the Frigate action propagation in the HA rules. You can use something like this in HA:

- alias: "Camera person detection sync"
  trigger:
    platform: mqtt
    topic: frigate/events
  condition:
    - condition: template
      value_template: '{{ trigger.payload_json["type"] == "end" }}'
  action:
    - delay: 00:00:01
    - service: input_boolean.turn_on
      data_template:
        entity_id: 'input_boolean.{{ trigger.payload_json.get("after",{}).get("camera") }}_person_detected'
    - delay: 00:00:10
    - service: input_boolean.turn_off
      data_template:
        entity_id: 'input_boolean.{{ trigger.payload_json.get("after",{}).get("camera") }}_person_detected'
  mode: single

- alias: Frigate alerts
  description: Person/car detection in cameras from Frigate
  trigger:
    platform: mqtt
    topic: frigate/events
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: '{{ trigger.payload_json["type"] == "end" }}'
    - condition: template
      value_template: >
        {% set name = trigger.payload_json.get("after",{}).get("camera") %}
        {{ (now().timestamp() - as_timestamp(states.input_boolean[name+'_person_detected'].last_changed)) > 1800 }}
  action:
    - service: notify.telegram_bot
      data:
        title: Alert at camera {{trigger.payload_json.get("after",{}).get("camera")}}
        message: Person detected
        data:
          photo:
            - url: http://x.x.x.x:5000/clips/{{trigger.payload_json.get("after",{}).get("camera")}}-{{trigger.payload_json.get("after",{}).get("id")}}.jpg
              caption: Человек на камере {{trigger.payload_json.get("after",{}).get("camera").replace("_","-")}}

This code block uses 2 fake input_boolean's to track the last detection event time in the first rule for 2 cameras. Then the second rule gets blocked if the following event appears within 30 minutes. That way I send Telegram messages with the screenshot of the person in my garden if no person was detected at least for 30 mins.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.