Skip to main content

st-webcam

Effortless webcam integration for computer vision projects with Streamlit

st-webcam is a Python package designed to simplify computer vision projects, providing an easy-to-use interface for common computer vision tasks, such as accessing and displaying webcam feeds, applying basic image processing techniques, and integrating with popular libraries like OpenCV and Streamlit. It is perfect for anyone who wants to get started quickly with computer vision applications without dealing with the complexities of managing camera devices and frame handling.

This package contains WebCam, which is a Python class designed to make webcam integration with Streamlit simple and effective. It abstracts away the complexity of accessing and managing webcam feeds, allowing you to focus on building computer vision applications. Whether you're prototyping a computer vision project, experimenting with real-time image processing, or just need a straightforward webcam interface, WebCam offers an easy-to-use solution.

Show Your Support

If you find this template useful, please consider giving it a ⭐ on GitHub! It helps others discover this project and lets me know you’re interested.

Star this repository

Features

WebCam Class: Easily integrate and control webcam feeds from various sources.

Simple Webcam Control: Easily start and stop webcam feeds.

Real-time Display: Stream webcam frames in real time within Streamlit apps.

Custom Frame Processing: Apply custom image processing (e.g., filters, effects) to webcam frames before displaying them.

Multi-Webcam Support: Manage multiple webcams by specifying different device indexes.

Session State Management: Leveraging Streamlit’s session state to handle webcam states and resources efficiently.

Lightweight & Beginner-Friendly: Easy-to-understand class-based structure designed for prototyping and learning.

Install st-webcam?

Run the following command to install dependencies.

pip install st-webcam

Quick Start

Import necessary libraries.

import streamlit as st
from st_webcam import WebCam
# import other required libraries for your project

Run the following command to start your streamlit app.

streamlit run app.py

Where app.py is your Python script that contains the code to display the webcam feed.

Methods

init(self, index=0, label=None)

Purpose

  1. Initializes the WebCam object with default or provided webcam index and label.
  2. Initializes the session state for controlling the webcam feed.
  3. Provides Start/Stop buttons for the webcam feed in the Streamlit interface

Arguements

  • index (int, optional): The index of the webcam device (default is 0).
  • label (str, optional): The label for the webcam that will be displayed in the control button (default is "Webcam #index", where 'index' is the webcam device index).

Example

webcam = WebCam(index=1, label="Custom")

start(self, index=None)

Purpose

Starts the webcam feed and initializes the VideoCapture object.

Arguements

  • index (int, optional): The index of the webcam device (default is self.index).

Example

webcam.start(index=1)

stop(self)

Purpose

This method releases the webcam resources, clears session state variables, and resets the webcam to a stopped state. If the webcam is not running, it does nothing.

Example

- webcam.stop()

display_frame(self, frame, frame_func=None, frame_placeholder=None)

Purpose

Displays the provided frame in the Streamlit interface. Can apply a function before displaying.

Arguements

  • frame (ndarray): The frame to be displayed.
  • frame_func (function, optional): A function to apply additional processing to the frame.
  • frame_placeholder (Streamlit placeholder, optional): A placeholder for displaying the frame. Defaults to the instance's placeholder.

Example

- webcam.display_frame(frame, frame_func=apply_filter, frame_placeholder=placeholder1)

For More info on Private Methods, Session States and Identifiers, review the code.

Usage Examples

Default Usage

webcam = WebCam() # webcam object

frames = webcam.start() # before use

if frames: 
    for frame in frames:
        webcam.display_frame(frame)

webcam.stop() # after use

Use Grayscale

import cv2

def convert_grayscale(frame):
    return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

webcam = WebCam(index=0) # for webcam at index 0

frames = webcam.start()

if frames:
    for frame in frames:
        webcam.display_frame(frame, frame_func=convert_grayscale)

webcam.stop()

Multiple Displays with different effects

def apply_canny_edge_detection(frame):
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    edges = cv2.Canny(gray_frame, 100, 200)
    edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
    return edges_rgb

def apply_cartoon_effect(frame):
    bilateral_filtered_frame = cv2.bilateralFilter(frame, d=9, sigmaColor=75, sigmaSpace=75)
    gray_frame = cv2.cvtColor(bilateral_filtered_frame, cv2.COLOR_RGB2GRAY)
    blurred_gray = cv2.medianBlur(gray_frame, 7)
    cartoon_edges = cv2.adaptiveThreshold(blurred_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, 
                                          cv2.THRESH_BINARY, blockSize=9, C=9)
    cartoon_frame = cv2.bitwise_and(bilateral_filtered_frame, bilateral_filtered_frame, mask=cartoon_edges)    
    return cartoon_frame

def apply_sobel_edge_detection(frame):
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    sobel_x = cv2.Sobel(gray_frame, cv2.CV_64F, 1, 0, ksize=3)
    sobel_y = cv2.Sobel(gray_frame, cv2.CV_64F, 0, 1, ksize=3)
    sobel_edges = cv2.magnitude(sobel_x, sobel_y)
    sobel_edges = cv2.convertScaleAbs(sobel_edges)
    sobel_edges_rgb = cv2.cvtColor(sobel_edges, cv2.COLOR_GRAY2RGB)
    return sobel_edges_rgb
webcam = WebCam(index=0, label="Cartoon")

frames = webcam.start()

placeholder1 = st.empty()
placeholder2 = st.empty()

if frames:
    for frame in frames:
        webcam.display_frame(frame, apply_canny_edge_detection)
        webcam.display_frame(frame, apply_cartoon_effect, placeholder1)
        webcam.display_frame(frame, apply_sobel_edge_detection, placeholder2)
        
webcam.stop()

Development

Feel free to fork the project, contribute, or create an issue for any bugs or new features you'd like to see. If you're interested in collaborating, please follow the standard GitHub contribution workflow: fork, clone, create a branch, and submit a pull request.

License

st-webcam is licensed under the MIT License. See the License file for more details.

Metadata

Release files for st-webcam 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for st-webcam 0.1.1
File Size Uploaded Provenance Actions
st_webcam-0.1.1.tar.gz 6.8 kB Without provenance Details for st_webcam-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for st-webcam 0.1.1
File Interpreter ABI Platform Actions
st_webcam-0.1.1-py3-none-any.whl Python 3 none any Details for st_webcam-0.1.1-py3-none-any.whl

Total release size: 13.9 kB

Release files / st_webcam-0.1.1.tar.gz

Download URL st_webcam-0.1.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2412b08c67ad48fe1e848c1dd89197629739d05f84b2a4fbbd58bd239cfe8e4b
BLAKE2b-256 checksum
How to use checksums
079845deb8c7f43284d5c13b4c64739792758a8c10bb579113e023273a02ae47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / st_webcam-0.1.1-py3-none-any.whl

Download URL st_webcam-0.1.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
143abc84b74cbd5a94c6057d7b37199daa629a45b13f15d3062d5120e6da6726
BLAKE2b-256 checksum
How to use checksums
fdedf5813ed140163fa9ec95b6b5e0178dec39b43531b50c8b93e908b810c8f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Security for st-webcam 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page