{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import seaborn as sns\n\nimport json\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport cv2\nimport os\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\npath = \"../input/iwildcam2021-fgvc8/\"\nwith 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)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test_image = pd.json_normalize(test_data['images'])\ndf_test_image.set_index('id',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_categories = pd.json_normalize(train_data['categories'])\ndf_categories.set_index('id',inplace=True)\ndf_categories.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_annotation = pd.json_normalize(train_data['annotations'])\ndf_train_annotation.set_index('id',inplace=True)\ndf_train_annotation['category_name'] = df_train_annotation['category_id'].apply(lambda x: df_categories['name'][x] if x in df_categories.index else 'Unkown' )\ndf_train_annotation.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_image_data = pd.json_normalize(train_data['images'])\ndf_train_image_data.set_index('id',inplace=True)\ndf_train_image_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_merge = pd.merge(df_train_image_data, df_train_annotation, left_on='id', right_on='image_id')\ndf_train_merge.set_index('image_id',inplace=True)\ndf_train_merge.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_image_data = pd.json_normalize(results_data['images'])\nresult_image_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.merge(df_train_merge, result_image_data, left_on='image_id', right_on='id')\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nsns.set_theme(style=\"darkgrid\")\nsns.set(rc={'figure.figsize':(20,5)})\nax = sns.countplot(x=\"category_id\", data=df_train)\nax.set_xticklabels(ax.get_xticklabels(), rotation=45)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_examples():\n    fig, axs = plt.subplots(5, 4, figsize=(20, 20))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    \n    axs = axs.ravel()\n    for i in range(20):\n        img = cv2.imread(path+'train/'+df_train['file_name'][i])\n        axs[i].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        if len(df_train['detections'][i]) > 0:\n            \n            width = df_train['width'][i]\n            height = df_train['height'][i]\n            for dets in range(len(df_train['detections'][i])):\n                bbox = df_train['detections'][i][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 '+str(df_train['category_name'][i]))\n        else:\n            axs[i].set_title('no detections')\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_examples()","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}