1、批量提取视频的所有帧
注意:将所有要处理的视频存放在同一个文件夹中,文件夹中不要放入其他无关文件。
'''
批量提取视频的所有帧
'''
import os
import cv2
#存放视频的地址
videos_src_path = 'D:/Pycharm_work/Smart_factory/original_vedio'
#存放图片的地址
videos_save_path = 'D:/Pycharm_work/Smart_factory/original_image'
#返回videos_src_path路径下包含的文件或文件夹名字的列表(所有视频的文件名),按字母顺序排序
videos = os.listdir(videos_src_path)
for each_video in videos:
#获取每个视频的名称
each_video_name, _ = each_video.split('.')
#创建目录,来保存图片帧
os.mkdir(videos_save_path + '/' + each_video_name)
#获取保存图片的完整路径,每个视频的图片帧存在以视频名为文件名的文件夹中
each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'
#获取每个视频的完整路径
each_video_full_path = os.path.join(videos_src_path, each_video)
#读入视频
cap = cv2.VideoCapture(each_video_full_path)
frame_count = 1
success = True
while (success):
#提取视频帧,success为是否成功获取视频帧(true/false),第二个返回值为返回的视频帧
success, frame = cap.read()
if success == True:
#存储视频帧
cv2.imwrite(each_video_save_full_path + "%06d.jpg" % frame_count, frame)
frame_count = frame_count + 1
2、批量提取视频的第n帧
有时候我们只需要处理视频中的某一帧,并不需要提取所有帧,那么如何来批量提取视频的第n帧呢?其实很简单,只需要对上面的代码进行一点点的修改。
'''
批量提取视频的第n帧
'''
import os
import cv2
#存放视频的地址
videos_src_path = 'D:/Pycharm_work/Smart_factory/original_vedio'
#存放图片的地址
videos_save_path = 'D:/Pycharm_work/Smart_factory/original_image'
#返回videos_src_path路径下包含的文件或文件夹名字的列表(所有视频的文件名),按字母顺序排序
videos = os.listdir(videos_src_path)
for each_video in videos:
#获取每个视频的名称
each_video_name, _ = each_video.split('.')
#获取每个视频的完整路径
each_video_full_path = os.path.join(videos_src_path, each_video)
#读入视频
cap = cv2.VideoCapture(each_video_full_path)
#设置提取视频的第几帧(n=几,就提取第几帧)
n=1
i=1
while i <= n:
#提取视频帧,success为是否成功获取视频帧(true/false),第二个返回值为返回的视频帧
success, frame = cap.read()
i+=1
if success == True:
#存储视频帧,图片名与视频名一致
cv2.imwrite(videos_save_path + each_video_name+".jpg", frame)
3、提取单个视频的所有帧
如果只想将单个视频转为图片,可以采取一下代码。
'''
提取单个视频的所有帧
'''
import cv2
import numpy as np
def save_image(image,addr,num):
#存储的图片路径
address=addr+str(num)+'.jpg'
#存储图片
cv2.imwrite(address,image)
#读入视频
videoCapture=cv2.VideoCapture("/mnt/sdb/video/filename.avi")
#读取视频帧
success,frame=videoCapture.read()
i=0
while success:
i=i+1
#保存图片
save_image(frame,'/mnt/sdb/img/',i)
if success:
print('save image:',i)
#读取视频帧
sucess,frame=videoCapture.read()
4、提取单个视频的部分帧-WKX
import cv2
vc = cv2.VideoCapture(r'F:\BaiduNetdiskDownload\zhihuigongchang\1\5d11ef141bc2b48a85797fb9a8e7bbc8.mp4')#这是我的路径
rval = vc.isOpened()
outpath='F:\\BaiduNetdiskDownload\\zhihuigongchang\\image5\\'
print("rval::",rval)
c=0
while rval:
rval, frame = vc.read()#farame就是帧对象
print("rval::",rval)
if rval:
if(c>9999):#只提取前10000帧
break
if(c//10==0):#注意真除和整除的区别
# print("frame::",frame)
cv2.imwrite(outpath+"000"+str(c) + '.jpg', frame) #000i
elif(c//100==0):
cv2.imwrite(outpath+'00'+str(c) + '.jpg', frame) #00i
elif(c//1000==0):
cv2.imwrite(outpath+'0'+str(c) + '.jpg', frame) #0i
else:
cv2.imwrite(outpath+str(c) + '.jpg', frame) #i
c=c+1
else:
break
vc.release()
5、批量提取间隔帧的图片
'''
批量提取视频的所有帧
'''
import os
import cv2
#存放视频的地址
videos_src_path = 'D:/Pycharm_work/Smart_factory/original_vedio'
#存放图片的地址
videos_save_path = 'D:/Pycharm_work/Smart_factory/original_image_2/original_image_100zhen'
#返回videos_src_path路径下包含的文件或文件夹名字的列表(所有视频的文件名),按字母顺序排序
videos = os.listdir(videos_src_path)
for each_video in videos:
#获取每个视频的名称
each_video_name, _ = each_video.split('.')
#创建目录,来保存图片帧
os.mkdir(videos_save_path + '/' + each_video_name)
#获取保存图片的完整路径,每个视频的图片帧存在以视频名为文件名的文件夹中
each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'
#获取每个视频的完整路径
each_video_full_path = os.path.join(videos_src_path, each_video)
#读入视频
cap = cv2.VideoCapture(each_video_full_path)
frame_count = 0
success = True
while (success):
#提取视频帧,success为是否成功获取视频帧(true/false),第二个返回值为返回的视频帧
success, frame = cap.read()
if success == True:
#存储视频帧
if (frame_count % 100==0):
cv2.imwrite(each_video_save_full_path + "%06d.jpg" % frame_count, frame)
frame_count = frame_count + 1