# 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](https://github.com/SaarthRajan/st_webcam)
## 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.
```python
pip install st-webcam
```
## Quick Start
Import necessary libraries.
```python
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.
```python
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**
```python
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**
```python
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**
```python
- 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**
```python
- 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
```python
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
```python
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
```python
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
```
```python
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.
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/07/98/45deb8c7f43284d5c13b4c64739792758a8c10bb579113e023273a02ae47/st_webcam-0.1.1.tar.gz",
"platform": null,
"description": "# st-webcam\n## Effortless webcam integration for computer vision projects with Streamlit\n\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\nThis 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.\n\n## Show Your Support\n\nIf you find this template useful, please consider giving it a \u2b50 on GitHub! It helps others discover this project and lets me know you\u2019re interested.\n\n[Star this repository](https://github.com/SaarthRajan/st_webcam)\n\n## Features\n\n**WebCam Class:** Easily integrate and control webcam feeds from various sources.\n\n**Simple Webcam Control:** Easily start and stop webcam feeds.\n\n**Real-time Display:** Stream webcam frames in real time within Streamlit apps.\n\n**Custom Frame Processing:** Apply custom image processing (e.g., filters, effects) to webcam frames before displaying them.\n\n**Multi-Webcam Support:** Manage multiple webcams by specifying different device indexes.\n\n**Session State Management:** Leveraging Streamlit\u2019s session state to handle webcam states and resources efficiently.\n\n**Lightweight & Beginner-Friendly:** Easy-to-understand class-based structure designed for prototyping and learning.\n\n## Install st-webcam?\n\nRun the following command to install dependencies. \n\n```python\npip install st-webcam\n```\n\n## Quick Start\n\nImport necessary libraries. \n\n```python\nimport streamlit as st\nfrom st_webcam import WebCam\n# import other required libraries for your project\n```\nRun the following command to start your streamlit app. \n\n```python\nstreamlit run app.py\n```\n\nWhere app.py is your Python script that contains the code to display the webcam feed.\n\n## Methods\n\n### __init__(self, index=0, label=None)\n\n**Purpose**\n1. Initializes the WebCam object with default or provided webcam index and label. \n2. Initializes the session state for controlling the webcam feed.\n3. Provides Start/Stop buttons for the webcam feed in the Streamlit interface\n\n**Arguements**\n- index (int, optional): The index of the webcam device (default is 0).\n- 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).\n\n**Example**\n```python\nwebcam = WebCam(index=1, label=\"Custom\")\n```\n\n### start(self, index=None)\n\n**Purpose**\n\nStarts the webcam feed and initializes the VideoCapture object.\n\n**Arguements**\n\n- index (int, optional): The index of the webcam device (default is self.index).\n\n**Example**\n```python\nwebcam.start(index=1)\n```\n\n### stop(self)\n\n**Purpose**\n\nThis 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.\n\n**Example**\n```python\n- webcam.stop()\n```\n\n### display_frame(self, frame, frame_func=None, frame_placeholder=None)\n\n**Purpose**\n\nDisplays the provided frame in the Streamlit interface. Can apply a function before displaying. \n\n**Arguements**\n - frame (ndarray): The frame to be displayed.\n - frame_func (function, optional): A function to apply additional processing to the frame.\n - frame_placeholder (Streamlit placeholder, optional): A placeholder for displaying the frame. Defaults to the instance's placeholder.\n\n**Example**\n```python\n- webcam.display_frame(frame, frame_func=apply_filter, frame_placeholder=placeholder1)\n```\n\n### For More info on Private Methods, Session States and Identifiers, review the code. \n\n## Usage Examples\n\n### Default Usage\n```python\n\nwebcam = WebCam() # webcam object\n\nframes = webcam.start() # before use\n\nif frames: \n for frame in frames:\n webcam.display_frame(frame)\n\nwebcam.stop() # after use\n```\n\n### Use Grayscale\n```python\nimport cv2\n\ndef convert_grayscale(frame):\n return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)\n\nwebcam = WebCam(index=0) # for webcam at index 0\n\nframes = webcam.start()\n\nif frames:\n for frame in frames:\n webcam.display_frame(frame, frame_func=convert_grayscale)\n\nwebcam.stop()\n```\n\n### Multiple Displays with different effects\n```python\ndef apply_canny_edge_detection(frame):\n gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)\n edges = cv2.Canny(gray_frame, 100, 200)\n edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)\n return edges_rgb\n\ndef apply_cartoon_effect(frame):\n bilateral_filtered_frame = cv2.bilateralFilter(frame, d=9, sigmaColor=75, sigmaSpace=75)\n gray_frame = cv2.cvtColor(bilateral_filtered_frame, cv2.COLOR_RGB2GRAY)\n blurred_gray = cv2.medianBlur(gray_frame, 7)\n cartoon_edges = cv2.adaptiveThreshold(blurred_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \n cv2.THRESH_BINARY, blockSize=9, C=9)\n cartoon_frame = cv2.bitwise_and(bilateral_filtered_frame, bilateral_filtered_frame, mask=cartoon_edges) \n return cartoon_frame\n\ndef apply_sobel_edge_detection(frame):\n gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)\n sobel_x = cv2.Sobel(gray_frame, cv2.CV_64F, 1, 0, ksize=3)\n sobel_y = cv2.Sobel(gray_frame, cv2.CV_64F, 0, 1, ksize=3)\n sobel_edges = cv2.magnitude(sobel_x, sobel_y)\n sobel_edges = cv2.convertScaleAbs(sobel_edges)\n sobel_edges_rgb = cv2.cvtColor(sobel_edges, cv2.COLOR_GRAY2RGB)\n return sobel_edges_rgb\n```\n\n```python\nwebcam = WebCam(index=0, label=\"Cartoon\")\n\nframes = webcam.start()\n\nplaceholder1 = st.empty()\nplaceholder2 = st.empty()\n\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 \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.1.1",
"project_urls": {
"Homepage": "https://github.com/SaarthRajan/st_webcam"
},
"split_keywords": [
"computer vision",
" streamlit",
" python",
" webcam",
" artificial intelligence"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fdedf5813ed140163fa9ec95b6b5e0178dec39b43531b50c8b93e908b810c8f0",
"md5": "ef85802910e2401d3645630cfe66ee32",
"sha256": "143abc84b74cbd5a94c6057d7b37199daa629a45b13f15d3062d5120e6da6726"
},
"downloads": -1,
"filename": "st_webcam-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ef85802910e2401d3645630cfe66ee32",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7088,
"upload_time": "2024-12-30T17:29:26",
"upload_time_iso_8601": "2024-12-30T17:29:26.722587Z",
"url": "https://files.pythonhosted.org/packages/fd/ed/f5813ed140163fa9ec95b6b5e0178dec39b43531b50c8b93e908b810c8f0/st_webcam-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "079845deb8c7f43284d5c13b4c64739792758a8c10bb579113e023273a02ae47",
"md5": "ed40df6af68da7c14b89a32cbe66e617",
"sha256": "2412b08c67ad48fe1e848c1dd89197629739d05f84b2a4fbbd58bd239cfe8e4b"
},
"downloads": -1,
"filename": "st_webcam-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "ed40df6af68da7c14b89a32cbe66e617",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6772,
"upload_time": "2024-12-30T17:29:28",
"upload_time_iso_8601": "2024-12-30T17:29:28.864820Z",
"url": "https://files.pythonhosted.org/packages/07/98/45deb8c7f43284d5c13b4c64739792758a8c10bb579113e023273a02ae47/st_webcam-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-30 17:29:28",
"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"
}