{"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.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport os\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:21:24.336800Z","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":"os.makedirs('/kaggle/working/train_images_128')\nos.makedirs('/kaggle/working/test_images_128')","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.074050Z","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":"NEW_SIZE = (128, 128) # (height, width)\nINTERPOLATION = cv2.INTER_CUBIC","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:40:01.774313Z","iopub.execute_input":"2022-02-06T13:40:01.774600Z","iopub.status.idle":"2022-02-06T13:40:01.779084Z","shell.execute_reply.started":"2022-02-06T13:40:01.774568Z","shell.execute_reply":"2022-02-06T13:40:01.778009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_resized_images(image_path, image_output_path):\n    image_list = os.listdir(image_path)\n    for image in tqdm(image_list):\n        img = cv2.imread(os.path.join(image_path, image))\n        img = cv2.resize(img, NEW_SIZE[::-1], interpolation=INTERPOLATION)\n        cv2.imwrite(os.path.join(image_output_path , image), img)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:41:28.448652Z","iopub.execute_input":"2022-02-06T13:41:28.448960Z","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":"train_image_path = \"../input/happy-whale-and-dolphin/train_images\"\ntrain_image_output_path = \"/kaggle/working/train_images_128\"\ncreate_resized_images(train_image_path, train_image_output_path)","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.788590Z","shell.execute_reply":"2022-02-06T13:41:36.897325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_path = \"../input/happy-whale-and-dolphin/test_images\"\ntest_image_output_path = \"/kaggle/working/test_images_128\"\ncreate_resized_images(test_image_path, test_image_output_path)","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":"import matplotlib.pyplot as plt\ndef plot_image_samples_test():\n    root_path = \"/kaggle/working/\"\n    images_folder=\"test_images_128/\"\n\n    f, ax = plt.subplots(4, 4, figsize=(16,16))\n    file_list = list(os.listdir(root_path+images_folder))\n    for i in range(16):\n        file = file_list[i]\n        img = cv2.imread(root_path+images_folder+file)\n        ax[i//4, i%4].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        ax[i//4, i%4].set_title(\"Test image: \"+file+f\"\\nsize: {img.shape}\")\n        ax[i//4, i%4].axis('off')\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":"plot_image_samples_test()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T13:44:33.185410Z","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":[]}]}