{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom pathlib import Path \nimport PIL.Image as Image \nimport matplotlib.pyplot as plt\n\nfile_path = '/kaggle/input/vesuvius-challenge-ink-detection/test'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-24T15:43:13.694318Z","iopub.execute_input":"2023-05-24T15:43:13.695058Z","iopub.status.idle":"2023-05-24T15:43:13.733433Z","shell.execute_reply.started":"2023-05-24T15:43:13.695011Z","shell.execute_reply":"2023-05-24T15:43:13.732307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle(img):\n    pixels = img.flatten()\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] = runs[1::2] - runs[:-1:2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:43:13.735433Z","iopub.execute_input":"2023-05-24T15:43:13.735886Z","iopub.status.idle":"2023-05-24T15:43:13.743140Z","shell.execute_reply.started":"2023-05-24T15:43:13.735856Z","shell.execute_reply":"2023-05-24T15:43:13.741816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is detecting the locations of test fragments.\nbase_path = Path(\"/kaggle/input/vesuvius-challenge-ink-detection/test/\")\n#test_fragments = sorted([fragment_name for fragment_name in base_path.iterdir()])\ntest_fragments = [fragment_name for fragment_name in base_path.iterdir()]\n\n# This creates the output dictionary.\nsubmission_dict = {'ID': [], 'Predicted': []}","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:43:13.744961Z","iopub.execute_input":"2023-05-24T15:43:13.745506Z","iopub.status.idle":"2023-05-24T15:43:13.761610Z","shell.execute_reply.started":"2023-05-24T15:43:13.745465Z","shell.execute_reply":"2023-05-24T15:43:13.760405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for PREFIX in test_fragments:\n    fragment_name = PREFIX.name\n    PREFIX = str(PREFIX)+ '/'\n    print(PREFIX)\n    mask = np.array(Image.open(PREFIX+\"mask.png\").convert('1'))\n\n    mask[:1000,:] = 0\n    mask[2000:,:] = 0\n\n    fig, (ax1) = plt.subplots(1, 1)\n    ax1.imshow(mask, cmap='gray')\n    plt.show()\n    \n    # This is converting the output into an RLE:\n    rle_output = rle(mask)\n    submission_dict['ID'].append(fragment_name)\n    submission_dict['Predicted'].append(rle_output)\n\nsubmission = pd.DataFrame(data=submission_dict)\nsubmission.to_csv('submission.csv', index=False)\nprint(submission)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:43:13.764145Z","iopub.execute_input":"2023-05-24T15:43:13.765104Z","iopub.status.idle":"2023-05-24T15:43:17.948838Z","shell.execute_reply.started":"2023-05-24T15:43:13.765059Z","shell.execute_reply":"2023-05-24T15:43:17.947779Z"},"trusted":true},"execution_count":null,"outputs":[]}]}