Sample processing - video decomposition picture, the picture composite video

Machine learning training samples + = + classifier feature
deep learning = + artificial neural network training sample mass of
the sample is always important, sometimes a picture image is always looking for trouble, you can record a video sample will break down video for the pictures, you can get a different sample, saving time and effort.

Video pictures decomposition
API Introduction:
cv2.VideoCapture () : used to read video files
isOpened () : Indicates whether the video file is opened

import cv2
cap = cv2.VideoCapture('E:\\pictures\\video1.mp4')
isOpened = cap.isOpened() #判断是否打开视频
print(isOpened)
fps = cap.get(cv2.CAP_PROP_FPS) #视频的帧率,即一秒出现多少张图片
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #视频中图片的宽度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #视频中图片的高度
print("fps="+str(fps)+",width="+str(width)+",height="+str(height))
i = 0 #播放图片的个数
while(isOpened): #在视频打开的情况下执行
    if i == 10:
        break
    else:
        i +=1
        (flag,frame) = cap.read()
        #表示读取每一张图片,flag表示读取是否成功,frame表示图片的内容
        fileName = '0image' + str(i) + '.jpg' #每张图片的文件名
        print(fileName)
        if flag == True: #保存视频的前十个图片
            cv2.imwrite(fileName,frame,(cv2.IMWRITE_JPEG_QUALITY,100))
            #(cv2.IMWRITE_JPEG_QUALITY,100)表示采用jpg的格式,100表示的是存储质量
print("end!")

输出:
True
fps=29.008016032064127,width=540,height=960
0image1.jpg
0image2.jpg
0image3.jpg
0image4.jpg
0image5.jpg
0image6.jpg
0image7.jpg
0image8.jpg
0image9.jpg
0image10.jpg
end!

Composite video image
the API: cv2.VideoWriter ( '2.mp4', - l, 5, size)
Parameters: The first parameter indicates the video file name and the second parameter indication -1 supported decoders, the third 5 parameter indicates the current frame rate, that is, one second to play a few pictures, the fourth parameter size indicates the size of the video.

import cv2
img = cv2.imread('E:\\python_work\\0image1.jpg')
imgInfo = img.shape
size = (imgInfo[1],imgInfo[0]) #size为长和宽,即width和height
print(size)
videoWrite = cv2.VideoWriter('2.mp4',-1,5,size)
for i in range(1,11):
    fileName = '0image' + str(i) + '.jpg'
    img = cv2.imread(fileName)
    videoWrite.write(img)
print('end!')
发布了25 篇原创文章 · 获赞 0 · 访问量 443

Guess you like

Origin blog.csdn.net/qq_45445740/article/details/104491396