python - 我在从视频中提取图像并将它们保存到特定文件夹时遇到问题
问题描述
每个人。我是一个在编程方面苦苦挣扎的大一新生。我将非常感谢您的任何帮助或建议。
我想让这段代码完成这些工作。
- 读取特定文件夹中的视频文件。
- 从此视频文件中提取帧图像
- 将它们保存在特定文件夹中(例如,从 video_1.mp4 制作文件夹 video_1_frameimage 并将图像 video_1_frame0、video_1_frame300、......等保存到此特定文件夹中)
为了实现这个功能,我编写了这段代码,但它并不能很好地工作。
感谢您阅读我的问题!
import cv2
import os
path_for_video = "C:/Users/정지원/Desktop/Video_files"
video_list = os.listdir(path_for_video)
for video_file in video_list:
file_path= os.path.join(path_for_video,video_file)
cap = cv2.VideoCapture(file_path)
index = 0
while (True):
ret, frame = cap.read()
if ret == False:
break
if index % 300 == 0:
name = video_file + '_frame' + str(index) + '.jpg'
print('Creating...' + name)
if not os.path.exists('C:/Users/정지원/Desktop/Video_files/'+ video_file):
os.makedirs('C:/Users/정지원/Desktop/Video_files/' + video_file)
path_for_image = 'C:/Users/정지원/Desktop/Video_files/' + video_file
cv2.imwrite(os.path.join(path_for_image, name), frame)
index += 1
cap.release()
cv2.destroyAllWindows()
解决方案
推荐阅读
- c# - Xml SelectSingleNode() 返回 null
- python - Python:在多路复用 io 传感器数据时执行 CPU 繁重任务的有效方法?
- python - 如何更新python动画(tkinter)中的曲线图
- python - Python在分数之间交替+和-
- forms - Symfony 3.4 和模态表单验证
- qt - 如何将 Gradientcolor 应用于 ArcItem Strokecolor?
- python - 通过 Python 阅读 Outlook 共享日历
- r - 如何使用 nls() 拟合指数衰减模型中的多个常数?
- html5-canvas - 使用 konva 图像模拟对象拟合/对象位置属性的最佳策略
- java - JasperReports 在固定字符串数据输入上出现 IndexOutOfBoundsException 中断(Java/Gradle 内置)