Coder Social home page Coder Social logo

pr0gstar / mqtt-camera-streamer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robmarkcole/mqtt-camera-streamer

1.0 1.0 0.0 2.15 MB

Stream images from a connected camera over MQTT & view using Streamlit

License: GNU General Public License v3.0

Python 100.00%

mqtt-camera-streamer's Introduction

mqtt-camera-streamer

TLDR: Publish frames from a connected camera (or MJPEG/RTSP stream) to an MQTT topic, and demonstrate how this can be used to create a simple image processing pipeline that can be used in an IOT or home automation project. The camera stream can be viewed in a browser with Streamlit or Home Assistant.

Long introduction: A typical task in IOT/science is that you have a camera connected to one machine and you want to view the camera feed on a different machine, and maybe preprocess the images and save them to disk. I have always found this to be may more work than expected. In particular working with camera streams can get quite complicated, and may force you to experiment with tools like Gstreamer and ffmpeg that have a steep learning curve. In contrast, working with MQTT is very straightforward and is also probably familiar to anyone with an interest in IOT. This code uses MQTT to send frames from a camera over a netork at low FPS. Whilst MQTT is rarely used for this purpose (sending files) I have not encountered any issues doing this. Furthermore it is possible to setup an image processing pipeline by linking MQTT topics together, using an on_message(topic) to do some processing and send the processed image downstream on another topic. Note that this is not a high FPS (frames per second) solution, and in practice I achieve around 1 FPS which is practical for tasks such as preprocessing (cropping, rotating) images prior to viewing them. The code is written for simplicity and ease of use, not high performance. If you require high performance/low latency checkout Frigate instead.

Setup

On linux/OSX/Windows use a venv to isolate your environment, and install the required dependencies:

$ (base) python3 -m venv venv
$ (base) source venv/bin/activate
$ (venv) pip3 install -r requirements.txt

Raspberry Pi

Do not use a venv and install openCV system wide using:

$ sudo apt install python3-opencv
$ pip3 install -r requirements.txt

I have not tested Streamlit on the Raspberry pi, but you can use the viewer on another machine (WIndows, OSX) anyways so don't worry.

Camera usage

Use the config.yaml file in config directory to setup the system (mqtt broker IP etc) and validate the config by running:

$ (venv) python3 scripts/validate_config.py

To publish camera frames over MQTT:

$ (venv) python3 scripts/camera.py

To view the camera frames with Streamlit:

$ (venv) streamlit run scripts/viewer.py

Note: if streamlit becomes unresponsive, ctrl-z to pause Streamlit then kill -9 %%. Also note that the viewer can be run on amy machine on your network.

Image processing pipeline

To process a camera stream (the example rotates the image):

$ (venv) python3 scripts/processing.py

Save frames

To save frames to disk:

$ (venv) python3 scripts/save-captures.py

Camera display

The viewer.py script uses Streamlit to display the camera feed:

Home Assistant

You can view the camera feed using Home Assistant and configuring an MQTT camera. Add to your configuration.yaml:

camera:
  - platform: mqtt
    topic: homie/mac_webcam/capture
    name: mqtt_camera
  - platform: mqtt
    topic: homie/mac_webcam/capture/rotated
    name: mqtt_camera_rotated

Listing cameras

If your laptop has a built-in webcam this will generally be listed as VIDEO_SOURCE = 0. If you plug in an external USB webcam this takes precedence over the inbuilt webcam, with the external camera becoming VIDEO_SOURCE = 0 and the built-in webcam becoming VIDEO_SOURCE = 1. To check which cameras are detected run:

$ (venv) python3 scripts/check-cameras.py

Alternatively you can pass a string to an MJPEG/RTSP stream, For example "rtsp://admin:[email protected]:554/11"

MQTT

Need an MQTT broker? If you have Docker installed I recommend eclipse-mosquitto. A basic broker can be run with

$ docker run --p 1883:1883 -d eclipse-mosquitto`

Note that I have stuctured the MQTT topics following the homie MQTT convention, link in the refefrences. This is not necessary but is best practice IMO.

References

mqtt-camera-streamer's People

Contributors

pr0gstar avatar robmarkcole avatar

Stargazers

 avatar

Watchers

 avatar

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.