python使用opencv调取摄像头捕获图片或视频通用模板

之前一直不清楚opencv调用摄像头的具体方法,于是参考b站视频写了个通用模板,以后可以根据需要自行调整。视频链接
先定义图片和视频的保存路径

img_path = './img_video/test_img.jpg'  # 图片保存路径
video_path = './img_video/test_video.mp4'  # 视频保存路径

这个是图片处理的函数,由摄像头捕获的图片或视频帧可以通过这个函数处理后再返回,我这里没有作处理直接返回。

def process_frame(frame):
    return frame

捕获单张图片并保存

# 延迟2秒
    time.sleep(2)
    # 调用摄像头,0是默认摄像头,1是外置摄像头
    cap = cv2.VideoCapture(0)
    # 捕获并处理一帧画面
    success, frame = cap.read()
    if not success:
        print('error!')
    frame = process_frame(frame)
    # 关闭摄像头
    cap.release()
    # 关闭窗口
    cv2.destroyAllWindows()
    # 保存图片
    cv2.imwrite(img_path, frame)
    print('图片已保存', img_path)

实时显示视频并保存(捕获视频其实也就是循环捕获图片)

 # 调用摄像头,0是默认摄像头,1是外置摄像头
    cap = cv2.VideoCapture(0)
    # 打开cap
    cap.open(0)
    #视频尺寸
    frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                  cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #文件编码方式
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    fps = cap.get(cv2.CAP_PROP_FPS)

    out = cv2.VideoWriter(video_path, fourcc, fps,
                          (int(frame_size[0]), int(frame_size[1])))

    # --!!!关键部分,无限循环--
    while cap.isOpened():
        # 获取画面
        success, frame = cap.read()
        # 未捕获到,报错并退出
        if not success:
            print('error!')
            break
        # 对每帧进行处理
        frame = process_frame(frame)

        # 将处理后的帧写入视频文件
        out.write(frame)

        # 实时显示图像
        cv2.imshow('press q to quit', frame)
        # 按q或esc退出
        if cv2.waitKey(1) in [ord('q'), 27]:
            break

    # 关闭图像窗口
    cv2.destroyAllWindows()
    out.release()
    # 关闭摄像头
    cap.release()
    print('视频已保存', video_path)

完整代码:(需要在main里自行调整捕获图片还是视频)

import cv2
import matplotlib.pyplot as plt
import time

img_path = './img_video/test_img.jpg'  # 图片保存路径
video_path = './img_video/test_video.mp4'  # 视频保存路径

# 自定义处理图像函数,也可以不处理直接返回
def process_frame(frame):
    return frame

# 调用摄像头拍摄照片并保存
def get_img():
    # 延迟2秒
    time.sleep(2)
    # 调用摄像头,0是默认摄像头,1是外置摄像头
    cap = cv2.VideoCapture(0)
    # 捕获并处理一帧画面
    success, frame = cap.read()
    if not success:
        print('error!')
    frame = process_frame(frame)
    # 关闭摄像头
    cap.release()
    # 关闭窗口
    cv2.destroyAllWindows()
    # 保存图片
    cv2.imwrite(img_path, frame)
    print('图片已保存', img_path)


# 调用摄像头捕获并保存视频
def get_video():
    # 调用摄像头,0是默认摄像头,1是外置摄像头
    cap = cv2.VideoCapture(0)
    # 打开cap
    cap.open(0)
    #视频尺寸
    frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                  cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #文件编码方式
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    fps = cap.get(cv2.CAP_PROP_FPS)

    out = cv2.VideoWriter(video_path, fourcc, fps,
                          (int(frame_size[0]), int(frame_size[1])))

    # --!!!关键部分,无限循环--
    while cap.isOpened():
        # 获取画面
        success, frame = cap.read()
        # 未捕获到,报错并退出
        if not success:
            print('error!')
            break
        # 对每帧进行处理
        frame = process_frame(frame)

        # 将处理后的帧写入视频文件
        out.write(frame)

        # 实时显示图像
        cv2.imshow('press q to quit', frame)
        # 按q或esc退出
        if cv2.waitKey(1) in [ord('q'), 27]:
            break

    # 关闭图像窗口
    cv2.destroyAllWindows()
    out.release()
    # 关闭摄像头
    cap.release()
    print('视频已保存', video_path)


def main():
    #get_img()
    get_video()

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值