{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Intro\nWelcome to the [iWildcam 2021 - FGVC8](https://www.kaggle.com/c/iwildcam2021-fgvc8) compedition.\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/24911/logos/header.png)\n\nInformations about the data structure you will get on the [github](https://github.com/visipedia/iwildcam_comp/blob/master/readme.md) repository.\n\n<span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span>","metadata":{}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport cv2\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport json\nfrom collections import Counter","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/iwildcam2021-fgvc8/'\nos.listdir(path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(path+'metadata')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"with open(path+'metadata/'+'iwildcam2021_train_annotations.json') as f:\n    train_data = json.load(f)\nwith open(path+'metadata/'+'iwildcam2021_test_information.json') as f:\n    test_data = json.load(f)\nwith open(path+'metadata/'+'iwildcam2021_megadetector_results.json') as f:\n    results_data = json.load(f)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Write image metadata into dataframe:","metadata":{}},{"cell_type":"code","source":"train_image_data = pd.json_normalize(train_data['images'])\ntrain_image_data.index=train_image_data['id']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions\nWe define some helper functions.","metadata":{}},{"cell_type":"code","source":"def plot_examples():\n    fig, axs = plt.subplots(4, 4, figsize=(20, 20))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    \n    axs = axs.ravel()\n    for i in range(16):\n        img = cv2.imread(path+'train/'+results_data['images'][i]['id']+'.jpg')\n        axs[i].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        if len(results_data['images'][i]['detections']) > 0:\n            \n            width = train_image_data.loc[results_data['images'][i]['id'], 'width']\n            height = train_image_data.loc[results_data['images'][i]['id'], 'height']\n            for dets in range(len(results_data['images'][i]['detections'])):\n                bbox = results_data['images'][i]['detections'][dets]['bbox']            \n                p = matplotlib.patches.Rectangle((width*bbox[0], height*bbox[1]),\n                                              width*bbox[2],\n                                              height*bbox[3],\n                                              ec='r', fc='none', lw=2.)\n                axs[i].add_patch(p)\n                axs[i].set_title('category '+results_data['images'][i]['detections'][dets]['category'])\n        else:\n            axs[i].set_title('no detections')\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n    plt.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview","metadata":{}},{"cell_type":"code","source":"print('Number train images:', len(os.listdir(path+'train/')))\nprint('Number test images:', len(os.listdir(path+'test/')))","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"## Plot Image And Bounding Boxes","metadata":{}},{"cell_type":"code","source":"plot_examples()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution Of Categories","metadata":{}},{"cell_type":"code","source":"train_cate = []\nfor row in range(len(results_data['images'])):\n    if len(results_data['images'][row]['detections']) > 0:\n        for dets in range(len(results_data['images'][row]['detections'])):\n            train_cate.append(results_data['images'][row]['detections'][dets]['category'])\n    else:\n        train_cate.append('no_detection')\nlabel_dist = Counter(train_cate)\nlabel_dist","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}