OpenCV从摄像头读取视频

1.具体代码实现:

# 1 导入库
import cv2
import argparse

# 2 获取参数
parser = argparse.ArgumentParser()

# 3 添加参数
parser.add_argument("index_camera", help="the camera ID", type=int)

# 4 解析参数
args = parser.parse_args()
print("the camera index:", args.index_camera)

# 5 捕获摄像头的视频
capture = cv2.VideoCapture(args.index_camera)

# 6 获取 帧的宽度、高度、FPS
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)#CAP:capture,PROP:property
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)#fps:frame per second
print("帧的宽度:{}".format(frame_width))
print("帧的高度:{}".format(frame_height))
print("FPS:{}".format(fps))

# 7 判断摄像头是否打开
if capture.isOpened() is False:
    print("Camera Error!")

# 8 从摄像头读取视频,直到关闭
while capture.isOpened():
    # 9 通过摄像头,捕获帧
    ret, frame = capture.read()
    # 10 把捕获的帧,变成灰度
    gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    # 11 显示每一帧(视频流)
    cv2.imshow("frame", frame)
    cv2.imshow("gray frame", gray_frame)
    # 12 键盘输入”q“, 关闭摄像头
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
# 13 释放资源
capture.release()
# 14 关闭窗口
cv2.destroyAllWindows()

2. 运行流程:

Terminal —> python read_camera.py 0

OpenCV从摄像头读取视频

上一篇:ffplay源码分析4-音视频同步


下一篇:STM32串口通信 链表接收不定长数据帧