# 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
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.
๐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
```