# OpenCV-Webcam **Repository Path**: CV_Lab/opencv_webcam ## Basic Information - **Project Name**: OpenCV-Webcam - **Description**: ๐Ÿš€ ๅŸบไบŽ OpenCV ็š„ Webcam ่„šๆœฌ็จ‹ๅบ๏ผŒ้€‚็”จไบŽ่ฎก็ฎ—ๆœบ่ง†่ง‰ๆ•ฐๆฎ้‡‡้›†๏ผŒๅฎžๆ—ถ่ง†้ข‘ๅธงๆ”ถ้›†็ญ‰ใ€‚ๅ…ทๆœ‰ๅฎ‰่ฃ…ไพฟๆทใ€ๆ“ไฝœ็ฎ€ๅ•ใ€่ทจๅนณๅฐ็ญ‰็‰น็‚นใ€‚ - **Primary Language**: Python - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: https://gitee.com/PyCVer - **GVP Project**: No ## Statistics - **Stars**: 223 - **Forks**: 80 - **Created**: 2021-12-29 - **Last Updated**: 2025-08-30 ## Categories & Tags **Categories**: cv **Tags**: OpenCV, Webcam, ่ฎก็ฎ—ๆœบ่ง†่ง‰, ๆ•ฐๆฎ้‡‡้›†, YOLOv5 ## README [ไธญๆ–‡็ฎ€ไฝ“](./README.md)|English
Simple Icons

Webcam program based on OpenCV, suitable for computer vision data acquisition, real-time video frame collection, etc.

It has the characteristics of convenient installation, simple operation and cross-platform.

code check Releases Version PyPI Docker Podman Jupyter License

pre-commit Python Version PyPI Package Health security bandit

star fork Docker Pulls PyPI All Downloads

๐Ÿš€Update Trend

