{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Kaggle.Kernels have some problems with widgets in the view mode, so you can't see the interactive widget untill you'll fork the notebook.\n## Let's hope the kaggle team will fix it soon (or I'll find a solution)))\n\nHello, I'm doing some EDA, so I explored very curious ways other participants check raw data, with grids etc.Nevertheless, I made utility for this task, thanks very much to https://www.kaggle.com/aleksandradeis/globalwheatdetection-eda for baseline)\nIts advantage is in space economy, so you can see a lot of images without mud in your code\n\nIf notebook it'll earn some upvotes, I'll post more utils","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom ipywidgets import interact, IntSlider, fixed\nfrom pathlib import Path\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_path = Path('../input/global-wheat-detection/')\nTRAIN_CSV_PATH = base_path / 'train.csv'\nTRAIN_DIR = base_path / 'train/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## utility:","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_all_bboxes(df, img_id):\n    image_bboxes = df[df.image_id == img_id]      \n    return image_bboxes[[\"x\", \"y\", \"w\", \"h\"]]\n\ndef plot_image(df, img_id, train_dir=TRAIN_DIR): # using constant, be careful\n    plt.figure(figsize=(9, 9))\n    ax = plt.gca()\n    \n    img = Image.open(str(train_dir / (img_id + '.jpg')))\n    ax.imshow(img)\n    \n    #plot bboxes on image\n    bboxes = get_all_bboxes(df, img_id)\n    for bbox in bboxes.iterrows():\n        bbox = bbox[1] # 0 element is index\n        coords = ((bbox.x, bbox.y), bbox.w, bbox.h)\n        rect = patches.Rectangle(*coords, linewidth=1, edgecolor='r',facecolor='none')\n        ax.add_patch(rect)\n\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def interactive_plot(df):\n    uniq = df[\"image_id\"].unique()\n    @interact(df=fixed(df), id_num=IntSlider(max=len(uniq)-1))\n    def choose_show(df, id_num):\n        img_id = uniq[id_num]\n        plot_image(df, img_id)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"primary preprocessing function for demonstrating of an example (you can open code)","execution_count":null},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def bbox2separate_cols(df):\n    \"\"\"\n    If df contains bbox column of 'O' dtype, function creates columns \"x\", \"y\", \"w\", \"h\". \n    Otherwise throws ValueError.\n    \"\"\"\n    try:\n        bbox_items = df[\"bbox\"].str.split(',', expand=True)\n    except KeyError:\n        raise ValueError(\"df has no bbox col\")\n        \n    # convert splitted strings to numbers\n    bbox_items[0] = bbox_items[0].transform(lambda x: x[1:]) #delete '[' and ']' symbols\n    bbox_items[3] = bbox_items[3].transform(lambda x: x[:-1])\n    bbox_items = bbox_items.astype(float)\n    \n    train[['x', 'y', 'w', 'h']] = bbox_items\n    train.drop(columns=['bbox'], inplace=True)\n    \n    return train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## example of use","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(TRAIN_CSV_PATH)\ntrain = bbox2separate_cols(train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### So, if you view this notebook in commit (not edit) mode, you probably can't see the widget that allows you to scroll through image ids and check all images. You still can copy notebook and use utility in your project)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"interactive_plot(train)","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":4}