import cv2
def capture_video():
capture = cv2.VideoCapture('01.mp4')
while True:
ret, frame = capture.read()
if not ret:
break
cv2.imshow('video', frame)
if cv2.waitKey(50) == 27:
break
capture_video()
cv2.waitKey(0)
cv2.destroyAllWindows()
if name == “main”:
stream_processing()
- 从摄像头中读取
```python
import cv2
def video_demo(oxff=None):
capture = cv2.VideoCapture(0) # 捕捉摄像头
while True:
ret, frame = capture.read()
# 读取摄像头,它能返回两个参数,第一个参数是bool 型的ret,其值为True或者False,代表有没有读到图片;
# 第二个参数frame,表示读取当前一帧的图片
frame = cv2.flip(frame, 1) # 翻转 等于0:上下颠倒,大于0:水平颠倒,小于0:180旋转
cv2.imshow("video", frame)
if cv2.waitKey(10) == 27: #按ESC停止(两下)
break
'''
if cv2.waitKey(10) & 0xff == ord('q'):
break
'''
video_demo()
cv2.waitKey(0)
cv2.destroyAllWindows()