Code Monkey home page Code Monkey logo

yolo2mqtt's Introduction

Yolo2Mqtt

Track objects detected by YOLO and publish that info to MQTT

Description

Yolo2Mqtt is a collection of python scripts for running image recognition on RTSP camera streams using a Yolov5 model, detecting when two objects interact, saving a video of the event, using MQTT as a communications mechanism.

This program was written to track cat feeding/drinking in a multi-cat home and to graph provide that information to Home Assistant via MQTT. Tracking both the cats and the food/water bowls allowed the bowls to be moved about the room, rather than designating fixed windows to trigger evnets.

It could be used for pretty much anything in which you want to run YOLO and detect if two objects are overlapping.

Running

Docker is the easiest way to run this. An MQTT broker is required as well. The sample docker-compose will run an MQTT broker, but for Home Assistant integration the same broker must be used by HA and Yolo2Mqtt

The minimal commands to get running would are:

$ git clone --recursive https://github.com/cobryan05/Yolo2Mqtt.git
$ cd Yolo2Mqtt
Yolo2Mqtt$ docker-compose build
Yolo2Mqtt$ docker-compose up

This will build the docker container and run the default configuration.

The default configuration will use YoloV8 and auto-download a sample model then run sample recognitions on a static image. If all goes well, you should be able to connect to the mqtt broker at <hostIp>:1833. Detection information should be published to the topic /myhome/ObjectTrackers/.

With the default sample config you should soon see topics identifying a dog riding a motorcycle.

Configuration

Configuration is done by editing /config/config.yml. A default config.yml is available as config.defaults.yml. You should configure the volumes in your docker-compose.yml so that so that your custom config.yml is available to it at /config/config.yml.

All settings and default values (if any) are commented in config.defaults.yml.

Note: This is meant to be used with a custom-trained YOLOv5 model. The default configuration will download the base yolov5 model, and the example interactions are some nonsense made with default labels. Any useful interactions will require a custom model.

CUDA GPU acceleration

To enable CUDA acceleration (assuming CUDA is already working on your system): * At the top of the Dockerfile change the FROM: image to the -cuda variant (then rebuild the image) * In the docker-compose.yml uncomment the 'deploy' block to pass the GPU to Docker * In the config.yml switch the 'yolo: device:' setting to 'cuda'

RTSP Proxy

The Docker container internally runs an RTSP Proxy on port 8554. If a port is forwarded to 8554 on Docker container, then any RTSP camera in the config.yml should be proxied at, eg <hostIp>:8554/<cameraName>

Home Assistant Integration

There are two ways in which Yolo2Mqtt can be 'integrated' with Home Assistant.

In Home Assistant you can create 'aggregation sensors' to help process further process these events. For example, adding this template sensor to Home Assistant:

binary_sensor:
 - platform: template
   sensors:
     cattracker_summary_sammy_eating_food:
       friendly_name: "Sammy Eating Food"
       unique_id: "cattracker_summary_sammy_food"
       value_template: >-
         {% set action='cateatingfood' %}
         {% set actor='sammy' %}

         {% set re=action + '[a-z_]+' + actor %}
         {% set ns = namespace(state='off') %}

         {% for entity in states.binary_sensor if entity|regex_search( re ) and entity.state=='on'%}
           {% set ns.state = 'on' %}
         {% endfor %}
         {{ ns.state }}

will create a new binary_sensor.cattracker_summary_sammy_eating_food which will be 'true' if any sensor matches the regex 'sammy[a-z]+cateatingfood'. Multiple yolo2mqtt 'events' can be combined into one logical Home Assistant event using tricks like this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.