{"cells":[{"metadata":{},"cell_type":"raw","source":"First, we need to install the japanese fonts to show the anotation of each characters. There exists library to show japanese fonts with following package (https://github.com/uehara1414/japanize-matplotlib)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\n# -*- coding: utf-8 -*-\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport matplotlib.patches as mpatches\nimport seaborn as sns\nimport pandas as pd\nimport numpy as np\nimport japanize_matplotlib\nimport os\n\ntrain_file = pd.read_csv(r\"../input/kuzushiji-recognition/train.csv\")\n\nunicode_chart = pd.read_csv(r\"../input/kuzushiji-recognition/unicode_translation.csv\")\ntrain_file.labels = train_file.labels.str.split(' ')\ntrain_file.labels.head(3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#changed the train label dimension\nlabel_np = np.array(train_file.labels[0])\nlabel_np = label_np.reshape(int(len(label_np)/5), 5)\nlabel_list = np.array([])\nfor i in label_np[:, 0]:\n    label_list= np.append(label_list, unicode_chart.char[unicode_chart.Unicode == i])\n#label_list = pd.DataFrame(label_list, columns=[\"chart_index\", \"char\"])\nprint(label_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for k in range(5):\n    label_np = np.array(train_file.labels[k])\n    # labeling skips when no characters in the image where the array value become NaN.\n    if pd.isnull(label_np).all() == True:\n        continue\n    label_np = label_np.reshape(int(len(label_np)/5), 5)\n    label_list = np.array([])\n    for i in label_np[:, 0]:\n        label_list= np.append(label_list, unicode_chart.char[unicode_chart.Unicode == i])\n    \n    image = cv2.imread(os.path.join(r\"../input/kuzushiji-recognition/train_images\", train_file.image_id[k] + \".jpg\"))\n    img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)   \n    height, width, channels = img.shape\n\n    fig =plt.figure(figsize=(width/200, height/200))\n    ax = fig.add_subplot(1,1,1)\n    j = 0\n    for i in label_np:\n        rect = mpatches.Rectangle((int(i[1]), int(i[2])), int(i[3]), int(i[4]), fill=False, edgecolor='red', linewidth=1)\n        ax.add_patch(rect)\n\n\n        ax.text((int(i[1]) + int(i[3]))/width, 1 - int(i[2])/height, label_list[j],\n                horizontalalignment='left',fontsize=20,\n                verticalalignment='center',\n                rotation='horizontal',\n                transform=ax.transAxes)\n        j = j+1\n    ax.imshow(img)\n    #plt.savefig(os.path.join(\"./anotated\", train_file.image_id[k]+\"_anotated.jpg\"))\n    plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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":1}