Skip to content

Commit 631e191

Browse files
committed
challenge 7: OpenCV capture camera video
1 parent d19ce9f commit 631e191

3 files changed

+127
-0
lines changed

007_OpenCV_Capture_Camera_Video.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# OpenCV Capture Camera Video
2+
Use OpenCV to capture computer's camera and show the video.
3+
4+
## Requirements
5+
6+
1. It'll open the computer's camera and show the video in a window.
7+
2. When you press "q" key it'll quit.
8+
9+
## What will we practice in this project?
10+
11+
- OpenCV: you need to install the `opencv-python` package via `pip install opencv-python` command in this project environment.
12+
- while loop
13+
- if condition
14+
- keyboard check
15+
16+
## A reference code
17+
18+
```python
19+
import cv2
20+
21+
# Open your computer's default camera device.
22+
# If your default camera ID is not 0, please use the correct ID
23+
camera = cv2.VideoCapture(0)
24+
25+
# while loop to read the camera image
26+
while True:
27+
success, img = camera.read()
28+
# if read success, it'll show the image in a "Video" window
29+
if success:
30+
cv2.imshow('Video', img)
31+
# get the key press, if you pressed "q" key it'll break the while loop
32+
k = cv2.waitKey(1)
33+
if k == ord('q'):
34+
break
35+
36+
# release your camera device and close the OpenCV windows
37+
camera.release()
38+
cv2.destroyAllWindows()
39+
40+
```
41+
42+
## Run the demo
43+
Install the OpenCV:
44+
```shell
45+
pip install -r requirements.txt
46+
```
47+
Please save the Python as opencv_capture_camera_video.py and run it in console:
48+
49+
```
50+
python opencv_capture_camera_video.py
51+
```
52+
53+
----
54+
55+
# 使用OpenCV捕捉电脑摄像头视频
56+
57+
## 项目需求
58+
59+
- 运行程序会打开电脑摄像头,并显示捕捉到的视频
60+
-`q`键会退出程序
61+
62+
## 项目练习
63+
64+
- 安装OpenCV,需要使用`pip install opencv-python`将OpenCV安装到项目环境中
65+
- while循环
66+
- if条件判断
67+
- OpenCV窗口按键
68+
69+
## 项目参考代码
70+
71+
```python
72+
import cv2
73+
74+
# 打开电脑摄像头
75+
# 默认摄像头编号为0,如果你的电脑默认摄像头编程不为0,请使用其他数字
76+
camera = cv2.VideoCapture(0)
77+
78+
# 重复去读取摄像头捕捉到的视频
79+
while True:
80+
success, img = camera.read()
81+
# 如果捕捉成功,将捕捉到的视频显示在一个"Video"窗口中
82+
if success:
83+
cv2.imshow('Video', img)
84+
# 当在Video窗口上按下k键退出程序
85+
k = cv2.waitKey(1)
86+
if k == ord('q'):
87+
break
88+
89+
# 释放摄像头并关闭OpenCV打开的窗口
90+
camera.release()
91+
cv2.destroyAllWindows()
92+
93+
```
94+
95+
## 测试运行
96+
97+
Install OpenCV:
98+
```shell
99+
pip install -r requirements.txt
100+
```
101+
保存代码为opencv_capture_camera_video.py并运行:
102+
103+
```
104+
python opencv_capture_camera_video.py
105+
```

code/7/opencv_capture_camera_video.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cv2
2+
3+
# Open your computer's default camera device.
4+
# If your default camera ID is not 0, please use the correct ID
5+
camera = cv2.VideoCapture(0)
6+
7+
# while loop to read the camera image
8+
while True:
9+
success, img = camera.read()
10+
# if read success, it'll show the image in a "Video" window
11+
if success:
12+
cv2.imshow('Video', img)
13+
# get the key press, if you pressed "q" key it'll break the while loop
14+
k = cv2.waitKey(1)
15+
if k == ord('q'):
16+
break
17+
18+
# release your camera device and close the OpenCV windows
19+
camera.release()
20+
cv2.destroyAllWindows()

code/7/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# My Python version is: 3.8.9
2+
opencv-python==4.5.4.60

0 commit comments

Comments
 (0)