st-webcam


Namest-webcam JSON
Version 0.0.6 PyPI version JSON
download
home_pagehttps://github.com/SaarthRajan/st_webcam
SummaryEffortless webcam integration for computer vision projects with Streamlit.
upload_time2024-11-19 04:24:39
maintainerNone
docs_urlNone
authorSaarthRajan
requires_pythonNone
licenseMIT
keywords computer vision streamlit python webcam artificial intelligence
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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 

```python
pip install st-webcam
```

## Setup your project in Streamlit
Import the libraries by using

```python
import streamlit as st
from st_webcam import WebCam
```
After writing your code, run the following command to see the streamlit app

```python
streamlit run main.py
```

where main.py is your python file

## Usage Examples

### Default Usage
```python
# 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
```python
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
```python
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
```
```python
# 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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SaarthRajan/st_webcam",
    "name": "st-webcam",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "Computer Vision, Streamlit, Python, Webcam, Artificial Intelligence",
    "author": "SaarthRajan",
    "author_email": "saarth.rajan@uwaterloo.ca",
    "download_url": "https://files.pythonhosted.org/packages/f9/5c/d5c057927c4829aad3f81909c079d624e1a7a971f60d61726dc60ca67e45/st_webcam-0.0.6.tar.gz",
    "platform": null,
    "description": "# st_webcam - Effortless webcam integration for computer vision projects with Streamlit.\n\n## What is st_webcam?\nst_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.\n\n## Features\n\nWebCam Class: Easily integrate and control webcam feeds from various sources.\n\nStreamlit Integration: Seamlessly display webcam feeds in Streamlit apps.\n\nImage Processing: Provides a simple interface for processing frames captured from the webcam.\n\nMulti-WebCam Support: Support for multiple webcams with different indexes.\n\nLightweight and Easy to Use: Simple class-based structure, perfect for beginners and prototyping.\n\n## How to install st_webcam?\nrun the following command \n\n```python\npip install st-webcam\n```\n\n## Setup your project in Streamlit\nImport the libraries by using\n\n```python\nimport streamlit as st\nfrom st_webcam import WebCam\n```\nAfter writing your code, run the following command to see the streamlit app\n\n```python\nstreamlit run main.py\n```\n\nwhere main.py is your python file\n\n## Usage Examples\n\n### Default Usage\n```python\n# Create a WebCam object\nwebcam = WebCam()\n\n# If the webcam is started, capture and display frames\nframes = webcam.start()\n\n# Ensure frames is not None before trying to iterate\nif frames: \n    for frame in frames:\n        webcam.display_frame(frame) # Display frames for webcam\n\n# Stop the webcam when done\nwebcam.stop()\n```\n\n### Use Grayscale\n```python\ndef convert_grayscale(frame):\n    return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)\n\n# Initialize WebCam instance for webcam 0\nwebcam = WebCam(index=0)\n\n# Start capturing frames\nframes = webcam.start()\n\n# Display frames with grayscale conversion\nif frames:\n    for frame in frames:\n        webcam.display_frame(frame, frame_func=convert_grayscale)\n\n# Stop the webcam feed once done\nwebcam.stop()\n```\n\n### Multiple Displays with different effects\n```python\ndef apply_canny_edge_detection(frame):\n    # Convert the frame to grayscale (Canny edge detection works on grayscale images)\n    gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)\n    \n    # Apply Canny edge detection\n    edges = cv2.Canny(gray_frame, 100, 200)\n    \n    # Convert the edges to RGB for display in Streamlit\n    edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)\n    \n    return edges_rgb\n\ndef apply_cartoon_effect(frame):\n    # Apply bilateral filter to smooth the image while preserving edges\n    bilateral_filtered_frame = cv2.bilateralFilter(frame, d=9, sigmaColor=75, sigmaSpace=75)\n    \n    # Convert to grayscale for edge detection\n    gray_frame = cv2.cvtColor(bilateral_filtered_frame, cv2.COLOR_RGB2GRAY)\n    \n    # Apply median blur to the grayscale frame to reduce noise\n    blurred_gray = cv2.medianBlur(gray_frame, 7)\n    \n    # Apply adaptive thresholding to detect edges\n    cartoon_edges = cv2.adaptiveThreshold(blurred_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \n                                          cv2.THRESH_BINARY, blockSize=9, C=9)\n    \n    # Convert the original frame to RGB (for color effect)\n    cartoon_frame = cv2.bitwise_and(bilateral_filtered_frame, bilateral_filtered_frame, mask=cartoon_edges)\n    \n    return cartoon_frame\n\ndef apply_sobel_edge_detection(frame):\n    # Convert the frame to grayscale (Sobel works on grayscale images)\n    gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)\n    \n    # Apply Sobel operator in the X direction (horizontal edges)\n    sobel_x = cv2.Sobel(gray_frame, cv2.CV_64F, 1, 0, ksize=3)\n    \n    # Apply Sobel operator in the Y direction (vertical edges)\n    sobel_y = cv2.Sobel(gray_frame, cv2.CV_64F, 0, 1, ksize=3)\n    \n    # Combine both X and Y gradients to get the final edge image\n    sobel_edges = cv2.magnitude(sobel_x, sobel_y)\n    \n    # Convert to uint8 and back to RGB for display\n    sobel_edges = cv2.convertScaleAbs(sobel_edges)\n    sobel_edges_rgb = cv2.cvtColor(sobel_edges, cv2.COLOR_GRAY2RGB)\n    \n    return sobel_edges_rgb\n```\n```python\n# Initialize WebCam instance for webcam 0\nwebcam = WebCam(index=0, label=\"Cartoon\")\n\n# Start capturing frames if webcam is running\nframes = webcam.start()\n\nplaceholder1 = st.empty()\nplaceholder2 = st.empty()\n\n# Display frames in Streamlit if available\nif frames:\n    for frame in frames:\n        webcam.display_frame(frame, apply_canny_edge_detection)\n        webcam.display_frame(frame, apply_cartoon_effect, placeholder1)\n        webcam.display_frame(frame, apply_sobel_edge_detection, placeholder2)\n        \n# Stop the webcam feed once done\nwebcam.stop()\n```\n\n## Development\nFeel 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.\n\n## License\nst_webcam is licensed under the MIT License. See the License file for more details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Effortless webcam integration for computer vision projects with Streamlit.",
    "version": "0.0.6",
    "project_urls": {
        "Homepage": "https://github.com/SaarthRajan/st_webcam"
    },
    "split_keywords": [
        "computer vision",
        " streamlit",
        " python",
        " webcam",
        " artificial intelligence"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4aa5a37666652c6811818011af00ddb5a189eae216d28b8051de1fbd10a9a896",
                "md5": "8bcd2db3423b235c71adb8d011af6dd5",
                "sha256": "69a9ae2e3980ba4602b143a8c9b100b2c061eb9f39e6490bc50b47a57d672a23"
            },
            "downloads": -1,
            "filename": "st_webcam-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8bcd2db3423b235c71adb8d011af6dd5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6480,
            "upload_time": "2024-11-19T04:24:38",
            "upload_time_iso_8601": "2024-11-19T04:24:38.499596Z",
            "url": "https://files.pythonhosted.org/packages/4a/a5/a37666652c6811818011af00ddb5a189eae216d28b8051de1fbd10a9a896/st_webcam-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f95cd5c057927c4829aad3f81909c079d624e1a7a971f60d61726dc60ca67e45",
                "md5": "a83f2b3ad83562464e996fe066eb2f49",
                "sha256": "6112ab472c14f58e338f4af3f53e1fd8f5febce728a23a3bd468bfff54755ec8"
            },
            "downloads": -1,
            "filename": "st_webcam-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "a83f2b3ad83562464e996fe066eb2f49",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6423,
            "upload_time": "2024-11-19T04:24:39",
            "upload_time_iso_8601": "2024-11-19T04:24:39.480805Z",
            "url": "https://files.pythonhosted.org/packages/f9/5c/d5c057927c4829aad3f81909c079d624e1a7a971f60d61726dc60ca67e45/st_webcam-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-19 04:24:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SaarthRajan",
    "github_project": "st_webcam",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "st-webcam"
}
        
Elapsed time: 0.37083s