{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#### Resize images\n\nWe use cv2 to read, resize and then save the resized images.\n\nThanks to @heyytanay and their notebook from petfinder:\nhttps://www.kaggle.com/heyytanay/petfinder-eda-resized-images-224-512/notebook","metadata":{}},{"cell_type":"code","source":"%%sh\npip install -q rich dabl","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport cv2\nimport dabl\nimport shutil\nimport glob\nfrom tqdm.notebook import tqdm\nfrom rich import print as _pprint\nfrom PIL import Image, ImageChops\nfrom joblib import Parallel, delayed\n\nimport warnings\nwarnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:21:24.3368Z","iopub.execute_input":"2022-02-06T13:21:24.337323Z","iopub.status.idle":"2022-02-06T13:21:24.341083Z","shell.execute_reply.started":"2022-02-06T13:21:24.337289Z","shell.execute_reply":"2022-02-06T13:21:24.340483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names = glob.glob(\"../input/happy-whale-and-dolphin/train_images/*.jpg\")\ntest_file_names = glob.glob(\"../input/happy-whale-and-dolphin/test_images/*.jpg\")\n\nprint(f\"Train Images Count: {len(train_file_names)}\")\nprint(f\"Test Images Count: {len(test_file_names)}\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%sh\nmkdir \"/kaggle/working/train_224/\"\nmkdir \"/kaggle/working/test_224/\"\n\nmkdir \"/kaggle/working/train_512/\"\nmkdir \"/kaggle/working/test_512/\"","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:27:34.068923Z","iopub.execute_input":"2022-02-06T13:27:34.069485Z","iopub.status.idle":"2022-02-06T13:27:34.07405Z","shell.execute_reply.started":"2022-02-06T13:27:34.069448Z","shell.execute_reply":"2022-02-06T13:27:34.073121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resizeImage(imagePath, outputFolder, resize=224):\n    \"\"\"\n    Function to resize Image using cv2\n    \"\"\"\n    img = cv2.imread(imagePath)\n    img = img[:, :, ::-1]\n    img = cv2.resize(img, (resize, resize))\n    imgPath = os.path.join(outputFolder, os.path.basename(imagePath))\n    cv2.imwrite(imgPath, img)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:41:28.448652Z","iopub.execute_input":"2022-02-06T13:41:28.44896Z","iopub.status.idle":"2022-02-06T13:41:28.454412Z","shell.execute_reply.started":"2022-02-06T13:41:28.448924Z","shell.execute_reply":"2022-02-06T13:41:28.453798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run in Parallel on 16 cores for quicky quick resizing and saving - 224 x 224 px\n_ = Parallel(n_jobs=16, verbose=0)(delayed(resizeImage)(fileName, \"/kaggle/working/train_224\") for fileName in tqdm(train_file_names))\n_ = Parallel(n_jobs=16, verbose=0)(delayed(resizeImage)(fileName, \"/kaggle/working/test_224\") for fileName in tqdm(test_file_names))","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:41:30.788164Z","iopub.execute_input":"2022-02-06T13:41:30.788627Z","iopub.status.idle":"2022-02-06T13:41:36.898071Z","shell.execute_reply.started":"2022-02-06T13:41:30.78859Z","shell.execute_reply":"2022-02-06T13:41:36.897325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run in Parallel on 16 cores for quicky quick resizing and saving - 512 x 512 px\n_ = Parallel(n_jobs=16, verbose=1)(delayed(resizeImage)(fileName, \"/kaggle/working/train_512\", 512) for fileName in train_file_names)\n_ = Parallel(n_jobs=16, verbose=1)(delayed(resizeImage)(fileName, \"/kaggle/working/test_512\", 512) for fileName in test_file_names)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:40:49.599416Z","iopub.execute_input":"2022-02-06T13:40:49.599989Z","iopub.status.idle":"2022-02-06T13:40:51.806203Z","shell.execute_reply.started":"2022-02-06T13:40:49.599947Z","shell.execute_reply":"2022-02-06T13:40:51.805476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test few images.","metadata":{}},{"cell_type":"code","source":"shutil.make_archive(\"/kaggle/working/train_512\", 'zip', \"/kaggle/working/train_512\")\nshutil.make_archive(\"/kaggle/working/test_512\", 'zip', \"/kaggle/working/test_512\")\nshutil.make_archive(\"/kaggle/working/train_224\", 'zip', \"/kaggle/working/train_224\")\nshutil.make_archive(\"/kaggle/working/test_224\", 'zip', \"/kaggle/working/test_224\")\n","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:44:31.261037Z","iopub.execute_input":"2022-02-06T13:44:31.261654Z","iopub.status.idle":"2022-02-06T13:44:31.267616Z","shell.execute_reply.started":"2022-02-06T13:44:31.261611Z","shell.execute_reply":"2022-02-06T13:44:31.266883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%sh\nrm -rf \"/kaggle/working/train_224/\"\nrm -rf \"/kaggle/working/test_224/\"\nrm -rf \"/kaggle/working/train_512/\"\nrm -rf \"/kaggle/working/test_512/\"","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:44:33.18541Z","iopub.execute_input":"2022-02-06T13:44:33.185813Z","iopub.status.idle":"2022-02-06T13:44:34.444816Z","shell.execute_reply.started":"2022-02-06T13:44:33.185782Z","shell.execute_reply":"2022-02-06T13:44:34.443915Z"},"trusted":true},"execution_count":null,"outputs":[]}]}