{"cells":[{"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\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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\n#from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten#, Dropout, Activation\nfrom keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow\nimport keras\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom matplotlib import patches\n#import opencv-python\nimport sklearn\nimport h5py","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# import data\ntrain_df = pd.read_csv(\"/kaggle/input/kuzushiji-recognition/train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reformat with just the first label \nnew_df = train_df.drop('labels', 1).join(train_df[\"labels\"].str.split(\" \", n=5, expand = True).drop(5,1)).fillna(0)\nnew_df.columns = ['image_id','label','xmin','ymin','width','height']\nnew_df['xmin'] = new_df['xmin'].astype(int)\nnew_df['ymin'] = new_df['ymin'].astype(int)\nnew_df['width'] = new_df['width'].astype(int)\nnew_df['height'] = new_df['height'].astype(int)\nnew_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# show training image and boxes\n\nfig = plt.figure()\n\n#add axes to the image\nax = fig.add_axes([0,0,1,1])\n\n# read and plot the image\ntop_image = plt.imread('/kaggle/input/kuzushiji-recognition/train_images/100241706_00004_2.jpg')\nplt.imshow(top_image)\n\n# iterating over the image for different objects\nfor _,row in new_df[new_df.image_id == \"100241706_00004_2\"].iterrows():\n    xmin = row.xmin\n    ymin = row.ymin\n    width = row.width\n    height = row.height\n    \n    # add bounding boxes to the image\n    rect = patches.Rectangle((xmin,ymin), width, height, edgecolor = 'r', facecolor = 'none')\n    ax.add_patch(rect)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.DataFrame()\ndata['format'] = train_df['image_id']\n\n# as the images are in train_images folder, add train_images before the image name\nfor i in range(data.shape[0]):\n    data['format'][i] = 'train_images/' + data['format'][i]\n\n# add xmin, ymin, xmax, ymax and class as per the format required\nfor i in range(data.shape[0]):\n    data['format'][i] = data['format'][i] + ',' + str(new_df['xmin'][i]) + ',' + str(new_df['ymin'][i]) + ',' + str(new_df['width'][i]) + ',' + str(new_df['height'][i]) + ',' + str(new_df['label'][i])\n\ndata.to_csv('annotate.txt', header=None, index=None, sep=' ')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","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}