0

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()))
  • did you try to run it directly in system without `Visual Studio` ? – furas Jun 18 '21 at 10:10
  • @furas yes I tried also terminal with little best performance but same behaviour – Francesco Pagani Jun 18 '21 at 12:19
  • 1
    It might be due to SSD caching. When the cache is full, SSD request can be very slow on low-end SSD (sometimes slower than an average HDD). Alternatively, It could be the Python GC performing a full collection. Another possibility is that you use memory swap because of a high memory usage on the target machine. – Jérôme Richard Jun 18 '21 at 17:51
  • 1
    You can [profile](https://stackoverflow.com/q/582336/11384184) a Python script to know which line is actually slow, it helps pinpoint the problem. You can also watch your OS ressources monitor while you run the script to catch a culprit. My guess would be IO (reading or writing large images) or CPU (finding the min/max of large images, copying the `img` into the `r_img`). But without measures or data to reproduce (images), I can't tell. – Lenormju Jun 18 '21 at 20:06

0 Answers0