opencv-python处理视频,常用代码记录
import cv2
import os
def video_to_frames(video_path, out_path=r"result", frequency=1):
if not os.path.exists(out_path):
os.makedirs(out_path)
# 读帧
cap = cv2.VideoCapture(video_path)
# 计数
cnt = 1
while True:
flag, frame = cap.read()
if flag is False:
print("End")
break
if cnt % frequency == 0:
cv2.imwrite(out_path + '\\' + str(cnt // frequency) + '.jpg', frame)
cnt += 1
print("succeed")
if __name__ == "__main__":
video_path = r"D:\machine_learning\exercise\detection_gas\放喷管线\61e1ec1a7cfa805d8415c11495f6f56d.mp4"
video_to_frames(video_path)