I am trying to extract frames from an mp4 and then generate back the the video from recently extracted frames using cv2. The problem is that the generated video seems to overlap frames somehow and some of the text from previous frames appears in the the current frame.
Below is the code I am using to extract the video to frames:
import cv2
import os
import glob
path = r"C:\Users\moham\OneDrive\Documents\Uni Freiburg\Semester 4\Study
Project\datasets\Videos\Plot11_SIDE_2021_04_11_10_00_02_fps_60_trim.mp4"
cap = cv2.VideoCapture(path)
try:
if not os.path.exists('data'):
os.makedirs('data')
except OSError:
print ('Error: Creating directory of data')
currentFrame = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
# Saves image of the current frame in jpg file
name = './data/frame' + str(currentFrame) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
and here is the code to convert frames back to an mp4 video:
img_array = []
for filename in glob.glob(f"/*.jpg"):
img = cv2.imread(filename)
height, width, _ = img.shape
size = (width, height)
img_array.append(img)
out = cv2.VideoWriter(f"bees_reconstructed.mp4", cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
I have added a GIF for what happens with the reconstructed video.
