opencv遇到的问题:
原代码中读取每帧图片的代码行为:
frame = video.read()
报错如下:
cv2.imencode('.jpg', frame)[1].tofile(img_file) TypeError: Expected Ptr<cv::UMat> 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)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有