1. 代码中用到的函数介绍
1)函数名cv2.VideoCapture(0):通过摄像头捕获实时图像数据,返回值:有,参数为摄像头代号,0为默认摄像头。
2)函数VideoCapture.set( propId , value ):设置视频参数。返回值:布尔值。propId:需要设置的视频参数,value :设置的参数值。propId为1:基于0的索引将被解码/捕获下一帧。propId为3:视频每一帧的宽。propId为4:视频每一帧的高。
3)函数VideoWriter(video_dir, fourcc, fps, img_size):函数中的参数分别对应文件路径、存储格式、帧率、图像大小
2. 代码如下
import cv2
def video_from_cap(path_file):
video_capture = cv2.VideoCapture(0)
video_capture.set(3,640)
video_capture.set(4,480)
video_capture.set(1, 10.0)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out=cv2.VideoWriter(path_file+'out.mp4',fourcc,10,(640,480))
while True:
ret,frame = video_capture.read()
if ret == True:
out.write(frame)
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
def main():
path_file='img/'
video_from_cap(path_file)
if __name__=="__main__":
main()
网友评论