from os.path import isfile, join
from imutils import paths
import numpy as np
import random
import imutils
import cv2
import os
# define the aspect aware prepeocessor class
class AspectAwarePreprocessor:
def __init__(self, width, height, inter=cv2.INTER_AREA):
# store the target image width, height, and interpolation
# method used when resizing
self.width = width
self.height = height
self.inter = inter
def preprocess(self, image):
# grab the dimensions of the image and then initialize
# the deltas to use when cropping
(h, w) = image.shape[:2]
dW = 0
dH = 0
# if the width is smaller than the height, then resize
# along the width (i.e., the smaller dimension) and then
# update the deltas to crop the height to the desired
# dimension
if w < h:
image = imutils.resize(image, width=self.width,
inter=self.inter)
dH = int((image.shape[0] - self.height) / 2.0)
# otherwise, the height is smaller than the width so
# resize along the height and then update the deltas
# crop along the width
else:
image = imutils.resize(image, height=self.height,
inter=self.inter)
dW = int((image.shape[1] - self.width) / 2.0)
# now that our images have been resized, we need to
# re-grab the width and height, followed by performing
# the crop
(h, w) = image.shape[:2]
image = image[dH:h - dH, dW:w - dW]
# finally, resize the image to the provided spatial
# dimensions to ensure our output image is always a fixed
# size
return cv2.resize(image, (self.width, self.height),
interpolation=self.inter)
def convert_frames_to_video(pathIn,pathOut,fps):
# initialize the frame, width, and height array
frame_array = []
width_array = []
height_array = []
files = list(paths.list_images(pathIn))
random.shuffle(files)
# initialize the image preprocessor and the target size
aap = AspectAwarePreprocessor(480, 480)
size = (480, 480)
for i in range(len(files)):
# filename=pathIn + files[i]
# reading each files
img = cv2.imread(files[i])
height, width, layers = img.shape
# resize the image
img = aap.preprocess(img)
# save image to a certain directory
f = files[i]
file_name, file_ext = os.path.splitext(f)
file_num = '{}'.format(i)
file_num = file_num.zfill(5)
new_name = 'test' + file_num + file_ext
path = '/home/ruzewei/Documents/test_ai_box/images_480X480'
cv2.imwrite(os.path.join(path , new_name), img)
if i % 500 == 0:
print("[INFO] Processing{}th image".format(i))
#inserting the frames into an image array
frame_array.append(img)
height_array.append(height)
width_array.append(width)
print('The minimum of image height is: {}.'.format(np.amin(height_array)))
print('The maximum of image height is: {}.'.format(np.amax(height_array)))
print('The mean of image height is: {}.'.format(np.mean(height_array)))
print('The minimum of image width is: {}.'.format(np.amin(width_array)))
print('The maximum of image width is: {}.'.format(np.amax(width_array)))
print('The mean of image width is: {}.'.format(np.mean(width_array)))
# out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
out = cv2.VideoWriter(pathOut, 0x00000021, fps, size)
for i in range(len(frame_array)):
# writing to a image array
out.write(frame_array[i])
out.release()
def main():
pathIn= './images/image_file_001/'
pathOut = 'video.mp4'
fps = 24.0
convert_frames_to_video(pathIn, pathOut, fps)
if __name__=="__main__":
main()