From c6458cf9c06e67fba2743a94f7d3dbef1c99683e Mon Sep 17 00:00:00 2001 From: Shubham Samanta Date: Mon, 17 Jan 2022 00:18:44 +0530 Subject: [PATCH] Update resize.py matplotlib and cv2 were added; scipy was removed. --- data/resize.py | 100 +++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/data/resize.py b/data/resize.py index 6c6aca6..722dea8 100644 --- a/data/resize.py +++ b/data/resize.py @@ -1,66 +1,70 @@ import os import constants import numpy as np -from scipy import misc, ndimage +from matplotlib.pyplot import imread, imsave +import cv2 + def resize(image, dim1, dim2): - return misc.imresize(image, (dim1, dim2)) + return cv2.resize(image, (dim2, dim1)) + def fileWalk(directory, destPath): - try: - os.makedirs(destPath) - except OSError: - if not os.path.isdir(destPath): - raise - - for subdir, dirs, files in os.walk(directory): - for file in files: - if len(file) <= 4 or file[-4:] != '.jpg': - continue - - pic = misc.imread(os.path.join(subdir, file)) - dim1 = len(pic) - dim2 = len(pic[0]) - if dim1 > dim2: - pic = np.rot90(pic) - - picResized = resize(pic,constants.DIM1, constants.DIM2) - misc.imsave(os.path.join(destPath, file), picResized) - + try: + os.makedirs(destPath) + except OSError: + if not os.path.isdir(destPath): + raise + + for subdir, dirs, files in os.walk(directory): + for file in files: + if len(file) <= 4 or file[-4:] != '.jpg': + continue + + pic = imread(os.path.join(subdir, file)) + dim1 = len(pic) + dim2 = len(pic[0]) + if dim1 > dim2: + pic = np.rot90(pic) + + picResized = resize(pic, constants.DIM1, constants.DIM2) + imsave(os.path.join(destPath, file), picResized) + def main(): - prepath = os.path.join(os.getcwd(), 'dataset-original') - glassDir = os.path.join(prepath, 'glass') - paperDir = os.path.join(prepath, 'paper') - cardboardDir = os.path.join(prepath, 'cardboard') - plasticDir = os.path.join(prepath, 'plastic') - metalDir = os.path.join(prepath, 'metal') - trashDir = os.path.join(prepath, 'trash') + prepath = os.path.join(os.getcwd(), 'dataset-original') + glassDir = os.path.join(prepath, 'glass') + paperDir = os.path.join(prepath, 'paper') + cardboardDir = os.path.join(prepath, 'cardboard') + plasticDir = os.path.join(prepath, 'plastic') + metalDir = os.path.join(prepath, 'metal') + trashDir = os.path.join(prepath, 'trash') + + destPath = os.path.join(os.getcwd(), 'dataset-resized') + try: + os.makedirs(destPath) + except OSError: + if not os.path.isdir(destPath): + raise - destPath = os.path.join(os.getcwd(), 'dataset-resized') - try: - os.makedirs(destPath) - except OSError: - if not os.path.isdir(destPath): - raise + # GLASS + fileWalk(glassDir, os.path.join(destPath, 'glass')) - #GLASS - fileWalk(glassDir, os.path.join(destPath, 'glass')) + # PAPER + fileWalk(paperDir, os.path.join(destPath, 'paper')) - #PAPER - fileWalk(paperDir, os.path.join(destPath, 'paper')) + # CARDBOARD + fileWalk(cardboardDir, os.path.join(destPath, 'cardboard')) - #CARDBOARD - fileWalk(cardboardDir, os.path.join(destPath, 'cardboard')) + # PLASTIC + fileWalk(plasticDir, os.path.join(destPath, 'plastic')) - #PLASTIC - fileWalk(plasticDir, os.path.join(destPath, 'plastic')) + # METAL + fileWalk(metalDir, os.path.join(destPath, 'metal')) - #METAL - fileWalk(metalDir, os.path.join(destPath, 'metal')) + # TRASH + fileWalk(trashDir, os.path.join(destPath, 'trash')) - #TRASH - fileWalk(trashDir, os.path.join(destPath, 'trash')) if __name__ == '__main__': - main() \ No newline at end of file + main()