Created
May 4, 2021 11:44
-
-
Save MachineLearningIsEasy/bb7850519246faeddd79a60957617840 to your computer and use it in GitHub Desktop.
COCO data visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Populating the interactive namespace from numpy and matplotlib\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"import json\n", | |
"import pandas as pd\n", | |
"import pprint\n", | |
"%pylab inline\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.image as mpimg\n", | |
"import matplotlib.patches as patches\n", | |
"plt.rcParams[\"figure.figsize\"] = (20,15)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Подгружаем аннотации" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"filenames_list = os.listdir('val2017')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"/Users/iccomplex/Desktop/JetsonNano/coco_xml/annotations/captions_val2017.json\", \"r\") as read_file:\n", | |
" caption_data = json.load(read_file)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"/Users/iccomplex/Desktop/JetsonNano/coco_xml/annotations/instances_val2017.json\", \"r\") as read_file:\n", | |
" annotation_data = json.load(read_file)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#caption_data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#annotation_data" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Смотрим на данные из COCO" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df_images_info = pd.DataFrame(annotation_data['images'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#df_images_info.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df_images_data = pd.DataFrame(annotation_data['annotations'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df_images_cat = pd.DataFrame(annotation_data['categories'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#df_images_cat[:20]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#df_images_data[:10]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df_all_data = df_images_data.merge(df_images_info, left_on='image_id', right_on='id')\n", | |
"df_all_data = df_all_data.merge(df_images_cat, left_on='category_id', right_on='id')\n", | |
"df_all_data_boat = df_all_data[df_all_data['id']==9]\n", | |
"#df_all_data_boat.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Пример изображения и bound box" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_id = 543043" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df_temp = df_all_data_boat[df_all_data_boat['image_id']==image_id]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'000000543043.jpg'" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df_temp.iloc[0]['file_name']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"img = mpimg.imread(f'val2017/{df_temp.iloc[0][\"file_name\"]}')\n", | |
"fig, ax = plt.subplots()\n", | |
"ax.imshow(img)\n", | |
"for i in range(len(df_temp)):\n", | |
" bbox_coord = df_temp.iloc[i][\"bbox\"]\n", | |
" rect = patches.Rectangle((bbox_coord[0], bbox_coord[1]), bbox_coord[2], bbox_coord[3], linewidth=1, edgecolor='r', facecolor='none')\n", | |
" ax.add_patch(rect)\n", | |
"plt.show()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment