python视频帧提取并保存 | 您所在的位置:网站首页 › 参考文献两端不对齐怎么弄出来 › python视频帧提取并保存 |
opencv遇到的问题: 原代码中读取每帧图片的代码行为: frame = video.read() 报错如下: cv2.imencode('.jpg', frame)[1].tofile(img_file) TypeError: Expected Ptr for argument '%s' 将其改为: flag, frame = video.read() 运行正常。 文件存储的形式 代码如下:import cv2 import os import json def get_frame(video_path): res = [] for each in os.listdir(video_path): new_path = os.path.join(video_path, each) video = cv2.VideoCapture(new_path) img_path = './img' + '/' + str(each.split('.')[0]) if not os.path.exists(img_path): os.mkdir(img_path) os.chdir(img_path) #改变当前工作目录至图片保存文件夹 img_num = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) video_fps = video.get(cv2.CAP_PROP_FPS) video_size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))) video_message = {'name': each, 'fps': video_fps, 'size': video_size, 'nums': img_num} res.append(video_message) for i in range(img_num): flag, frame = video.read() img_file = '0000' + str(i) + '.jpg' print(img_file) cv2.imencode('.jpg', frame)[1].tofile(img_file) video.release() os.chdir(os.path.dirname(os.path.abspath(__file__)))# 返回代码工作目录 with open('data.json', 'w') as f: json.dump(res, f) if __name__ == '__main__': videoPath = "./video" get_frame(videoPath) |
CopyRight 2018-2019 实验室设备网 版权所有 |