Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 89708c6

Browse files
committed
提交代码
1 parent da3b306 commit 89708c6

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

‎xianhuan/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Python技术 公众号文章代码库
1010

1111
## 实例代码
1212

13+
[几行代码,实现Python捕获、播放和保存摄像头视频!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/videobase):几行代码,实现Python捕获、播放和保存摄像头视频!
14+
1315
[几行代码迅速提取音频,YYDS!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/extractaudio):几行代码迅速提取音频,YYDS!
1416

1517
[一行代码,生成和读取二维码!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/qrcode):一行代码,生成和读取二维码!

‎xianhuan/videobase/capturevideo.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import cv2 as cv
7+
cap = cv.VideoCapture(0)
8+
if not cap.isOpened():
9+
print("Cannot open camera")
10+
exit()
11+
while True:
12+
# 逐帧捕获
13+
ret, frame = cap.read()
14+
# 如果正确读取帧,ret为True
15+
if not ret:
16+
break
17+
# 显示结果帧e
18+
cv.imshow('frame', frame)
19+
if cv.waitKey(1) == ord('q'):
20+
break
21+
# 完成所有操作后,释放捕获器
22+
cap.release()
23+
cv.destroyAllWindows()

‎xianhuan/videobase/playvideo.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import cv2 as cv
7+
cap = cv.VideoCapture('video.mp4')
8+
while cap.isOpened():
9+
ret, frame = cap.read()
10+
# 如果正确读取帧,ret为True
11+
if not ret:
12+
break
13+
cv.imshow('frame', frame)
14+
if cv.waitKey(25) == ord('q'):
15+
break
16+
cap.release()
17+
cv.destroyAllWindows()

‎xianhuan/videobase/savevideo.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import cv2 as cv
7+
cap = cv.VideoCapture(0)
8+
# 定义编解码器并创建VideoWriter对象
9+
fourcc = cv.VideoWriter_fourcc(*'MJPG')
10+
out = cv.VideoWriter('output.mp4', fourcc, 20.0, (640, 480))
11+
while cap.isOpened():
12+
ret, frame = cap.read()
13+
if not ret:
14+
break
15+
frame = cv.flip(frame, 1)
16+
# 写翻转的框架
17+
out.write(frame)
18+
cv.imshow('frame', frame)
19+
if cv.waitKey(1) == ord('q'):
20+
break
21+
# 完成工作后释放所有内容
22+
cap.release()
23+
out.release()
24+
cv.destroyAllWindows()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /