diff --git a/component-library/input/input-xView-clipping.cwl b/component-library/input/input-xView-clipping.cwl new file mode 100644 index 00000000..7b93293d --- /dev/null +++ b/component-library/input/input-xView-clipping.cwl @@ -0,0 +1,39 @@ +cwlVersion: v1.2 +class: CommandLineTool + +baseCommand: "claimed" + +inputs: + component: + type: string + default: docker.io/mdorzweiler/claimed-input-xview-clipping:0.1 + inputBinding: + position: 1 + prefix: --component + log_level: + type: string + default: "INFO" + inputBinding: + position: 2 + prefix: --log_level + directory_path: + type: string + default: None + inputBinding: + position: 3 + prefix: --directory_path + window_size: + type: string + default: None + inputBinding: + position: 4 + prefix: --window_size + step_size: + type: string + default: None + inputBinding: + position: 5 + prefix: --step_size + + +outputs: [] diff --git a/component-library/input/input-xView-clipping.ipynb b/component-library/input/input-xView-clipping.ipynb new file mode 100644 index 00000000..292109c6 --- /dev/null +++ b/component-library/input/input-xView-clipping.ipynb @@ -0,0 +1,178 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "2e8a0fac-adbd-4428-90ea-869aee2b95bf", + "metadata": {}, + "source": [ + "## Xview Dataset clipping\n", + "This is a component designed to clip the dataset provided by xview. " + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "c0421aaf-4da4-4a66-b626-e8962315afe8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: Pillow in /Users/maxdorzweiler/anaconda3/lib/python3.11/site-packages (9.4.0)\n" + ] + } + ], + "source": [ + "!pip install Pillow" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "id": "0b7ad533-dabc-488e-bf65-59a54a85f3f9", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import os\n", + "from PIL import Image\n" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "c8df047b-6167-43f1-9a31-d836580180ac", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "#directory_path is the path to the folder with the unzipped .tif images from xview dataset \n", + "directory_path = os.environ.get(\"directory_path\")\n", + "\n", + "#Each image is cropped using a square window with edge_length window_size which has to be given in number of pixels \n", + "window_size = os.environ.get(\"window_size\")\n", + "\n", + "#step_size is the length in pixels the sliding window is moved after each step\n", + "#For tumbling window step_size must equal window_size\n", + "step_size = os.environ.get(\"step_size\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "8ed74228-937d-4d5d-b6d9-97026af3eb50", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def clear_directory(directory):\n", + " for item in os.listdir(directory):\n", + " item_path = os.path.join(directory, item)\n", + " # Check if the item is a file\n", + " if os.path.isfile(item_path):\n", + " # If it's a file, remove it\n", + " os.remove(item_path)\n", + " # If it's a directory, recursively clear it\n", + " elif os.path.isdir(item_path):\n", + " clear_directory(item_path)\n", + " os.rmdir(item_path)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "d6e89f94-490a-4c1a-80cd-f2ab30adb374", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def sliding_window(directory_path, window_size, step_size):\n", + " \n", + " parent_dir = os.path.dirname(directory_path)\n", + "\n", + " clipped = os.path.join(parent_dir, \"clipped\")\n", + " if os.path.isdir(clipped):\n", + " clear_directory(clipped)\n", + " else: \n", + " os.makedirs(clipped)\n", + " \n", + " \n", + " for item in os.listdir(directory_path):\n", + " if item == \".DS_Store\":\n", + " continue\n", + " item_path = os.path.join(directory_path, item)\n", + " clipped_item = os.path.join(clipped, item)\n", + " os.makedirs(clipped_item)\n", + " \n", + " image = Image.open(item_path)\n", + " width, height = image.size\n", + " \n", + " x_range = [0]\n", + " while(x_range[-1] + step_size + window_size < width):\n", + " x_range += [x_range[-1] + step_size]\n", + " \n", + " y_range = [0]\n", + " while(y_range[-1] + step_size + window_size < height):\n", + " y_range += [y_range[-1] + step_size]\n", + " \n", + " counter = 0\n", + " for x in x_range:\n", + " for y in y_range:\n", + " cropped = image.crop((x,y, x+window_size, y+window_size))\n", + "\n", + " file_name = \"{}\".format(y)\n", + " file_name += \"_{}\".format(x) + \".tif\"\n", + " path = os.path.join(clipped_item, file_name)\n", + "\n", + " cropped.save(path)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "ce60bf47-7235-43a1-a4f7-5b81080d617d", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "expected str, bytes or os.PathLike object, not NoneType", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[93], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m sliding_window(directory_path, window_size, step_size)\n", + "Cell \u001b[0;32mIn[92], line 3\u001b[0m, in \u001b[0;36msliding_window\u001b[0;34m(directory_path, window_size, step_size)\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, window_size, step_size):\n\u001b[0;32m----> 3\u001b[0m parent_dir \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(directory_path)\n\u001b[1;32m 5\u001b[0m clipped \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(parent_dir, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mclipped\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misdir(clipped):\n", + "File \u001b[0;32m:152\u001b[0m, in \u001b[0;36mdirname\u001b[0;34m(p)\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: expected str, bytes or os.PathLike object, not NoneType" + ] + } + ], + "source": [ + "sliding_window(directory_path, window_size, step_size)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/component-library/input/input-xView-clipping.job.yaml b/component-library/input/input-xView-clipping.job.yaml new file mode 100644 index 00000000..65cef74e --- /dev/null +++ b/component-library/input/input-xView-clipping.job.yaml @@ -0,0 +1,24 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: input-xview-clipping +spec: + template: + spec: + containers: + - name: input-xview-clipping + image: docker.io/mdorzweiler/claimed-input-xview-clipping:0.1 + workingDir: /opt/app-root/src/ + command: ["/opt/app-root/bin/ipython","claimed_input-xView-clipping.ipynb"] + env: + - name: log_level + value: value_of_log_level + - name: directory_path + value: value_of_directory_path + - name: window_size + value: value_of_window_size + - name: step_size + value: value_of_step_size + restartPolicy: OnFailure + imagePullSecrets: + - name: image_pull_secret \ No newline at end of file diff --git a/component-library/input/input-xView-clipping.yaml b/component-library/input/input-xView-clipping.yaml new file mode 100644 index 00000000..0b560889 --- /dev/null +++ b/component-library/input/input-xView-clipping.yaml @@ -0,0 +1,25 @@ +name: input-xview-clipping +description: "## Xview Dataset clipping – CLAIMED V0.1" + +inputs: +- {name: log_level, type: String, description: "update log level", default: "INFO"} +- {name: directory_path, type: String, description: "directory_path is the path to the folder with the unzipped .tif images from xview dataset"} +- {name: window_size, type: String, description: "Each image is cropped using a square window with edge_length window_size which has to be given in number of pixels"} +- {name: step_size, type: String, description: "For tumbling window step_size must equal window_size"} + + +outputs: + + +implementation: + container: + image: docker.io/mdorzweiler/claimed-input-xview-clipping:0.1 + command: + - sh + - -ec + - | + ipython ./claimed_input-xView-clipping.ipynb log_level="${0}" directory_path="${1}" window_size="${2}" step_size="${3}" + - {inputValue: log_level} + - {inputValue: directory_path} + - {inputValue: window_size} + - {inputValue: step_size}