{"metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "language_info": {"mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "file_extension": ".py", "version": "3.6.1", "codemirror_mode": {"name": "ipython", "version": 3}}}, "nbformat": 4, "nbformat_minor": 1, "cells": [{"cell_type": "markdown", "metadata": {"_uuid": "8044a9a2eba0d8eccf208a1b65a430619cf9e225", "_cell_guid": "87edae59-bba0-4ea1-bfe1-35f493d9e6ee"}, "source": ["#### This script walks you through how to read the bson data into pandas dataframe, and shows some of the images present in the data."]}, {"cell_type": "code", "metadata": {"_uuid": "ceddcac017b4cf8bd214ef07954e8efc7cb8513d", "collapsed": true, "_cell_guid": "71b84003-f514-4801-9c07-e251a4949a24"}, "execution_count": null, "outputs": [], "source": ["import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from PIL import Image\n", "import io, bson, multiprocessing\n", "from skimage.data import imread"]}, {"cell_type": "code", "metadata": {"_uuid": "3ccc12e0b1a61b5b86e898e11e72f18c60f01e66", "collapsed": true, "_cell_guid": "ea5323cc-2873-4bf1-92dc-c32cd2b39f9d"}, "execution_count": null, "outputs": [], "source": ["categories = pd.read_csv('../input/category_names.csv', index_col='category_id')"]}, {"cell_type": "code", "metadata": {"_uuid": "56ed2644d1fd5018a449383bc3cf674e5365cf10", "collapsed": true, "_cell_guid": "34d2ec7b-5761-4f03-88a0-d508b0ae6bd3"}, "execution_count": null, "outputs": [], "source": ["print(categories.describe(), \"\\n\\n\", \"==================================================================\")\n", "print(categories.head())"]}, {"cell_type": "markdown", "metadata": {"_uuid": "3223b05089c5995d3429a7cb02494e71fb92b5ff", "_cell_guid": "0c1ba893-af9e-439d-a8cb-47d5a06aeb0f"}, "source": ["### Thanks to this kernel: https://www.kaggle.com/sophieg/explore-dataset for the following code. Check her script out, it's really cool."]}, {"cell_type": "code", "metadata": {"_uuid": "e6aea50041e69b0c541f22f05afe36433ed0877c", "collapsed": true, "_cell_guid": "605a9061-cb08-44fc-b17d-08a4a11fe494"}, "execution_count": null, "outputs": [], "source": ["# read bson file into pandas DataFrame\n", "with open('../input/train_example.bson','rb') as b:\n", "    df = pd.DataFrame(bson.decode_all(b.read()))\n", "    \n", "# convert binary image to raw image and store in the imgs column\n", "df['imgs'] = df['imgs'].apply(lambda rec: rec[0]['picture'])\n", "df['imgs'] = df['imgs'].apply(lambda img: Image.open(io.BytesIO(img)))"]}, {"cell_type": "code", "metadata": {"_uuid": "64a69cfe943df94690b5c81eea48f28c469bc735", "collapsed": true, "_cell_guid": "0f39aec7-2d9f-471e-a2f5-85db4738e930"}, "execution_count": null, "outputs": [], "source": ["print(df.head())\n", "print(df.imgs.head())"]}, {"cell_type": "markdown", "metadata": {"_uuid": "fb0022433cd13caa8b8c9225cabc8c8cce7225d5", "_cell_guid": "b5b6180d-5091-4e24-9ef8-839e22ab628d"}, "source": ["#### A simple way to print the initial 16 images using matlab"]}, {"cell_type": "code", "metadata": {"_uuid": "acb59651a81e30271415e15f175040a8fb3360dc", "scrolled": true, "collapsed": true, "_cell_guid": "d99e0bb2-6ce1-44ec-aaa0-b066e92f08dd"}, "execution_count": null, "outputs": [], "source": ["for i in range(16):\n", "    plt.imshow(df.iloc[i,2])\n", "    plt.show()"]}, {"cell_type": "markdown", "metadata": {"_uuid": "650796322114dcbb3f64109d78e771f0aa98b6c3", "collapsed": true, "_cell_guid": "8fe585b8-394a-40cf-b93b-2947f050e4c7"}, "source": ["Please **upvote** if you liked the notebook. Although it is a small one, but I hope you took away something."]}]}