Git Product home page Git Product logo

hello-websocket's Introduction

hello-websocket

Webcam over websocket in Python, using OpenCV and Tornado.

Details

The code runs a recorder process that continuously reads images from the webcam. Upon every capture it writes the image to a Redis key-value store.

Separately, a server process (running Tornado) handles websocket messages. Upon receiving a request message (sent from client web browser) it retrieves the latest image from the Redis database and sends it to the client over websocket connection.

https://raw.github.com/vmlaker/hello-websocket/master/diagram.png

The client web page is dead simple: It sends an initial request on a WebSocket. When image data comes in, it assigns it to src attribute of the <img> tag, then simply sends the next request. That's it!

Installation

The code uses Python and third-party modules installed in a virtualenv with pip. But since OpenCV is not part of the Python Package Index, you're gonna need to install it system-wide. (Later below, make will manually pull the library into the virtual environment):

apt-get install python-opencv

Also install Redis server:

apt-get install redis-server

Now go ahead and grab the source code repo:

git clone https://github.com/vmlaker/hello-websocket
cd hello-websocket

Build the virtual environment with all needed modules:

make

Usage

There are two separate programs that need to be running:

  1. recorder - webcam capture process that writes to Redis database.
  2. server - the Tornado server which reads current image from the Redis database and serves to requesting WebSocket clients.

First run the recorder:

make recorder

Now (in a different shell) run the server:

make server

Go to http://localhost:9000 to view the webcam.

systemd services

If your O/S has systemd you have the option of installing recorder and server as systemd services. Begin by customizing settings in file systemd/hello.conf. Then, from the project root directory, generate your service files:

python systemd/create.py

Install your newly-created services into your systemd location:

sudo cp `pwd`/systemd/*.service /usr/lib/systemd/system/

You can now start the two services by starting the server (recorder is a dependency, and will start automatically):

sudo systemctl start hws-server

To shut down all three services, just stop the mapper (recorder and server depend on mapper, and will stop automatically):

sudo systemctl stop hws-mapper

hello-websocket's People

Contributors

vmlaker avatar

Watchers

 avatar  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.