{"cells":[{"metadata":{},"cell_type":"markdown","source":"**Version 3**<br>\nAdded center points to the bounding box image.\n\n<hr>"},{"metadata":{},"cell_type":"markdown","source":"### A tool to quickly display the following:\n\n- Image\n- Bounding Boxes\n- Image + Bounding Boxes\n- Masks\n- Image + Masks"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\nimport cv2\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":false,"trusted":true},"cell_type":"code","source":"# Load the train data\ndf_train = pd.read_csv('../input/train.csv')\n\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> ### To view a train image please enter the image_id and then run all cells in this kernel"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Enter train image_id (without file extension)\n# ..........................\n\nimage_id = 'brsk004-029'\n\n# ..........................\n\n# Note:\n# If the image does not have any Kuzushiji characters then there will be an error message immediately after the next cell.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# Set the file name\nfname = image_id + '.jpg'\n\n\nfrom matplotlib.pyplot import figure\nfigure(figsize=(20,20)) \n\npath = '../input/train_images/' + fname\nimage = plt.imread(path)\nplt.imshow(image)\nplt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# Get the labels for the image\ndf = df_train[df_train['image_id'] == image_id]\ndf = df.reset_index(drop=True)\nlabel = df.loc[0, 'labels']\n\n\n# Set plot_type = 'mask' to see a mask instead of a bounding box.\n#plot_type = 'mask'\nplot_type = 'bbox'\n\n# blank mask\nmask = np.zeros((image.shape[0], image.shape[1], 1))\n\ntarget_list = label.split(' ')\n\nfor i, item in enumerate(target_list):\n    if item[0] == 'U':\n        \n        # coordinates of the bbox\n        x = int(target_list[i+1])\n        y = int(target_list[i+2])\n        w = int(target_list[i+3])\n        h = int(target_list[i+4])\n        \n        \n        # =========================================\n        # Imagine that this is a crop inside a crop.\n        # i.e. two boxes, one within another.\n        # =========================================\n        \n        # set the line thickness\n        stroke = 4\n\n        # larger mask\n        # Set plot_type = 'mask' to see a mask instead of a bounding box.\n        mask[y:y+h, x:x+w] = 1\n        \n        if plot_type == 'bbox':\n            \n            # smaller mask inside larger mask\n            y1=y + stroke\n            x1=x + stroke\n            h1 = h - (stroke*2)\n            w1 = w - (stroke*2)\n            mask[y1:y1+h1, x1:x1+w1] = 0\n            \n            \n            \n            # view the center point (top left corner is the center)\n            center_x = int(x + (w/2))\n            center_y = int(y + (h/2))\n            w_center = 5\n            h_center = 5\n            mask[center_y:center_y+h_center, center_x:center_x+w_center] = 1\n            \n          \n        \n\n# Plot the bounding boxes\nfigure(figsize=(20,20))\nmask = mask[:,:,0]\n\nplt.imshow(mask)\nplt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure(figsize=(20,20))\n\nplt.imshow(image, cmap='Greys')\nplt.imshow(mask, cmap='Reds', alpha=0.3)\nplt.axis('off')\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\n# Set plot_type = 'mask' to see a mask instead of a bounding box.\nplot_type = 'mask'\n#plot_type = 'bbox'\n\n# blank mask\nmask = np.zeros((image.shape[0], image.shape[1], 1))\n\ntarget_list = label.split(' ')\n\nfor i, item in enumerate(target_list):\n    if item[0] == 'U':\n        \n        # coordinates of the bbox\n        x = int(target_list[i+1])\n        y = int(target_list[i+2])\n        w = int(target_list[i+3])\n        h = int(target_list[i+4])\n        \n        # =========================================\n        # Imagine that this is a crop inside a crop.\n        # i.e. two boxes, one within another.\n        # =========================================\n        \n        # set the line thickness\n        stroke = 4\n\n        # larger mask\n        # Set plot_type = 'mask' to see a mask instead of a bounding box.\n        mask[y:y+h, x:x+w] = 1\n        \n        if plot_type == 'bbox':\n            # smaller mask inside larger mask\n            y1=y + stroke\n            x1=x + stroke\n            h1 = h - (stroke*2)\n            w1 = w - (stroke*2)\n            mask[y1:y1+h1, x1:x1+w1] = 0\n                \n\n# Plot the bounding boxes\nfigure(figsize=(20,20))\nmask = mask[:,:,0]\n\nplt.imshow(mask)\nplt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure(figsize=(20,20))\n\nplt.imshow(image, cmap='Greys')\nplt.imshow(mask, cmap='Reds', alpha=0.3)\nplt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}