{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\n%matplotlib inline\n\nPATH = '../input/vinbigdata-chest-xray-abnormalities-detection/train.csv'\nIMG_ROOT = '../input/vinbigdata-chest-xray-abnormalities-detection/train'\n\ndata = pd.read_csv(PATH, delimiter=',')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:17:14.557194Z","iopub.execute_input":"2022-11-16T19:17:14.559046Z","iopub.status.idle":"2022-11-16T19:17:14.809402Z","shell.execute_reply.started":"2022-11-16T19:17:14.558976Z","shell.execute_reply":"2022-11-16T19:17:14.807872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get image as numpy array\ndef load_image(name, path):\n    img_path = path + name + '.jpg'\n    img = cv2.imread(img_path)\n    return img\n\n# Plot numpy array\ndef plot_image(img):\n    plt.imshow(img)\n    plt.title(img.shape)\n    \n# Plot a grid of examples\ndef plot_grid(img_names, img_root, rows=5, cols=5):\n    fig = plt.figure(figsize=(25,25))\n    \n    for i,name in enumerate(img_names):\n        fig.add_subplot(rows,cols,i+1)\n        img = load_image(name, img_root)\n        plot_image(img)\n        \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:17:51.262548Z","iopub.execute_input":"2022-11-16T19:17:51.263054Z","iopub.status.idle":"2022-11-16T19:17:51.271872Z","shell.execute_reply.started":"2022-11-16T19:17:51.263015Z","shell.execute_reply":"2022-11-16T19:17:51.270848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_grid(data['image_id'][:25], IMG_ROOT)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:17:53.637103Z","iopub.execute_input":"2022-11-16T19:17:53.637583Z","iopub.status.idle":"2022-11-16T19:17:53.945595Z","shell.execute_reply.started":"2022-11-16T19:17:53.637546Z","shell.execute_reply":"2022-11-16T19:17:53.943202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filter out healthy samples\ndisease_data = data[data['class_id'] != 14]\n# get unique filenames\nfilenames = list(set(disease_data['image_id'].values.tolist()))\n# print a few filenames\nprint(filenames[:10])","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:19:29.769784Z","iopub.execute_input":"2022-11-16T19:19:29.77037Z","iopub.status.idle":"2022-11-16T19:19:29.790314Z","shell.execute_reply.started":"2022-11-16T19:19:29.770328Z","shell.execute_reply":"2022-11-16T19:19:29.788867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disease_data[disease_data['image_id'] == filenames[10]]","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:19:38.607835Z","iopub.execute_input":"2022-11-16T19:19:38.608606Z","iopub.status.idle":"2022-11-16T19:19:38.643128Z","shell.execute_reply.started":"2022-11-16T19:19:38.608543Z","shell.execute_reply":"2022-11-16T19:19:38.641565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def class_to_color(class_id):\n    colors = [(255,0,0),(0,255,0),(0,0,255),(255,255,0),(255,0,255),(0,255,255),(255,100,100),\n              (100,255,100),(100,100,255),(255,100,0),(255,0,100),(100,0,255),(100,100,255),(100,255,0),\n              (100,255,100)]\n    return colors[class_id]\n\n# draw a single bounding box onto a numpy array image\ndef draw_bounding_box(img, annotation):\n    if annotation.isnull().values.any():\n        return\n    \n    x_min, y_min = int(annotation['x_min']), int(annotation['y_min'])\n    x_max, y_max = int(annotation['x_max']), int(annotation['y_max'])\n    \n    class_id = int(annotation['class_id'])\n    color = class_to_color(class_id)\n    \n    cv2.rectangle(img,(x_min,y_min),(x_max,y_max), color, 2)\n\n# draw all annotation bounding boxes on an image\ndef annotate_image(img, name, all_annotations):\n    annotations = all_annotations[all_annotations['image_id'] == name]\n    for index, row in annotations.iterrows():\n        draw_bounding_box(img, row)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:19:56.538782Z","iopub.execute_input":"2022-11-16T19:19:56.539331Z","iopub.status.idle":"2022-11-16T19:19:56.552484Z","shell.execute_reply.started":"2022-11-16T19:19:56.539286Z","shell.execute_reply":"2022-11-16T19:19:56.551077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:20:20.613923Z","iopub.execute_input":"2022-11-16T19:20:20.615116Z","iopub.status.idle":"2022-11-16T19:20:20.621637Z","shell.execute_reply.started":"2022-11-16T19:20:20.61507Z","shell.execute_reply":"2022-11-16T19:20:20.620411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot a single sample with all its bounding boxes\nname = 'b75bba1e9dfb84fe1bd84c88c638c339'\nimg = load_image(name, IMG_ROOT)\nannotate_image(img, name, data)\nplot_image(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T19:19:58.837652Z","iopub.execute_input":"2022-11-16T19:19:58.83814Z","iopub.status.idle":"2022-11-16T19:19:59.045163Z","shell.execute_reply.started":"2022-11-16T19:19:58.838105Z","shell.execute_reply":"2022-11-16T19:19:59.043417Z"},"trusted":true},"execution_count":null,"outputs":[]}]}