{"metadata": {"language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "file_extension": ".py", "version": "3.6.1", "name": "python", "pygments_lexer": "ipython3", "mimetype": "text/x-python"}, "kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}}, "nbformat_minor": 1, "cells": [{"metadata": {"_uuid": "d32c1acf83b15d885879cf12d5d60e548aad43ec", "_cell_guid": "5e864f79-12e5-424b-b57d-a4ed3101c2ed"}, "cell_type": "markdown", "source": ["This script will create one folder per category and save all respective images on it.\n", "\n", "The pattern to each file is  `../input/train/[category]/[_id]-[index].jpg`, where `index` is the position of image on each product."]}, {"metadata": {"_uuid": "99ace6c238acb9258ee3973c633375e92f734a52", "collapsed": true, "_cell_guid": "f491f022-2cb1-4d73-a853-771cc294b1d6"}, "outputs": [], "cell_type": "code", "source": ["import bson\n", "import numpy as np\n", "import pandas as pd\n", "import os\n", "from tqdm import tqdm_notebook"], "execution_count": 1}, {"metadata": {"_uuid": "1d972da796176fecb151d1b2a441d50243eed639", "_kg_hide-output": true, "_cell_guid": "39c2a23b-202e-43a8-af31-203816665be8"}, "outputs": [], "cell_type": "code", "source": ["out_folder = '../output/train'\n", "\n", "# Create output folder\n", "if not os.path.exists(out_folder):\n", "    os.makedirs(out_folder)"], "execution_count": 2}, {"metadata": {"_uuid": "b67ebdba8b1e818d8c21757a139e333807440cbe", "_kg_hide-input": false, "_kg_hide-output": true, "_cell_guid": "9855c244-3e1a-453a-891b-01d207896acc"}, "outputs": [], "cell_type": "code", "source": ["# Create categories folders\n", "categories = pd.read_csv('../input/category_names.csv', index_col='category_id')\n", "\n", "for category in tqdm_notebook(categories.index):\n", "    os.mkdir(os.path.join(out_folder, str(category)))"], "execution_count": 3}, {"metadata": {"_uuid": "ea65d6cb7fea91c1c921a2f2b21f596778143bfb", "_kg_hide-output": true, "_cell_guid": "50438f3c-e2eb-4add-bfc2-c32cfc1c4682"}, "outputs": [], "cell_type": "code", "source": ["num_products = 7069896  # 7069896 for train and 1768182 for test\n", "\n", "bar = tqdm_notebook(total=num_products)\n", "with open('../input/train.bson', 'rb') as fbson:\n", "\n", "    data = bson.decode_file_iter(fbson)\n", "    \n", "    for c, d in enumerate(data):\n", "        category = d['category_id']\n", "        _id = d['_id']\n", "        for e, pic in enumerate(d['imgs']):\n", "            fname = os.path.join(out_folder, str(category), '{}-{}.jpg'.format(_id, e))\n", "            with open(fname, 'wb') as f:\n", "                f.write(pic['picture'])\n", "\n", "        bar.update()"], "execution_count": 4}, {"metadata": {"_uuid": "bf1962a30297cec98152eda11004c69ad0b7bba9", "collapsed": true, "_cell_guid": "8f0bb98e-965b-465c-b49b-aa6ea2001324"}, "outputs": [], "cell_type": "code", "source": [], "execution_count": null}], "nbformat": 4}