Skip to main content

Effortless webcam integration for computer vision projects with Streamlit.

Project description

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.

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

st_webcam-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

st_webcam-0.1.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file st_webcam-0.1.1.tar.gz.

File metadata

  • Download URL: st_webcam-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for st_webcam-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2412b08c67ad48fe1e848c1dd89197629739d05f84b2a4fbbd58bd239cfe8e4b
MD5 ed40df6af68da7c14b89a32cbe66e617
BLAKE2b-256 079845deb8c7f43284d5c13b4c64739792758a8c10bb579113e023273a02ae47

See more details on using hashes here.

File details

Details for the file st_webcam-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: st_webcam-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for st_webcam-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 143abc84b74cbd5a94c6057d7b37199daa629a45b13f15d3062d5120e6da6726
MD5 ef85802910e2401d3645630cfe66ee32
BLAKE2b-256 fdedf5813ed140163fa9ec95b6b5e0178dec39b43531b50c8b93e908b810c8f0

See more details on using hashes here.

Supported by

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