{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Plotting Data\n### This brief notebook simply plots many xray images with and without their bounding boxes. It is intended for understanding what the data looks like we are working with."},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = '../input/vinbigdata-competition-jpg-data-4x-downsampled/train_downsampled.csv'\nIMG_ROOT = '../input/vinbigdata-competition-jpg-data-4x-downsampled/train/train/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv(PATH, delimiter=',')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_grid(data['image_id'][:25], IMG_ROOT)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# print the annotations from a single xray image\ndisease_data[disease_data['image_id'] == filenames[10]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot a grid of xray images, each with all its bounding boxes\ndef plot_annotated_grid(img_names, img_root, all_annotations, rows=5, cols=5):\n    fig = plt.figure(figsize=(16,16))\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        annotate_image(img, name, all_annotations)\n        plot_image(img)\n        \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# filter out healthy samples\ndisease_data = data[data['class_id'] != 14]\nfilenames = list(set(disease_data['image_id'].values.tolist()))\n\n# Plot 25 xray images with their bounding boxes\nplot_annotated_grid(filenames[:25], IMG_ROOT, data, rows=5, cols=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# filter out healthy samples\ndisease_data = data[data['class_id'] != 14]\nfilenames = list(set(disease_data['image_id'].values.tolist()))\n\n# Plot 25 xray images with their bounding boxes\nplot_annotated_grid(filenames[25:50], IMG_ROOT, data, rows=5, cols=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}