{"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":"Code is adapted https://www.geeksforgeeks.org/how-to-draw-bounding-boxes-on-an-image-in-pytorch/","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torchvision.io import read_image\nfrom torchvision.utils import draw_bounding_boxes\nfrom torchvision import transforms\nimport numpy as np\nimport IPython.display\nimport pandas as pd\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:23:08.721424Z","iopub.execute_input":"2022-08-01T12:23:08.72189Z","iopub.status.idle":"2022-08-01T12:23:09.745696Z","shell.execute_reply.started":"2022-08-01T12:23:08.721801Z","shell.execute_reply":"2022-08-01T12:23:09.74454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import detections.csv from main file\ndetections = pd.read_csv('../input/detections/detections.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:16.720358Z","iopub.execute_input":"2022-08-01T12:31:16.720768Z","iopub.status.idle":"2022-08-01T12:31:16.738596Z","shell.execute_reply.started":"2022-08-01T12:31:16.720735Z","shell.execute_reply":"2022-08-01T12:31:16.73759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choosing some image ids to create bounding boxes for\nimage_ids = ['f55460fccf2d3c591f57f9c0de2c37c2', '22b8e616a61bbc4caaed0cf23b7159df', 'e6df9b3fba3fb8745fe06fe9fb84ecfe']\nimage_paths = ['../input/vinbigdata-chest-xray-original-png/train/f55460fccf2d3c591f57f9c0de2c37c2.png',\n              '../input/vinbigdata-chest-xray-original-png/train/22b8e616a61bbc4caaed0cf23b7159df.png',\n              '../input/vinbigdata-chest-xray-original-png/train/e6df9b3fba3fb8745fe06fe9fb84ecfe.png']","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:19.701046Z","iopub.execute_input":"2022-08-01T12:31:19.701511Z","iopub.status.idle":"2022-08-01T12:31:19.706991Z","shell.execute_reply.started":"2022-08-01T12:31:19.701473Z","shell.execute_reply":"2022-08-01T12:31:19.705992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read input image\nimg = read_image('../input/vinbigdata-chest-xray-original-png/train/e6df9b3fba3fb8745fe06fe9fb84ecfe.png')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:22.179173Z","iopub.execute_input":"2022-08-01T12:31:22.18009Z","iopub.status.idle":"2022-08-01T12:31:22.293295Z","shell.execute_reply.started":"2022-08-01T12:31:22.180054Z","shell.execute_reply":"2022-08-01T12:31:22.292372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_min = np.array(detections[detections['image_id'] == image_ids[1]]['x_min'])\ny_min = np.array(detections[detections['image_id'] == image_ids[1]]['y_min'])\nx_max = np.array(detections[detections['image_id'] == image_ids[1]]['x_max'])\ny_max = np.array(detections[detections['image_id'] == image_ids[1]]['y_max'])\nlabels = np.array(detections[detections['image_id'] == image_ids[1]]['class_name'])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:24.368338Z","iopub.execute_input":"2022-08-01T12:31:24.368772Z","iopub.status.idle":"2022-08-01T12:31:24.383009Z","shell.execute_reply.started":"2022-08-01T12:31:24.368724Z","shell.execute_reply":"2022-08-01T12:31:24.381857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bounding box in (xmin, ymin, xmax, ymax) format\n# top-left point=(xmin, ymin), bottom-right point = (xmax, ymax)\n\nbbox = []\nclass_name = []\n\nfor i in range(len(x_min)): \n    bbox.append([x_min[i], y_min[i], x_max[i], y_max[i]])\n    class_name.append(labels[i])\n    \nbbox = torch.tensor(bbox, dtype=torch.int)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:27.020269Z","iopub.execute_input":"2022-08-01T12:31:27.020703Z","iopub.status.idle":"2022-08-01T12:31:27.02718Z","shell.execute_reply.started":"2022-08-01T12:31:27.020669Z","shell.execute_reply":"2022-08-01T12:31:27.026441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# draw bounding box on the input image\nimg = draw_bounding_boxes(img, bbox, width=14, labels= class_name, colors=(255,255,0), font = '../input/synth-indic-custom-resources/SYNTH_INDIC/fonts/english/English.ttf', font_size = 80)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:29.499622Z","iopub.execute_input":"2022-08-01T12:31:29.500193Z","iopub.status.idle":"2022-08-01T12:31:29.78734Z","shell.execute_reply.started":"2022-08-01T12:31:29.500158Z","shell.execute_reply":"2022-08-01T12:31:29.786355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prints a sample image\nnewsize = (300, 300)\nimg = img.resize(newsize)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:31:41.105679Z","iopub.execute_input":"2022-08-01T12:31:41.106097Z","iopub.status.idle":"2022-08-01T12:31:41.265879Z","shell.execute_reply.started":"2022-08-01T12:31:41.106066Z","shell.execute_reply":"2022-08-01T12:31:41.264561Z"},"trusted":true},"execution_count":null,"outputs":[]}]}