{"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":"markdown","source":"# Create annotated images (for us to manually see)","metadata":{}},{"cell_type":"code","source":"img_folder = \"../input/hubmap-organ-segmentation/train_images/\"\nann_folder = \"../input/hubmap-organ-segmentation/train_annotations/\"\ntab_file = \"../input/hubmap-organ-segmentation/train.csv\"\ndst_folder = \"annotated_images/\"\n\nJPEG_QUALITY = 50","metadata":{"_uuid":"d536fc3f-188c-4f28-adfd-4e2f42612494","_cell_guid":"dbe2fbfa-ee50-4bf2-b525-50409df37348","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-18T06:47:01.971308Z","iopub.execute_input":"2022-08-18T06:47:01.973476Z","iopub.status.idle":"2022-08-18T06:47:01.979010Z","shell.execute_reply.started":"2022-08-18T06:47:01.973427Z","shell.execute_reply":"2022-08-18T06:47:01.977876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-08-18T06:47:01.981631Z","iopub.execute_input":"2022-08-18T06:47:01.982401Z","iopub.status.idle":"2022-08-18T06:47:02.004997Z","shell.execute_reply.started":"2022-08-18T06:47:01.982363Z","shell.execute_reply":"2022-08-18T06:47:02.004086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def annotate_and_compress_image(img_file,ann_file,dst_file,quality):\n    imga = cv2.imread(img_file)\n\n    f = open(ann_file)\n    l = eval(f.read())\n    f.close\n\n    for m in l:\n        first_point = True\n        for [x,y] in m:\n            if first_point:\n                first_point = False                \n            else:\n                cv2.line(imga,\n                        pt1=(x0, y0),\n                        pt2=(x, y),\n                        color=(0, 0, 255),\n                        thickness=3,\n                        lineType=cv2.LINE_4,\n                        shift=0)                \n            [x0,y0] = [x,y]\n    cv2.imwrite(dst_file,imga,[cv2.IMWRITE_JPEG_QUALITY, quality])","metadata":{"_uuid":"dcd704ca-855a-4900-9f99-846382bb939a","_cell_guid":"06907415-43df-4fb5-9596-45eb80ca7c07","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-18T06:47:02.007209Z","iopub.execute_input":"2022-08-18T06:47:02.008119Z","iopub.status.idle":"2022-08-18T06:47:02.017532Z","shell.execute_reply.started":"2022-08-18T06:47:02.008070Z","shell.execute_reply":"2022-08-18T06:47:02.016751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tab = pd.read_csv(tab_file)\norgans = tab.organ.unique()\n\ntry:\n    os.mkdir(dst_folder)\nexcept FileExistsError:\n    pass\n\nfor o in organs :\n    try:\n        os.mkdir(dst_folder + o)\n    except FileExistsError:\n        pass\n\ntiff_files = os.listdir(img_folder)\n\nfor tif in tiff_files:\n    [idnum,ext] = tif.split('.')\n    if ext != \"tiff\":\n        tiff_files.remove(tif)\n\nfor tif in tqdm(tiff_files) :\n    [idnum,ext] = tif.split('.')\n    organ = tab[tab.id==int(idnum)][\"organ\"].values[0]\n    sex = tab[tab.id==int(idnum)][\"sex\"].values[0]\n    age = tab[tab.id==int(idnum)][\"age\"].values[0]\n    src_fname = img_folder + tif\n    ann_fname = ann_folder + idnum + '.json'\n    dst_fname = dst_folder + organ + '/' + idnum + '_' + sex[0] + '_' + '%d'%age + '.jpg'\n    annotate_and_compress_image(src_fname,ann_fname,dst_fname,JPEG_QUALITY)\n","metadata":{"_uuid":"ff32c3fa-c2a2-458c-b9f0-6c82ef371c63","_cell_guid":"85e1083e-f0b2-4fdb-ab0a-2179483ca7bc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-18T06:47:02.020141Z","iopub.execute_input":"2022-08-18T06:47:02.020982Z","iopub.status.idle":"2022-08-18T06:47:09.930879Z","shell.execute_reply.started":"2022-08-18T06:47:02.020919Z","shell.execute_reply":"2022-08-18T06:47:09.929319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}