其他分享
首页 > 其他分享> > OpenCV从摄像头读取视频

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

在这里插入图片描述

标签:capture,读取,frame,cv2,OpenCV,camera,print,摄像头
来源: https://blog.csdn.net/qq_43707174/article/details/122288146