Browse Source

send the best person frame over mqtt for faster updates in homeassistant

blakeblackshear 5 years ago
parent
commit
2ec45cd1b6
3 changed files with 33 additions and 5 deletions
  1. 22 2
      README.md
  2. 10 2
      frigate/mqtt.py
  3. 1 1
      frigate/video.py

+ 22 - 2
README.md

@@ -61,8 +61,8 @@ Access the mjpeg stream at `http://localhost:5000/<camera_name>` and the best pe
 ```
 camera:
   - name: Camera Last Person
-    platform: generic
-    still_image_url: http://<ip>:5000/<camera_name>/best_person.jpg
+    platform: mqtt
+    topic: frigate/<camera_name>/snapshot
 
 binary_sensor:
   - name: Camera Person
@@ -71,6 +71,26 @@ binary_sensor:
     value_template: '{{ value_json.person }}'
     device_class: motion
     availability_topic: "frigate/available"
+
+automation:
+  - alias: Alert me if a person is detected while armed away
+    trigger: 
+      platform: state
+      entity_id: binary_sensor.camera_person
+      from: 'off'
+      to: 'on'
+    condition:
+      - condition: state
+        entity_id: alarm_control_panel.home_alarm
+        state: armed_away
+    action:
+      - service: notify.user_telegram
+        data:
+          message: "A person was detected."
+          data:
+            photo:
+              - url: http://<ip>:5000/<camera_name>/best_person.jpg
+                caption: A person was detected.
 ```
 
 ## Tips

+ 10 - 2
frigate/mqtt.py

@@ -1,13 +1,15 @@
 import json
+import cv2
 import threading
 
 class MqttObjectPublisher(threading.Thread):
-    def __init__(self, client, topic_prefix, objects_parsed, detected_objects):
+    def __init__(self, client, topic_prefix, objects_parsed, detected_objects, best_person_frame):
         threading.Thread.__init__(self)
         self.client = client
         self.topic_prefix = topic_prefix
         self.objects_parsed = objects_parsed
         self._detected_objects = detected_objects
+        self.best_person_frame = best_person_frame
 
     def run(self):
         last_sent_payload = ""
@@ -30,4 +32,10 @@ class MqttObjectPublisher(threading.Thread):
             new_payload = json.dumps(payload, sort_keys=True)
             if new_payload != last_sent_payload:
                 last_sent_payload = new_payload
-                self.client.publish(self.topic_prefix+'/objects', new_payload, retain=False)
+                self.client.publish(self.topic_prefix+'/objects', new_payload, retain=False)
+                # send the snapshot over mqtt as well
+                if not self.best_person_frame.best_frame is None:
+                    ret, jpg = cv2.imencode('.jpg', self.best_person_frame.best_frame)
+                    if ret:
+                        jpg_bytes = jpg.tobytes()
+                        self.client.publish(self.topic_prefix+'/snapshot', jpg_bytes, retain=True)

+ 1 - 1
frigate/video.py

@@ -175,7 +175,7 @@ class Camera:
         self.object_cleaner.start()
 
         # start a thread to publish object scores (currently only person)
-        mqtt_publisher = MqttObjectPublisher(self.mqtt_client, self.mqtt_topic_prefix, self.objects_parsed, self.detected_objects)
+        mqtt_publisher = MqttObjectPublisher(self.mqtt_client, self.mqtt_topic_prefix, self.objects_parsed, self.detected_objects, self.best_person_frame)
         mqtt_publisher.start()
 
         # create a watchdog thread for capture process