{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install patchify","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:30:23.980968Z","iopub.execute_input":"2023-11-09T10:30:23.981818Z","iopub.status.idle":"2023-11-09T10:30:35.305983Z","shell.execute_reply.started":"2023-11-09T10:30:23.981762Z","shell.execute_reply":"2023-11-09T10:30:35.304303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from patchify import patchify\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport cv2\nImage.MAX_IMAGE_PIXELS = None\nimport numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:30:35.308146Z","iopub.execute_input":"2023-11-09T10:30:35.308434Z","iopub.status.idle":"2023-11-09T10:30:35.868698Z","shell.execute_reply.started":"2023-11-09T10:30:35.308409Z","shell.execute_reply":"2023-11-09T10:30:35.866651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def percent(numpy):\n    '''Here image is RGB image'''\n    image = Image.fromarray(numpy)\n    image_gray = image.convert('L')\n    width, height = image.size\n    total_pixels = width * height\n    array= np.asarray(image_gray)\n    black_pixels = np.count_nonzero(array < 10)\n#     print(black_pixels)\n    white_pixels = np.count_nonzero(array>250)\n#     print(white_pixels)\n    other_pixels = total_pixels - white_pixels - black_pixels\n    black_percentage = (black_pixels / total_pixels) \n    white_percentage = (white_pixels / total_pixels) \n    other_percentage = (other_pixels / total_pixels)\n    \n    return black_percentage, white_percentage, other_percentage","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:30:36.131448Z","iopub.execute_input":"2023-11-09T10:30:36.13214Z","iopub.status.idle":"2023-11-09T10:30:36.141031Z","shell.execute_reply.started":"2023-11-09T10:30:36.132067Z","shell.execute_reply":"2023-11-09T10:30:36.139391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_image_dir = '/kaggle/input/UBC-OCEAN/train_images/'\nNo_of_folder = 30\ntrain_dataset = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ntrain_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:30:37.8176Z","iopub.execute_input":"2023-11-09T10:30:37.817943Z","iopub.status.idle":"2023-11-09T10:30:37.85865Z","shell.execute_reply.started":"2023-11-09T10:30:37.817917Z","shell.execute_reply":"2023-11-09T10:30:37.857946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset['image_id_path'] = base_image_dir + train_dataset['image_id'].astype(str) + '.png' \ntrain_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:30:38.967491Z","iopub.execute_input":"2023-11-09T10:30:38.968159Z","iopub.status.idle":"2023-11-09T10:30:38.98167Z","shell.execute_reply.started":"2023-11-09T10:30:38.968106Z","shell.execute_reply":"2023-11-09T10:30:38.980493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(No_of_folder):\n    folder = '/kaggle/working/patched/'+str(train_dataset['image_id'].iloc[i])+'/'\n#     print(folder)\n    os.makedirs(folder)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:30:40.007517Z","iopub.execute_input":"2023-11-09T10:30:40.00788Z","iopub.status.idle":"2023-11-09T10:30:40.019796Z","shell.execute_reply.started":"2023-11-09T10:30:40.007854Z","shell.execute_reply":"2023-11-09T10:30:40.017515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nImage.MAX_IMAGE_PIXELS = None\nnumber = 1024\nfor i in range(No_of_folder):\n    images = (train_dataset['image_id_path'].iloc[i])\n    img = Image.open(images)\n    image = np.asarray(img)\n    del img\n    image_height, image_width, channel_count = image.shape\n    patch_height, patch_width, step = number, number, number\n    patch_shape = (patch_height, patch_width, channel_count)\n    patches = patchify(image, patch_shape, step=step)\n    print(patches.shape)\n    image_dataset = patches.reshape((-1, number, number, 3))\n    del patches\n    print(image_dataset.shape)\n    \n    for j in range(image_dataset.shape[0]):\n        \n#         for k in range(patches.shape[1]):\n        single_patch_img = image_dataset[j, :, :, :]\n        black_percent, white_percent, other_percent = percent(single_patch_img)\n        filepath = '/kaggle/working/patched/'+str(train_dataset['image_id'].iloc[i])+'/'+str(j)+'.jpg'\n        if (black_percent <= 0.05):\n            if not cv2.imwrite(filepath, single_patch_img):\n                raise Exception(\"Could not write the image\")  \n                \n    del image_dataset\n#         elif (white_percent <= 0.55 and black_percent <=0.2):\n#             if not cv2.imwrite(filepath, single_patch_img):\n#                 raise Exception(\"Could not write the image\")\n                \n#         elif (other_percent>0.3):\n#             if not cv2.imwrite(filepath, single_patch_img):\n#                 raise Exception(\"Could not write the image\")\n        ","metadata":{"execution":{"iopub.status.busy":"2023-11-09T09:17:56.342905Z","iopub.execute_input":"2023-11-09T09:17:56.343349Z","iopub.status.idle":"2023-11-09T09:30:02.897057Z","shell.execute_reply.started":"2023-11-09T09:17:56.343298Z","shell.execute_reply":"2023-11-09T09:30:02.89559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(No_of_folder):\n    img = Image.open(train_dataset['image_id_path'].iloc[0])\n    print(img.size)\n#     roa = train_dataset['image_id'].iloc[i]\n#     print(roa)\n#     ra = roa.astype('str')\n#     print('/'+ ra+'/')\n    print('/kaggle/working/'+str(train_dataset['image_id'].iloc[i])+'.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-11-09T09:08:35.091692Z","iopub.execute_input":"2023-11-09T09:08:35.092834Z","iopub.status.idle":"2023-11-09T09:08:35.128179Z","shell.execute_reply.started":"2023-11-09T09:08:35.092784Z","shell.execute_reply":"2023-11-09T09:08:35.127337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listpath = train_dataset['image_id_path'].iloc[0] \nprint(listpath)\nprint(train_dataset['image_id'].iloc[0])\nroad = '/kaggle/working/'+str(train_dataset['image_id'].iloc[0]) + '/'\nprint(road)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:58:45.056689Z","iopub.execute_input":"2023-11-09T08:58:45.056982Z","iopub.status.idle":"2023-11-09T08:58:45.064047Z","shell.execute_reply.started":"2023-11-09T08:58:45.056955Z","shell.execute_reply":"2023-11-09T08:58:45.063248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-09T10:27:13.523103Z","iopub.execute_input":"2023-11-09T10:27:13.523639Z","iopub.status.idle":"2023-11-09T10:27:14.690736Z","shell.execute_reply.started":"2023-11-09T10:27:13.5236Z","shell.execute_reply":"2023-11-09T10:27:14.688909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str(train_dataset['image_id'].iloc[1])","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:35:02.911986Z","iopub.execute_input":"2023-11-09T08:35:02.912427Z","iopub.status.idle":"2023-11-09T08:35:02.929967Z","shell.execute_reply.started":"2023-11-09T08:35:02.912388Z","shell.execute_reply":"2023-11-09T08:35:02.928307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img = Image.open(train_dataset['image_id_path'].iloc[0])\n# print(img.size)\n# plt.imshow(img)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:33:54.54314Z","iopub.execute_input":"2023-11-09T08:33:54.54352Z","iopub.status.idle":"2023-11-09T08:35:02.747378Z","shell.execute_reply.started":"2023-11-09T08:33:54.543488Z","shell.execute_reply":"2023-11-09T08:35:02.746026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = '/kaggle/working/4/140.png'\n# path1 = '/kaggle/working/4/140.jpg'","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:52:10.553513Z","iopub.execute_input":"2023-11-09T08:52:10.553982Z","iopub.status.idle":"2023-11-09T08:52:10.559194Z","shell.execute_reply.started":"2023-11-09T08:52:10.553945Z","shell.execute_reply":"2023-11-09T08:52:10.557874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# imag = Image.open(path)\n# imag1 = Image.open(path1)\n# fig, axs = plt.subplots(1, 2, sharex = True, sharey = True)\n# axs[0].imshow(imag)\n# axs[1].imshow(imag1)\n# # img_numpy = np.asarray(imag)\n# # black, white, other = percent(img_numpy)\n# # print(black, white, other)\n\n# # plt.imshow(imag)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:53:08.012594Z","iopub.execute_input":"2023-11-09T08:53:08.013074Z","iopub.status.idle":"2023-11-09T08:53:08.691434Z","shell.execute_reply.started":"2023-11-09T08:53:08.013036Z","shell.execute_reply":"2023-11-09T08:53:08.69013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}