栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

基于opencv 实现录像

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于opencv 实现录像

import time
import cv2
import os
class VideoSave():
   def __init__(self) -> None:
       self.Number=0
       self.LastPath=""   
   def saveImg(self,img,i):
      """保存截图"""
      # 让图片按分钟保存
      timecc=time.localtime(time.time())
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)+"/"+str(timecc.tm_mon)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)) 
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)):
         if self.LastPath!="":
            self.MergVideo(self.LastPath,self.Number)
            self.Number=0
         self.LastPath="../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min))
      cv2.imwrite(self.LastPath+"/"+str(i)+".jpg",img)

   def Videos(self):
      """读取摄像头"""
      VideoObj=cv2.VideoCapture(0)
      while True:
         ret,img=VideoObj.read()
         self.saveImg(img,self.Number)
         self.Number+=1
   def MergVideo(self,ImgListPath,Number): 
      """将图片保存为短视频"""
      path=ImgListPath
      out=cv2.VideoWriter("../videos/videos/"+time.time()+".mp4",-1,20,(640,480))
      # for root,dirs,files in os.walk(path):
      #    for i in range(len(files)):
      #为了保证图片顺序一致
      for i in range(0,Number):
            #print(path+"/"+str(i)+".jpg")
            out.write(cv2.imread(path+"/"+str(i)+".jpg") )
      out.release()

if __name__ == '__main__':
   obj=VideoSave()
   obj.Videos()import time
import cv2
import os
class VideoSave():
   def __init__(self) -> None:
       self.Number=0
       self.LastPath=""   
   def saveImg(self,img,i):
      """保存截图"""
      timecc=time.localtime(time.time())
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)+"/"+str(timecc.tm_mon)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)) 
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)):
         if self.LastPath!="":
            self.MergVideo(self.LastPath,self.Number)
            self.Number=0
         self.LastPath="../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min))
      cv2.imwrite(self.LastPath+"/"+str(i)+".jpg",img)

   def Videos(self):
      """读取摄像头"""
      VideoObj=cv2.VideoCapture(0)
      while True:
         ret,img=VideoObj.read()
         self.saveImg(img,self.Number)
         self.Number+=1
   def MergVideo(self,ImgListPath,Number): 
      """将图片保存为短视频"""
      path=ImgListPath
      out=cv2.VideoWriter("../videos/videos/"+time.time()+".mp4",-1,20,(640,480))
      # for root,dirs,files in os.walk(path):
      #    for i in range(len(files)):
      for i in range(0,Number):
            print(path+"/"+str(i)+".jpg")
            out.write(cv2.imread(path+"/"+str(i)+".jpg") )
      out.release()

if __name__ == '__main__':
   obj=VideoSave()
   obj.Videos()
``
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/294371.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号