- `2022-07-26` **โšก [opencv-webcam-script v0.9 release version](https://gitee.com/CV_Lab/opencv_webcam/releases/v0.9)officially launched** - `2022-07-26` **โšก [opencv-webcam-script v0.9 Podman version](./tutorial/ows_podman.md)officially launched** - `2022-07-26` **โšก [opencv-webcam-script v0.9 docker version](https://hub.docker.com/r/zengdockerdocker/opencv-webcam-script)officially launched** - `2022-07-26` **โšก [opencv-webcam-script v0.9.1 PyPI version](https://pypi.org/project/opencv-webcam-script/0.9.1/)officially launched** - `2022-07-26` **โšก [opencv-webcam-script v0.9 Jupyter version](./tutorial/ows_jupyter.ipynb)officially launched** - โšก **[opencv-webcam-script historical version](./v_change)**

๐Ÿ’ŽProject Purpose

### ๐Ÿ”ฅ Quickly create object detection datasets #### ๐Ÿ“Œ Project 1: [Gesture recognition demo based on YOLOv5](yolov5_GestureData.md)

๐Ÿ’กProject Structure

``` . โ”œโ”€โ”€ opencv_webcam # project name โ”‚ โ”œโ”€โ”€ utils # Toolkit โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py # Toolkit initialization โ”‚ โ”‚ โ”œโ”€โ”€ args_yaml.py # Instruction management โ”‚ โ”‚ โ”œโ”€โ”€ compress.py # Compression management โ”‚ โ”‚ โ”œโ”€โ”€ frame_opt.py # Frame save management โ”‚ โ”‚ โ”œโ”€โ”€ check_opt.py # Check management โ”‚ โ”‚ โ”œโ”€โ”€ fonts_opt.py # Font management โ”‚ โ”‚ โ”œโ”€โ”€ plot.py # Drawing management โ”‚ โ”‚ โ”œโ”€โ”€ rm_bgColor.py # Picture background management โ”‚ โ”‚ โ”œโ”€โ”€ log.py # log management โ”‚ โ”‚ โ”œโ”€โ”€ path_opt.py # Save path management โ”‚ โ”‚ โ”œโ”€โ”€ sys_opt.py # system management โ”‚ โ”‚ โ””โ”€โ”€ time_format.py # time format โ”‚ โ”œโ”€โ”€ tutorial # ows tutorial โ”‚ โ”‚ โ”œโ”€โ”€ ows_bash.md # ows Bash version tutorial โ”‚ โ”‚ โ”œโ”€โ”€ ows_pypi.md # ows PyPI version tutorial โ”‚ โ”‚ โ”œโ”€โ”€ ows_docker.md # ows docker version tutorial โ”‚ โ”‚ โ”œโ”€โ”€ ows_podman.md # ows Podman version tutorial โ”‚ โ”‚ โ””โ”€โ”€ ows_jupyter.ipynb # ows Jupyter version tutorial โ”‚ โ”œโ”€โ”€ v_change # Version history โ”‚ โ”‚ โ”œโ”€โ”€ v01_05_change.md # v0.1-v0.5 version change โ”‚ โ”‚ โ”œโ”€โ”€ v06_change.md # v0.6 version change โ”‚ โ”‚ โ”œโ”€โ”€ v07_change.md # v0.7 version change โ”‚ โ”‚ โ”œโ”€โ”€ v08_change.md # v0.8 version change โ”‚ โ”‚ โ””โ”€โ”€ README.md # Version history description โ”‚ โ”œโ”€โ”€ __init__.py # Project initialization โ”‚ โ”œโ”€โ”€ opencv_webcam.py # Script main run file โ”‚ โ”œโ”€โ”€ LICENSE # Project License โ”‚ โ”œโ”€โ”€ CodeCheck.md # Code Check โ”‚ โ”œโ”€โ”€ CodeSecurity.md # Code Security Check โ”‚ โ”œโ”€โ”€ setup.sh # Environment installation script โ”‚ โ”œโ”€โ”€ fonts.sh # Font download script โ”‚ โ”œโ”€โ”€ Dockerfile # docker build file โ”‚ โ”œโ”€โ”€ .gitignore # git ignore files โ”‚ โ”œโ”€โ”€ .dockerignore # docker ignore file โ”‚ โ”œโ”€โ”€ GestureData_yolov5.md # Gesture&YOLOv5 demo โ”‚ โ”œโ”€โ”€ README.md # Project description โ”‚ โ”œโ”€โ”€ README.en.md # Project description (English version) โ”‚ โ””โ”€โ”€ requirements.txt # Script dependencies ```

๐Ÿ”ฅInstallation Tutorial

โค๏ธ OWS provides **5** installation methods. Click on the corresponding logo below for detailed installation and usage tutorials.
### โœ… Method 1: Linux Shell Installation (Development Edition) #### ๐Ÿ”ฅ [opencv-webcam-script Bash version detailed tutorial](./tutorial/ows_bash.md) ๐Ÿ“Œ Step 1: Clone the project ```shell git clone https://gitee.com/CV_Lab/opencv_webcam.git # clone project ``` ๐Ÿ“Œ Step 2: Create a virtual environment ```shell # Create a conda virtual environment, taking python 3.8 as an example conda create -n ows python==3.8 # The virtual environment name is ows conda activate ows # Activate the virtual environment ``` ๐Ÿ“Œ Step 3: Install the script ```shell pip install --upgrade pip # upgrade pip pip install -r ./requirements.txt -U # Install OpenCV Webcam Script ``` ๐Ÿ“Œ Steps 2 and 3 can also be performed via [setup.sh](./setup.sh) ```shell bash ./setup.sh ``` โ—Note: **OWS v0.6** and above need to run in the environment of [**Python>=3.8.0**](https://www.python.org/) ### โœ… Method 2: pip quick installation (PyPI version) #### ๐Ÿ”ฅ [opencv-webcam-script PyPI version detailed tutorial](./tutorial/ows_pypi.md) #### ๐ŸŽจ Easy Tutorial ๐Ÿ“Œ Step 1: Create an ows virtual environment, see Method 1 ๐Ÿ“Œ Step 2: Execute the pip command ```shell pip install opencv-webcam-script==0.9.0 ``` ๐Ÿ“Œ Step 3: Write a python program ```python from opencv_webcam.opencv_webcam import webcam_opencv # ------------Example of a few functions------------ webcam_opencv() # regular call webcam_opencv(is_autoSaveFrame=True) # Auto save frames webcam_opencv(is_autoSaveFrame=True, is_compress=True) # compressed frame ``` โ— Note: Be sure to add **version number** after `==`. If you execute `pip install opencv-webcam-script`, it may be installed as a beta version, which will affect the use. ### โœ… Method 3: docker image installation (docker version) #### ๐Ÿ”ฅ [opencv-webcam-script docker version detailed tutorial](./tutorial/ows_docker.md) #### ๐ŸŽจ Easy Tutorial ๐Ÿ“Œ Step 1: Download the mirror ```shell sudo docker pull zengdockerdocker/opencv-webcam-script:v0.9 # Mirror pull ``` ๐Ÿ“Œ Step 2: Create the container ```shell xhost +local:root # Allow root user access to running X server (important) # Create a container named ows sudo docker run --name=ows --ipc=host -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY --device=/dev/video0:/dev/video0 zengdockerdocker/opencv-webcam-script:v0.9 ``` ๐Ÿ“Œ Step 3: Run the program ```shell python opencv_webcam.py # run script python opencv_webcam.py -isasf # Auto save frames # Get docker frame data to this machine sudo docker cp Container name: container directory local host directory sudo docker cp ows:/usr/src/app/WebcamFrame /home/username # Example ``` โ— Note: Before starting the ows container, you need to execute the `xhost +local:root` command to ensure that it is connected to the local device. ### โœ… Method 4: Podman installation (Podman version) #### ๐Ÿ”ฅ [opencv-webcam-script Podman version detailed tutorial](./tutorial/ows_podman.md) #### ๐ŸŽจ Easy Tutorial ๐Ÿ“Œ Step 1: Download the mirror ```shell sudo podman pull docker.io/zengdockerdocker/opencv-webcam-script:v0.9 # Mirror pull ``` ๐Ÿ“Œ Step 2: Create the container ```shell xhost +local:root # Allow root user access to running X server (important) # Create a container named ows sudo podman run --name=ows --ipc=host -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY --device=/dev/video0:/dev/video0 docker.io/zengdockerdocker/opencv-webcam-script:v0.9 ``` ๐Ÿ“Œ Step 3: Run the program ```shell python opencv_webcam.py # run script python opencv_webcam.py -isasf # Auto save frames # Get Podman frame data to this machine sudo podman cp Container name: container directory local host directory sudo podman cp ows:/usr/src/app/WebcamFrame /home/username # Example ``` โ— Note: Before starting the ows container, you need to execute the `xhost +local:root` command to ensure that it is connected to the local device. ### โœ… Method 5: Jupyter installation (Jupyter version) #### ๐Ÿ”ฅ [opencv-webcam-script Jupyter version detailed tutorial](./tutorial/ows_jupyter.ipynb) #### ๐ŸŽจ Easy Tutorial ๐Ÿ“Œ Step 1: Installation ```shell # First create a virtual environment manually # conda create -n ows python==3.8 # The virtual environment name is ows # conda activate ows # Activate the virtual environment !git clone https://gitee.com/CV_Lab/opencv_webcam.git # clone %cd opencv_webcam %pip install -qr requirements.txt -U # install ``` ๐Ÿ“Œ Step 2: Run ```shell !python opencv_webcam.py # Press the q key to exit by default !python opencv_webcam.py -isasf # Automatically save video frames ``` **Note: [Jupyter version of ows ](https://gitee.com/CV_Lab/opencv_webcam/blob/master/tutorial/ows_jupyter.ipynb)program can run unit instructions through `Ctrl+Enter`**

โšกUsage

โค๏ธ **OWS v0.9** Bash Shell display information highly integrated [rich](https://github.com/Textualize/rich) component
OWS v0.9 Bash Shell Display Information
#### ๐Ÿ’ก regular start ```shell # start the ows program python opencv_webcam.py ``` โ— Tip: After starting the ows program, first enter the **countdown mode**, there is a **150 frame** countdown preparation time, and the user will be prompted to adjust the device to a suitable position, ready to start.
#### ๐Ÿ’ก Device Selection ```shell # Multi-camera switching example, the default is 0 python opencv_webcam.py -dev 0 python opencv_webcam.py -dev 1 python opencv_webcam.py -dev 2 # RTSP python opencv_webcam.py -dev "rtsp://username:password@xxx.xxx.xxx" ``` #### ๐Ÿ’ก set escape key ```shell # By default press the q key to exit python opencv_webcam.py -q z # set z key to exit python opencv_webcam.py -q k # set k key to exit ```
#### ๐Ÿ’ก Auto save frames ```shell python opencv_webcam.py -isasf ``` โ— Tip: The system calculates the maximum number of pre-saved pictures according to the size of the pictures saved by the camera and the capacity of the local disk
#### ๐Ÿ’ก Save frames every n frames ```shell # save a frame every 10 frames python opencv_webcam.py -isasf -fns 10 ``` #### ๐Ÿ’ก Save frames manually ```shell # By default press a key to capture a frame python opencv_webcam.py -ishsf ``` #### ๐Ÿ’ก custom capture key ```shell # Set the z key as the capture key, the default is the a key python opencv_webcam.py -ishsf -fck z ``` #### ๐Ÿ’ก Reshape frame size (custom width and height) ```shell # reshape width 300 height 200 python opencv_webcam.py -isasf -isrf -rf 300,200 # Automatic version python opencv_webcam.py -ishsf -isrf -rf 300,200 # Manual version ``` #### ๐Ÿ’ก Reshape frame size (custom aspect scaling) ```shell # The aspect ratio is 0.5 python opencv_webcam.py -isasf -isrf -rrf 0.5 # Automatic version python opencv_webcam.py -ishsf -isrf -rrf 0.5 # Manual version ``` #### ๐Ÿ’ก Custom save path ```shell # Set the save path, the default save path is WebcamFrame python opencv_webcam.py -fsd custom_dir -isasf # Take the automatic version as an example ``` #### ๐Ÿ’ก custom frame directory name ```shell # Set the frame directory name, the default save path is frames python opencv_webcam.py -fdn frames_custom -isasf # Take the automatic version as an example ``` #### ๐Ÿ’ก Custom frame name prefix ```shell # Set the prefix name of the frame image python opencv_webcam.py -isasf -fnp webcam ``` #### ๐Ÿ’ก Set the frame save format ```shell # Set JPG quality to 100, default to 95 python opencv_webcam.py -isasf -fss jpg -jq 100 # Set PNG quality to 5, default is 3 python opencv_webcam.py -isasf -fss png -jq 5 ``` #### ๐Ÿ’ก set pause button ```shell # Set the w key as the pause key, the default is the p key python opencv_webcam.py -p w # Press any key to continue ``` #### ๐Ÿ’ก Set the number of frames to save ```shell # Set to save 100 frames python opencv_webcam.py -isasf -afn 100 ``` #### ๐Ÿ’ก Resize form ```shell # Manually resize form (v0.9) python opencv_webcam.py -isrw ```
#### ๐Ÿ’ก log settings ```shell # The log file is saved in the project root directory by default, ows.log python opencv_webcam.py # Set the log file name and type, including .log, .txt, .data python opencv_webcam.py -ln ows02.txt # Set the log saving method, the default is append mode python opencv_webcam.py -lm w # set to overlay mode ```
#### ๐Ÿ’ก video frame compression ```shell # Regular compression, default ows.zip (take the automatic version as an example) python opencv_webcam.py -isasf -isc # Customize the compressed file name python opencv_webcam.py -isasf -isc -cn ows02 # Automatically name archives python opencv_webcam.py -isasf -isc -isacn # Custom compression mode, default is write overwrite python opencv_webcam.py -isasf -isc -cs tar -cm w:gz # tar compression python opencv_webcam.py -isasf -isc -cm a # Append mode (a mode is only for zip) Note: This command is only available in v0.4 ```
#### ๐Ÿ’ก Remove background color (cut green, cut blue) ```shell # Remove background color, default is green python opencv_webcam.py -isasf -isrbgc # Choose a background color mode python opencv_webcam.py -isasf -isrbgc -rbgcm green # cut green python opencv_webcam.py -isasf -isrbgc -rbgcm blue # cut blue ``` โ— Note: In order to achieve the **best effect** of removing the background color, try not to mix the background color with white light, and keep the background color as high as possible.
#### ๐Ÿ’ก Date-Frame Number Chart Every time this function runs the script, the program will automatically generate a `date-frames graph` based on the `date_time_frames.csv` file. โค๏ธ This function automatically generates a **date-frame number graph** according to the `date_time_frames.csv` file every time the OWS program is run. ##### โœจ Operation Example ๐Ÿ“Œ run command ```shell python opencv_webcam.py -isasf -fdn tiger python opencv_webcam.py -isasf -fdn lion python opencv_webcam.py -isasf -fdn elephant ``` ๐Ÿ“Œ Date-Frame Number Chart Directory Structure ```shell . โ”œโ”€โ”€ opencv_webcam # project name โ”‚ โ”œโ”€โ”€ DateFrames # Date-frame number chart directory โ”‚ โ”‚ โ”œโ”€โ”€ 2022-02-21 # Date list โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ lion.png # lion category date-frame number chart โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ elephant.png # elephant category date-frame number chart โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ tiger.png # tiger category date-frame number chart โ”‚ โ”œโ”€โ”€ date_time_frames.csv # Date-frame number graph log โ”‚ โ”œโ”€โ”€ .... # Other files ```
๐Ÿ“Œ date_time_frames.csvๆ—ฅๅฟ—ๆ–‡ไปถ ```shell 2022-02-21 07:48:41,58,lion,WebcamFrame 2022-02-21 07:49:52,68,lion,WebcamFrame 2022-02-21 07:50:59,37,tiger,WebcamFrame 2022-02-21 07:51:14,59,elephant,WebcamFrame 2022-02-21 07:51:30,72,tiger,WebcamFrame 2022-02-21 07:51:42,116,lion,WebcamFrame 2022-02-21 07:51:54,127,elephant,WebcamFrame 2022-02-21 07:52:16,122,elephant,WebcamFrame 2022-02-21 07:52:27,106,lion,WebcamFrame 2022-02-21 07:52:38,127,tiger,WebcamFrame 2022-02-21 07:53:00,78,elephant,WebcamFrame 2022-02-21 07:53:10,107,tiger,WebcamFrame 2022-02-21 07:53:21,95,lion,WebcamFrame 2022-02-21 07:53:42,59,lion,WebcamFrame 2022-02-21 07:53:50,74,tiger,WebcamFrame 2022-02-21 07:53:59,97,elephant,WebcamFrame 2022-02-21 07:54:58,125,elephant,WebcamFrame 2022-02-21 07:55:11,79,lion,WebcamFrame 2022-02-21 07:55:20,110,tiger,WebcamFrame 2022-02-21 07:55:29,146,tiger,WebcamFrame 2022-02-21 07:55:55,50,tiger,WebcamFrame 2022-02-21 07:56:02,153,lion,WebcamFrame 2022-02-21 07:56:16,108,elephant,WebcamFrame 2022-02-21 07:56:38,121,tiger,WebcamFrame ```

๐Ÿ“Œ lion.png
๐Ÿ“Œ elephant.png
๐Ÿ“Œ tiger.png

#### ๐Ÿ’ก Instruction query ```shell # query script parameters python opencv_webcam.py --help ```
๐Ÿ“Œ Command query result ```shell usage: opencv_webcam.py [-h] [--device DEVICE] [--quit QUIT] [--is_autoSaveFrame] [--is_handSaveFrame] [--is_resizeFrame] [--frame_saveDir FRAME_SAVEDIR] [--frame_dirName FRAME_DIRNAME] [--frame_nSave FRAME_NSAVE] [--frame_capKey FRAME_CAPKEY] [--resize_frame RESIZE_FRAME] [--resizeRatio_frame RESIZERATIO_FRAME] [--frame_namePrefix FRAME_NAMEPREFIX] [--frame_saveStyle FRAME_SAVESTYLE] [--jpg_quality JPG_QUALITY] [--png_quality PNG_QUALITY] [--pause PAUSE] [--auto_frameNum AUTO_FRAMENUM] [--logName LOGNAME] [--logMode LOGMODE] [--is_compress] [--compressStyle COMPRESSSTYLE] [--is_autoCompressName] [--compressName COMPRESSNAME] [--compressMode COMPRESSMODE] [--is_rmbgColor] [--rmbgColorMode RMBGCOLORMODE] [--is_resizeWindow] OpenCV Webcam Script v0.9 optional arguments: -h, --help show this help message and exit --device DEVICE, -dev DEVICE device index for webcam, 0 or rtsp --quit QUIT, -q QUIT quit key for webcam --is_autoSaveFrame, -isasf is auto save frame --is_handSaveFrame, -ishsf is hand save frame --is_resizeFrame, -isrf is resize frame --frame_saveDir FRAME_SAVEDIR, -fsd FRAME_SAVEDIR save frame dir --frame_dirName FRAME_DIRNAME, -fdn FRAME_DIRNAME save frame dir name --frame_nSave FRAME_NSAVE, -fns FRAME_NSAVE n frames save a frame (auto save frame) --frame_capKey FRAME_CAPKEY, -fck FRAME_CAPKEY frame capture key (hand save frame) --resize_frame RESIZE_FRAME, -rf RESIZE_FRAME resize frame save --resizeRatio_frame RESIZERATIO_FRAME, -rrf RESIZERATIO_FRAME resize ratio frame save --frame_namePrefix FRAME_NAMEPREFIX, -fnp FRAME_NAMEPREFIX frame name prefix --frame_saveStyle FRAME_SAVESTYLE, -fss FRAME_SAVESTYLE frame save style --jpg_quality JPG_QUALITY, -jq JPG_QUALITY frame save jpg quality (0-100) default 95 --png_quality PNG_QUALITY, -pq PNG_QUALITY frame save jpg quality (0-9) default 3 --pause PAUSE, -p PAUSE webcam pause --auto_frameNum AUTO_FRAMENUM, -afn AUTO_FRAMENUM auto save number of frames --logName LOGNAME, -ln LOGNAME log save name --logMode LOGMODE, -lm LOGMODE log write mode --is_compress, -isc is compress file --compressStyle COMPRESSSTYLE, -cs COMPRESSSTYLE compress style --is_autoCompressName, -isacn is auto compress name --compressName COMPRESSNAME, -cn COMPRESSNAME compress save name --compressMode COMPRESSMODE, -cm COMPRESSMODE compress save mode, tar w:gz --is_rmbgColor, -isrbgc is remove background color --rmbgColorMode RMBGCOLORMODE, -rbgcm RMBGCOLORMODE remove background color mode --is_resizeWindow, -isrw is resize window ```