Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Edon, Sean Darwin S.

March 6, 2020
CPE51

1. Video to Frames (Code)


# show how many frames are created
import cv2 currentframe += 1
import os else:
break
# Read the video from specified path
cam =
cv2.VideoCapture("E:\TITI\Yow.mp4") # Release all space and windows once done
cam.release()
try: cv2.destroyAllWindows()

# creating a folder named data


if not os.path.exists('Yow'):
os.makedirs('Yow')

# if not created then raise error


except OSError:
print ('Error: Creating directory of data')

# frame
currentframe = 0

while(True):

# reading from frame


ret,frame = cam.read()

if ret:
# if video is still left continue creating
images
name = './data/frame' +
str(currentframe) + '.jpg'
print ('Creating...' + name)

# writing the extracted images


cv2.imwrite(name, frame)

# increasing counter so that it will


2. Frames to Video (Code)
def main():
import cv2 pathIn= './data/'
import numpy as np pathOut = 'video.avi'
import os fps = 60
convert_frames_to_video(pathIn,
from os.path import isfile, join pathOut, fps)

def if __name__=="__main__":
convert_frames_to_video(pathIn,pathOut,fp main()
s):
frame_array = []
files = [f for f in os.listdir(pathIn) if
isfile(join(pathIn, f))]

#for sorting the file names properly


files.sort(key = lambda x: int(x[5:-4]))

for i in range(len(files)):
filename=pathIn + files[i]
#reading each files
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
print(filename)
#inserting the frames into an image
array
frame_array.append(img)

out =
cv2.VideoWriter(pathOut,cv2.VideoWriter
_fourcc(*'DIVX'), fps, size)

for i in range(len(frame_array)):
# writing to a image array
out.write(frame_array[i])
out.release()

You might also like