使用python实现电脑摄像头的视频录制

    最近我忽然有个想法,我是否可以用python实现使用我自己笔记本的摄像头监控家里的情况,每拍摄一个小时的视频自动保存为一个文件,或者可以把视频文件上传到云服务器。所以用行动尝试把想法编程实现。

有如下难点:

     1. 使用python操作笔记本的摄像头

     2. 控制摄像头拍摄视频,对视频流编码保存到本地磁盘

     3. 连接云服务器,上传视频文件

一,使用python操作笔记本摄像头

      1,python opencv 安装

pip install numpy Matplotlib
pip install opencv-python

      使用 pip install opencv-python 下载失败,直接从https://www.lfd.uci.edu/~gohlke/pythonlibs/下载

 opencv_python‑3.4.2‑cp36‑cp36m‑win32.whl 

安装 : pip install opencv_python‑3.4.2‑cp36‑cp36m‑win32.whl 

测试cv2:

def test():
    '''
   调用摄像头,捕捉图像
   '''
    import  time
    #读取摄像头,0表示系统默认摄像头
    cap = cv2.VideoCapture(0)
    while True:
        #读取图像
        ret,photo=cap.read()
        #将图像传送至窗口
        cv2.imshow('Please Take Your Photo!!',photo)
        
        #设置等待时间,若数字为0则图像定格
        key=cv2.waitKey(2)
        #按空格获取图像
        if key==ord(" "):
            #以当前时间存储
            filename = time.strftime('%Y%m%d-%H%M%S') + ".jpg"
            #保存位置
            cv2.imwrite(filename,photo)
        #按“q”退出程序
        if key==ord("q"):
            cap.release()
            break
            pass

 

2. 定义一个摄像头的类实现录制一段时长的视频并保存在本地的方法

# -*- coding: utf-8 -*-
import cv2
import  time
#定义摄像头类
class cameraComput(object):
    def __init__(self):
        #获取摄像头对象,0 系统默认摄像头
        self.cap = cv2.VideoCapture(0)
        #判断摄像头是否打开,没有则打开
        if not self.cap.isOpened():
            self.cap.open()

    def getFrame(self):
        ret,frame = self.cap.read()
        if ret:
            cv2.imshow("frame",frame)
            time.sleep(5)
        return frame

    #录制一段时长的
    def saveVideo(self,filepath,delays):
        # Define the codec and create VideoWriter object
        #视频编码
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        outputPath=filepath
        # 20fps ,640*480size
        out = cv2.VideoWriter(outputPath,fourcc, 20.0, (640,480))
        startTime = time.time()
        while(self.cap.isOpened):
            ret,frame = self.cap.read()
            if ret:
                #翻转图片
                # frame = cv2.flip(frame,0)
                # write the flipped frame
                out.write(frame)
                cv2.imshow('frame',frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                break
            if time.time() - startTime > delays :
                break
        out.release()
        cv2.destroyAllWindows()
        return True

    #保存一个快照
    def saveSnapshot(self,filepath):
        if self.cap.isOpened :
            ret,frame = self.cap.read()
            if ret:
                cv2.imwrite(filepath,frame)
            else:
                print("save snapshot fail")
                return False
        return True

    def releaseDevice(self):
        #释放设备
        self.cap.release()


    def reOpen(self):
        if not self.cap.isOpened():
            print("re opened device")
            self.cap = cv2.VideoCapture(0)
            if not self.cap.isOpened():
                self.cap.open()

 

 

 

 

 

 

   

 

 

 

你可能感兴趣的:(python,opencv)