Skip to main content

Stream live video feed from a webpage to a server-side python openCV script

Project description

webopencv · demo · 1-click setup

Stream webcam from a webpage to a server-side OpenCV Python script. This gives you the ability to work with a webcam in Python, without installing anything on your computer.

🎥 Live demo: webopencv.glitch.me

👉 1-click WebOpenCV Setup: Fork on Glitch

💻 View demo source: on Glitch or on Github

created by Alvin Wan, for an online computer vision tutorial

Why Use WebOpenCV?

WebOpenCV makes it easy for anyone to start working with their own webcam, in Python. Before WebOpenCV, you would need to (1) install packages, their package managers, and miscellaneous tools on your own computer, then (2) pray that webcam access worked. Now, you simply click once to launch a free, remote server that comes pre-setup. No installation on your own computer necessary.

Getting Started

For the 1-click WebOpenCV setup, fork on Glitch.

Alternatively, to setup locally on your machine instead, install the Python package.

pip install webopencv

Create a new file app.py.

import webopencv as wcv

app = wcv.WebApplication()

@app.transform('Hello')
def helloworld(img, frame):
    return img

if __name__ == '__main__':
    app.run()

Then, run the file.

python app.py

This launches a web server by default at https://localhost:8080. Navigate to that URL, and hit "Start" to see the demo in action. Note: When developing locally, navigating to https://0.0.0.0:8080 won't work. Make sure to use localhost.

Transforms

Create transforms, or hooks that process images in a real-time video feed. Each transform takes in an

  1. img: numpy array image
  2. frame: VideoFrame object that additionally contains metadata, like time

Like with Flask routes, you can register transforms using a decorator. Add whatever processing you would like to the transform, and return the image at the end. For example, the below adds a "cinematic" crop to the live feed, by adding black bars.

@app.transform('Cinematic')
def cinematic(img, frame):
    h, w, _ = img.shape
    img[-w//4:] = 0
    img[:w//4] = 0

Default Transform: Use default=True in the transform decorator to set a transform as the default on page load. Note that only 1 transform can be set as default. If no transform has default=True set, the default is no transform on page load.

Customize Homepage

To build a custom homepage:

  1. Initialize the app without the default homepage. You can use either the aiohttp or Flask backends.
  2. Add your own homepage with:
    • WebOpenCV's client-side Javascript: <script src="/client.js"></script>
    • a video tag with id="video", for the webcam feed: <video id="video" autoplay="true" playsinline="true"></video>
    • a button tag with id="action", for "Start" and "Stop": <button id="action"></button>

aiohttp: The default backend uses aiohttp, so you can treat app as you would any other web.Application object.

from aiohttp import web
import webopencv as wcv

app = wcv.WebApplication(use_default_homepage=False)

html = """
<html>
    Custom webpage
    <button id="action"></button>
    <video id="video" autoplay="true" playsinline="true"></video>
    <script src="/client.js"></script>
</html>
"""

def index(app):
    return web.Response(text=html, content_type="text/html")

if __name__ == '__main__':
    app.router.add_get_url('/', index)
    app.run()

Flask: You can alternatively use the Flask backend, treating the app as you would any other Flask object. Note that the Flask implementation drops the ICE connection. Needs debugging.

import webopencv as wcv

app = wcv.Flask(use_default_homepage=False)

@app.route("/")
def index():
    return """
    <html>
        Custom webpage
        <button id="action"></button>
        <video id="video" autoplay="true" playsinline="true"></video>
        <script src="/client.js"></script>
    </html>
    """

if __name__ == '__main__':
    app.run()

Acknowledgments: This library was built off of the aiortc official server example.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webopencv-0.0.4.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

webopencv-0.0.4-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file webopencv-0.0.4.tar.gz.

File metadata

  • Download URL: webopencv-0.0.4.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

File hashes

Hashes for webopencv-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8d1460194dac4974139e131152db0c0aa92b480ebd17846dc8261185c4895a37
MD5 f287786c165dda98661c1ad620b9e1be
BLAKE2b-256 822013926db13afb0d80abf261f5ddf3472257cc4cc3261a23b48cf8004275d5

See more details on using hashes here.

File details

Details for the file webopencv-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: webopencv-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

File hashes

Hashes for webopencv-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 97e52a82392e1bd93f13938c3ebffd897fc9a3ca3eb976ac29cbe400d5b0eabe
MD5 4faa6ccc9525af88f6a52f548f41eab3
BLAKE2b-256 d4c92107b2e15c83adbceda32690676742f31f2b81e5fcee10eb1e0104c64ee6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page