摄像头转视频
import numpy as np
import cv2
import os
def videocapture():
cap = cv2.VideoCapture(0) # 生成读取摄像头对象
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 获取视频的宽度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 获取视频的高度
fps = cap.get(cv2.CAP_PROP_FPS) # 获取视频的帧率
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) # 视频的编码
# 定义视频对象输出
writer = cv2.VideoWriter("video_result.mp4", fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read() # 读取摄像头画面
cv2.imshow('tes-well', frame) # 显示画面
cv2.waitKey(2)
# key = cv2.waitKey(24)
writer.write(frame) # 视频保存
# 按Q退出
# if key == ord('q'):
# break
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 释放所有显示图像窗口
if __name__ == '__main__':
videocapture()
视频转图片
import cv2
vidcap = cv2.VideoCapture('E:/baiduAI/video/video_result.mp4') # 文件路径
success, image = vidcap.read()
fps = int(vidcap.get(cv2.CAP_PROP_FPS)) # 每秒有多少帧
count = 0
while success:
if count % fps == 0: # 此时是每秒取一帧
cv2.imwrite("E:\\baiduAI\\video\\jpg/%d.jpg" % int(count / fps), image)
print('Process %dth seconds: ' % int(count / fps), success)
success, image = vidcap.read()
count += 1
print('frame_number: ', count)
05-27
334