Python:opencv库实现调用摄像头录像并拍照

一:依赖库

import threading
import time
import cv2
from datetime import datetime

二:上代码

import cv2
from datetime import datetime
import threading
import time


class Camera(object):

    def __init__(self, video_path):
        #如果是笔记本调用外界摄像头,则把0改为1
        self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
        self.ret, self.frame = self.cap.read()
        FPS = 24.0
        # 视频写入的图像尺寸与画布尺寸不对应会导致视频无法播放,需要实时获取
        WIDTH = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        HEIGHT = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        # 设置摄像头设备分辨率
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
        # 设置摄像头设备帧率,如不指定,默认600
        self.cap.set(cv2.CAP_PROP_FPS, 24)
        # 建议使用XVID编码,图像质量和文件大小比较都兼顾的方案
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        self.out = cv2.VideoWriter(video_path, fourcc, FPS, (WIDTH, HEIGHT))

    def picture_shoot(self, image_name, image_path=r"E:\report") -> None:
        '''
        调用摄像头拍照并保存图片到本地
        :param image_name: 图片名
        :param image_path: 图片保存路径
        :return: None
        '''
        self.image_name = image_name
        self.image_path = image_path
        cv2.imwrite(self.image_path + '\\' + self.image_name, self.frame)

    def video_record(self, video_path) -> None:
        '''
        调用摄像头录制视频并保存到本地
        :param video_path: 视频保存路径
        :return: None
        '''
        self.video_path = video_path
        while (self.cap.isOpened()):
            self.ret, self.frame = self.cap.read()
            if self.ret:
                font = cv2.FONT_HERSHEY_SIMPLEX
                datet = str(datetime.now())
                frame = cv2.putText(self.frame, datet, (10, 50), font, 1,
                                    (0, 255, 255), 2, cv2.LINE_AA)
                self.out.write(frame)

    def video_stop(self):
        self.cap.release()
        self.out.release()
        cv2.destroyAllWindows()
        print('视频录制结束')


if __name__ == '__main__':
    # 录像+拍照
    path = r'E:\report\video.avi'
    camera = Camera(path)
    thread = threading.Thread(target=camera.video_record, args=(path,))
    thread.start()
    camera.picture_shoot(image_name='1.png', image_path=r'E:/report')
    time.sleep(10)
    camera.video_stop()

    # 拍照
    path = r'E:\report\video.avi'
    camera = Camera(path)
    camera.picture_shoot(image_name='2.png', image_path=r'E:/report')

三:运行结果

D:\2_Python\python.exe D:\3_PythonWorkSpace\camera.py 
视频录制结束

Process finished with exit code 0

四:查看文件

Python:opencv库实现调用摄像头录像并拍照_第1张图片

你可能感兴趣的:(python,开发语言)