{"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":true,"execution":{"iopub.status.busy":"2023-11-28T07:25:52.040331Z","iopub.execute_input":"2023-11-28T07:25:52.041179Z","iopub.status.idle":"2023-11-28T07:27:04.864815Z","shell.execute_reply.started":"2023-11-28T07:25:52.0411Z","shell.execute_reply":"2023-11-28T07:27:04.863652Z"},"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":"2023-11-28T07:27:04.866522Z","iopub.execute_input":"2023-11-28T07:27:04.867038Z","iopub.status.idle":"2023-11-28T07:27:05.5976Z","shell.execute_reply.started":"2023-11-28T07:27:04.866984Z","shell.execute_reply":"2023-11-28T07:27:05.596193Z"},"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":"2023-11-28T07:27:05.600281Z","iopub.execute_input":"2023-11-28T07:27:05.600728Z","iopub.status.idle":"2023-11-28T07:27:05.666543Z","shell.execute_reply.started":"2023-11-28T07:27:05.60069Z","shell.execute_reply":"2023-11-28T07:27:05.665296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(mask_files)","metadata":{"execution":{"iopub.status.busy":"2023-11-28T07:27:05.668307Z","iopub.execute_input":"2023-11-28T07:27:05.668706Z","iopub.status.idle":"2023-11-28T07:27:05.673584Z","shell.execute_reply.started":"2023-11-28T07:27:05.668675Z","shell.execute_reply":"2023-11-28T07:27:05.672452Z"},"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":"2023-11-28T07:27:05.674936Z","iopub.execute_input":"2023-11-28T07:27:05.675839Z","iopub.status.idle":"2023-11-28T07:27:05.685651Z","shell.execute_reply.started":"2023-11-28T07:27:05.675803Z","shell.execute_reply":"2023-11-28T07:27:05.684385Z"},"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    # 转成Numpy数组\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    blended_image = Image.blend(thumbnail_img, mask_image, alpha=0.3)\n    return blended_image","metadata":{"execution":{"iopub.status.busy":"2023-11-28T07:27:05.687526Z","iopub.execute_input":"2023-11-28T07:27:05.687981Z","iopub.status.idle":"2023-11-28T07:27:05.698727Z","shell.execute_reply.started":"2023-11-28T07:27:05.687947Z","shell.execute_reply":"2023-11-28T07:27:05.697448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from concurrent.futures import ThreadPoolExecutor\nN = 30\n\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":"2023-11-28T07:27:05.700888Z","iopub.execute_input":"2023-11-28T07:27:05.70147Z","iopub.status.idle":"2023-11-28T07:32:19.226704Z","shell.execute_reply.started":"2023-11-28T07:27:05.701364Z","shell.execute_reply":"2023-11-28T07:32:19.225478Z"},"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":"2023-11-28T07:32:19.233875Z","iopub.execute_input":"2023-11-28T07:32:19.234271Z"},"trusted":true},"execution_count":null,"outputs":[]}]}