|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "2e8a0fac-adbd-4428-90ea-869aee2b95bf", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Xview Dataset clipping\n", |
| 9 | + "This is a component designed to clip the dataset provided by xview. " |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 3, |
| 15 | + "id": "c0421aaf-4da4-4a66-b626-e8962315afe8", |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "name": "stdout", |
| 20 | + "output_type": "stream", |
| 21 | + "text": [ |
| 22 | + "Requirement already satisfied: Pillow in /Users/maxdorzweiler/anaconda3/lib/python3.11/site-packages (9.4.0)\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "!pip install Pillow" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 2, |
| 33 | + "id": "0b7ad533-dabc-488e-bf65-59a54a85f3f9", |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "\n", |
| 38 | + "import os\n", |
| 39 | + "from PIL import Image\n" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 4, |
| 45 | + "id": "c8df047b-6167-43f1-9a31-d836580180ac", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "\n", |
| 50 | + "#directory_path is the path to the folder with the unzipped .tif images from xview dataset \n", |
| 51 | + "directory_path = os.environ.get(\"directory_path\")\n", |
| 52 | + "\n", |
| 53 | + "#destination is the path to the folder which saves all the extracted tiles. \n", |
| 54 | + "destination = os.environ.get(\"destination\")\n", |
| 55 | + "\n", |
| 56 | + "#Each image is cropped using a rectangular window with edge_length tile_size_x and tile_size_y which has to be given in number of pixels \n", |
| 57 | + "tile_size_x = os.environ.get(\"tile_size_x\")\n", |
| 58 | + "tile_size_y = os.environ.get(\"tile_size_y\")\n", |
| 59 | + "\n", |
| 60 | + "#stride_x is the length in pixels the sliding window is moved to the right after each step\n", |
| 61 | + "#stride_y is the length in pixels the sliding window is moved down after completing a row\n", |
| 62 | + "#For tumbling window stride_x must equal tile_size_x and stride_y must equal tile_size_y\n", |
| 63 | + "stride_x = os.environ.get(\"stride_x\")\n", |
| 64 | + "stride_y = os.environ.get(\"stride_y\")\n" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 5, |
| 70 | + "id": "8ed74228-937d-4d5d-b6d9-97026af3eb50", |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "\n", |
| 75 | + "def clear_directory(directory):\n", |
| 76 | + " for item in os.listdir(directory):\n", |
| 77 | + " item_path = os.path.join(directory, item)\n", |
| 78 | + " # Check if the item is a file\n", |
| 79 | + " if os.path.isfile(item_path):\n", |
| 80 | + " # If it's a file, remove it\n", |
| 81 | + " os.remove(item_path)\n", |
| 82 | + " # If it's a directory, recursively clear it\n", |
| 83 | + " elif os.path.isdir(item_path):\n", |
| 84 | + " clear_directory(item_path)\n", |
| 85 | + " os.rmdir(item_path)\n", |
| 86 | + " " |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 9, |
| 92 | + "id": "d6e89f94-490a-4c1a-80cd-f2ab30adb374", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "\n", |
| 97 | + "def sliding_window(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y):\n", |
| 98 | + " \n", |
| 99 | + " if os.path.isdir(destination):\n", |
| 100 | + " clear_directory(destination)\n", |
| 101 | + " else: \n", |
| 102 | + " os.makedirs(destination)\n", |
| 103 | + " \n", |
| 104 | + " \n", |
| 105 | + " for item in os.listdir(directory_path):\n", |
| 106 | + " if item == \".DS_Store\":\n", |
| 107 | + " continue\n", |
| 108 | + " item_path = os.path.join(directory_path, item)\n", |
| 109 | + " clipped_item = os.path.join(destination, item)\n", |
| 110 | + " os.makedirs(clipped_item)\n", |
| 111 | + " \n", |
| 112 | + " image = Image.open(item_path)\n", |
| 113 | + " width, height = image.size\n", |
| 114 | + " \n", |
| 115 | + " x_range = [0]\n", |
| 116 | + " while(x_range[-1] + stride_x + tile_size_x < width):\n", |
| 117 | + " x_range += [x_range[-1] + stride_x]\n", |
| 118 | + " \n", |
| 119 | + " y_range = [0]\n", |
| 120 | + " while(y_range[-1] + stride_y + tile_size_y < height):\n", |
| 121 | + " y_range += [y_range[-1] + stride_y]\n", |
| 122 | + " \n", |
| 123 | + " counter = 0\n", |
| 124 | + " for x in x_range:\n", |
| 125 | + " for y in y_range:\n", |
| 126 | + " cropped = image.crop((x,y, x+tile_size_x, y+tile_size_y))\n", |
| 127 | + "\n", |
| 128 | + " file_name = \"{}\".format(y)\n", |
| 129 | + " file_name += \"_{}\".format(x) + \".tif\"\n", |
| 130 | + " path = os.path.join(clipped_item, file_name)\n", |
| 131 | + " \n", |
| 132 | + " cropped.save(path)\n", |
| 133 | + " \n" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": 10, |
| 139 | + "id": "ce60bf47-7235-43a1-a4f7-5b81080d617d", |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [ |
| 142 | + { |
| 143 | + "ename": "TypeError", |
| 144 | + "evalue": "stat: path should be string, bytes, os.PathLike or integer, not NoneType", |
| 145 | + "output_type": "error", |
| 146 | + "traceback": [ |
| 147 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 148 | + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 149 | + "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m sliding_window(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y)\n", |
| 150 | + "Cell \u001b[0;32mIn[9], line 3\u001b[0m, in \u001b[0;36msliding_window\u001b[0;34m(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msliding_window\u001b[39m(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y):\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misdir(destination):\n\u001b[1;32m 4\u001b[0m clear_directory(destination)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m: \n", |
| 151 | + "File \u001b[0;32m<frozen genericpath>:42\u001b[0m, in \u001b[0;36misdir\u001b[0;34m(s)\u001b[0m\n", |
| 152 | + "\u001b[0;31mTypeError\u001b[0m: stat: path should be string, bytes, os.PathLike or integer, not NoneType" |
| 153 | + ] |
| 154 | + } |
| 155 | + ], |
| 156 | + "source": [ |
| 157 | + "sliding_window(directory_path, destination, tile_size_x, tile_size_y, stride_x, stride_y)" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | + "metadata": { |
| 162 | + "kernelspec": { |
| 163 | + "display_name": "Python 3 (ipykernel)", |
| 164 | + "language": "python", |
| 165 | + "name": "python3" |
| 166 | + }, |
| 167 | + "language_info": { |
| 168 | + "codemirror_mode": { |
| 169 | + "name": "ipython", |
| 170 | + "version": 3 |
| 171 | + }, |
| 172 | + "file_extension": ".py", |
| 173 | + "mimetype": "text/x-python", |
| 174 | + "name": "python", |
| 175 | + "nbconvert_exporter": "python", |
| 176 | + "pygments_lexer": "ipython3", |
| 177 | + "version": "3.11.9" |
| 178 | + } |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 5 |
| 182 | +} |
0 commit comments