I have seen a strange behavior of my program. It should perform thousands of iterations, and what I have noticed on the terminal is that execution freezes for some seconds, then it goes on very fast, then it freezes then goes on fast ...
I'm using terminal in Visual Studio and tried also Ubuntu cmd. My code (down here) opens all images in an input folder, does size-operations, saves them all in an output folder.
Is this about some not best practices I used? Is it due to operating system? Due to other task interference?
Thanks in advance
# Give in argument a folder path with images in it and an output folder path
# This program will scale all images to fixed size, cutting extra parts and padding to black missing parts.
import cv2 as cv
import numpy as np
import argparse
import numpy as np
from os import listdir
from os.path import isfile, join, exists
width = 1280
height = 720
def rescaleAllImages(args):
inputFolder = args['input_directory']
outputFolder = args['output_directory']
print(inputFolder+" > "+outputFolder)
if(not exists(inputFolder)):
print("ERROR: "+inputFolder+" do not exist")
if(not exists(outputFolder)):
print("ERROR: "+outputFolder+" do not exist")
countIterations=0
for currImage in listdir(inputFolder):
if isfile(join(inputFolder, currImage)):
inFilePath = inputFolder+'/'+currImage
currFrameName = currImage.split('.')[0]
countIterations += countIterations
print("{0} \t{1}".format(countIterations, currFrameName))
img = cv.imread(inFilePath)
try:
rows, columns, colors = img.shape
except Exception as e:
print("Failed to open {0}: {1}\n".format(inFilePath, str(e)))
continue
print("\nImage size: {0}x{1}".format(columns, rows, countIterations))
# set blank image. Channel = 3 means R, G, B. If is black-white then only 1 channel is needed
r_img = np.zeros((height,width,3), dtype='uint8') #(height, width, channels)
# print("x axix: {0} {1}".format(columns, width))
xTop = np.min((columns, width))
# print("y axix: {0} {1}".format(rows, height))
yTop = np.min((rows, height))
print("min size is {0}x{1}".format(xTop,yTop))
r_img[0:yTop, 0:xTop, :] = img[0:yTop, 0:xTop, :]
outFilePath = outputFolder+'/'+currImage
cv.imwrite(outFilePath, r_img)
# halt = 0
# cv.imshow(currFrameName, img)
# cv.imshow("resized", r_img)
# k = cv.waitKey(0)
# print(k)
# if k == 27: # Stop (Esc key)
# halt = 1
# cv.destroyAllWindows()
# if halt:
# break
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'input_directory',
type=str,
default=None,
help=('Contains path to images folder'))
parser.add_argument(
'output_directory',
type=str,
default=None,
help=('Here output will be stored'))
parser.add_argument(
'--optional_argument', # -- before argument means "optional"
type=str,
default=None,
help='unused')
rescaleAllImages(vars(parser.parse_args()))