{"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"},{"sourceId":6984590,"sourceType":"datasetVersion","datasetId":4014175}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!sudo apt-get update\n!sudo apt-get install -y libvips\n!pip install pyvips","metadata":{"scrolled":true,"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-04T01:40:22.686297Z","iopub.execute_input":"2024-01-04T01:40:22.686591Z","iopub.status.idle":"2024-01-04T01:41:02.455911Z","shell.execute_reply.started":"2024-01-04T01:40:22.686562Z","shell.execute_reply":"2024-01-04T01:41:02.454993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport random\nimport numpy as np\nfrom glob import glob\nfrom tqdm import tqdm\nfrom PIL import Image\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport pyvips\n\nimport os\nos.environ['VIPS_CONCURRENCY'] = '4'\nos.environ['VIPS_DISC_THRESHOLD'] = '15gb'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-04T01:41:02.457351Z","iopub.execute_input":"2024-01-04T01:41:02.457616Z","iopub.status.idle":"2024-01-04T01:41:02.944656Z","shell.execute_reply.started":"2024-01-04T01:41:02.457592Z","shell.execute_reply":"2024-01-04T01:41:02.944044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_files = glob('/kaggle/input/ubc-ovarian-cancer-competition-supplemental-masks/*.png')\nlen(mask_files)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T01:41:02.945554Z","iopub.execute_input":"2024-01-04T01:41:02.94588Z","iopub.status.idle":"2024-01-04T01:41:02.97995Z","shell.execute_reply.started":"2024-01-04T01:41:02.94586Z","shell.execute_reply":"2024-01-04T01:41:02.979351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(mask_files)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T01:41:02.981539Z","iopub.execute_input":"2024-01-04T01:41:02.98189Z","iopub.status.idle":"2024-01-04T01:41:02.985434Z","shell.execute_reply.started":"2024-01-04T01:41:02.981868Z","shell.execute_reply":"2024-01-04T01:41:02.984504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_mask2thumbnail_path(mask_path):\n    image_id = Path(mask_path).stem\n    return f'/kaggle/input/UBC-OCEAN/train_thumbnails/{image_id}_thumbnail.png'","metadata":{"execution":{"iopub.status.busy":"2024-01-04T01:41:02.987784Z","iopub.execute_input":"2024-01-04T01:41:02.988048Z","iopub.status.idle":"2024-01-04T01:41:02.996075Z","shell.execute_reply.started":"2024-01-04T01:41:02.988027Z","shell.execute_reply":"2024-01-04T01:41:02.995527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def blend_image(mask_path):\n    mask_image = pyvips.Image.new_from_file(mask_path, access='sequential')\n\n    thumbnail_path = convert_mask2thumbnail_path(mask_path)\n    thumbnail_img = Image.open(thumbnail_path)\n\n    scale = thumbnail_img.width / mask_image.width\n    resized_image = mask_image.resize(scale)\n\n    # Convert the PyVIPS image to a NumPy array\n    mask_image = np.ndarray(buffer=resized_image.write_to_memory(),\n                          dtype=np.uint8,\n                          shape=[thumbnail_img.height, thumbnail_img.width, 3])\n    mask_image = Image.fromarray(mask_image)\n    \n    # Blend images\n    blended_image = Image.blend(thumbnail_img, mask_image, alpha=0.3)\n    return blended_image","metadata":{"execution":{"iopub.status.busy":"2024-01-04T01:42:19.524407Z","iopub.execute_input":"2024-01-04T01:42:19.524739Z","iopub.status.idle":"2024-01-04T01:42:19.530158Z","shell.execute_reply.started":"2024-01-04T01:42:19.524713Z","shell.execute_reply":"2024-01-04T01:42:19.529473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from concurrent.futures import ThreadPoolExecutor\nN = 30\nwith ThreadPoolExecutor(max_workers=4) as executor:\n    blended_images = list(tqdm(executor.map(blend_image, mask_files[:N]), total=N))","metadata":{"execution":{"iopub.status.busy":"2024-01-04T01:42:23.292322Z","iopub.execute_input":"2024-01-04T01:42:23.292636Z","iopub.status.idle":"2024-01-04T01:42:33.124635Z","shell.execute_reply.started":"2024-01-04T01:42:23.292611Z","shell.execute_reply":"2024-01-04T01:42:33.123587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import display\nfor img in blended_images:\n    display(img)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T01:42:33.12609Z","iopub.execute_input":"2024-01-04T01:42:33.126361Z","iopub.status.idle":"2024-01-04T01:42:34.26893Z","shell.execute_reply.started":"2024-01-04T01:42:33.126338Z","shell.execute_reply":"2024-01-04T01:42:34.268314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}