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.

What is st_webcam?

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.

Features

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

Streamlit Integration: Seamlessly display webcam feeds in Streamlit apps.

Image Processing: Provides a simple interface for processing frames captured from the webcam.

Multi-WebCam Support: Support for multiple webcams with different indexes.

Lightweight and Easy to Use: Simple class-based structure, perfect for beginners and prototyping.

How to install st_webcam?

run the following command

pip install st-webcam

Setup your project in Streamlit

Import the libraries by using

import streamlit as st
from st_webcam import WebCam

After writing your code, run the following command to see the streamlit app

streamlit run main.py

where main.py is your python file

Usage Examples

Default Usage

# Create a WebCam object
webcam = WebCam()

# If the webcam is started, capture and display frames
frames = webcam.start()

# Ensure frames is not None before trying to iterate
if frames: 
    for frame in frames:
        webcam.display_frame(frame) # Display frames for webcam

# Stop the webcam when done
webcam.stop()

Use Grayscale

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

# Initialize WebCam instance for webcam 0
webcam = WebCam(index=0)

# Start capturing frames
frames = webcam.start()

# Display frames with grayscale conversion
if frames:
    for frame in frames:
        webcam.display_frame(frame, frame_func=convert_grayscale)

# Stop the webcam feed once done
webcam.stop()

Multiple Displays with different effects

def apply_canny_edge_detection(frame):
    # Convert the frame to grayscale (Canny edge detection works on grayscale images)
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    
    # Apply Canny edge detection
    edges = cv2.Canny(gray_frame, 100, 200)
    
    # Convert the edges to RGB for display in Streamlit
    edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
    
    return edges_rgb

def apply_cartoon_effect(frame):
    # Apply bilateral filter to smooth the image while preserving edges
    bilateral_filtered_frame = cv2.bilateralFilter(frame, d=9, sigmaColor=75, sigmaSpace=75)
    
    # Convert to grayscale for edge detection
    gray_frame = cv2.cvtColor(bilateral_filtered_frame, cv2.COLOR_RGB2GRAY)
    
    # Apply median blur to the grayscale frame to reduce noise
    blurred_gray = cv2.medianBlur(gray_frame, 7)
    
    # Apply adaptive thresholding to detect edges
    cartoon_edges = cv2.adaptiveThreshold(blurred_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, 
                                          cv2.THRESH_BINARY, blockSize=9, C=9)
    
    # Convert the original frame to RGB (for color effect)
    cartoon_frame = cv2.bitwise_and(bilateral_filtered_frame, bilateral_filtered_frame, mask=cartoon_edges)
    
    return cartoon_frame

def apply_sobel_edge_detection(frame):
    # Convert the frame to grayscale (Sobel works on grayscale images)
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    
    # Apply Sobel operator in the X direction (horizontal edges)
    sobel_x = cv2.Sobel(gray_frame, cv2.CV_64F, 1, 0, ksize=3)
    
    # Apply Sobel operator in the Y direction (vertical edges)
    sobel_y = cv2.Sobel(gray_frame, cv2.CV_64F, 0, 1, ksize=3)
    
    # Combine both X and Y gradients to get the final edge image
    sobel_edges = cv2.magnitude(sobel_x, sobel_y)
    
    # Convert to uint8 and back to RGB for display
    sobel_edges = cv2.convertScaleAbs(sobel_edges)
    sobel_edges_rgb = cv2.cvtColor(sobel_edges, cv2.COLOR_GRAY2RGB)
    
    return sobel_edges_rgb
# Initialize WebCam instance for webcam 0
webcam = WebCam(index=0, label="Cartoon")

# Start capturing frames if webcam is running
frames = webcam.start()

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

# Display frames in Streamlit if available
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)
        
# Stop the webcam feed once done
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.0.6.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

st_webcam-0.0.6-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: st_webcam-0.0.6.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for st_webcam-0.0.6.tar.gz
Algorithm Hash digest
SHA256 6112ab472c14f58e338f4af3f53e1fd8f5febce728a23a3bd468bfff54755ec8
MD5 a83f2b3ad83562464e996fe066eb2f49
BLAKE2b-256 f95cd5c057927c4829aad3f81909c079d624e1a7a971f60d61726dc60ca67e45

See more details on using hashes here.

File details

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

File metadata

  • Download URL: st_webcam-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for st_webcam-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 69a9ae2e3980ba4602b143a8c9b100b2c061eb9f39e6490bc50b47a57d672a23
MD5 8bcd2db3423b235c71adb8d011af6dd5
BLAKE2b-256 4aa5a37666652c6811818011af00ddb5a189eae216d28b8051de1fbd10a9a896

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