303 lines
9.1 KiB
Plaintext
303 lines
9.1 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"id": "60b9db20",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"import glob\n",
|
|||
|
"import pandas as pd\n",
|
|||
|
"import cv2\n",
|
|||
|
"import os\n",
|
|||
|
"from xml.etree import ElementTree"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"id": "02938bb8",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"ANNOTATIONS_DIR = './data/annotations'\n",
|
|||
|
"IMAGES_DIR = './data/images'\n",
|
|||
|
"CROPPED_DIR = './data/cropped_images'"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"id": "b2cd1af7",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"metadata = {'xmin': [], 'ymin': [], 'xmax': [], 'ymax': [], 'label': [], 'file': []}\n",
|
|||
|
"\n",
|
|||
|
"for file in glob.glob(ANNOTATIONS_DIR + '/*.xml'):\n",
|
|||
|
" tree = ElementTree.parse(file)\n",
|
|||
|
" file_name = os.path.splitext(os.path.basename(file))[0]\n",
|
|||
|
" \n",
|
|||
|
" for annotation in tree.iter():\n",
|
|||
|
"\n",
|
|||
|
" if 'object' in annotation.tag:\n",
|
|||
|
" for attr in list(annotation):\n",
|
|||
|
" \n",
|
|||
|
" if 'name' in attr.tag:\n",
|
|||
|
" name = attr.text \n",
|
|||
|
" metadata['label'].append(name)\n",
|
|||
|
" metadata['file'].append(file_name)\n",
|
|||
|
" \n",
|
|||
|
" if 'bndbox' in attr.tag:\n",
|
|||
|
" for dimensions in list(attr):\n",
|
|||
|
" if 'xmin' in dimensions.tag:\n",
|
|||
|
" xmin = int(round(float(dimensions.text)))\n",
|
|||
|
" metadata['xmin'].append(xmin)\n",
|
|||
|
" if 'ymin' in dimensions.tag:\n",
|
|||
|
" ymin = int(round(float(dimensions.text)))\n",
|
|||
|
" metadata['ymin'].append(ymin) \n",
|
|||
|
" if 'xmax' in dimensions.tag:\n",
|
|||
|
" xmax = int(round(float(dimensions.text)))\n",
|
|||
|
" metadata['xmax'].append(xmax) \n",
|
|||
|
" if 'ymax' in dimensions.tag:\n",
|
|||
|
" ymax = int(round(float(dimensions.text)))\n",
|
|||
|
" metadata['ymax'].append(ymax)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"id": "7de14020",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/html": [
|
|||
|
"<div>\n",
|
|||
|
"<style scoped>\n",
|
|||
|
" .dataframe tbody tr th:only-of-type {\n",
|
|||
|
" vertical-align: middle;\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" .dataframe tbody tr th {\n",
|
|||
|
" vertical-align: top;\n",
|
|||
|
" }\n",
|
|||
|
"\n",
|
|||
|
" .dataframe thead th {\n",
|
|||
|
" text-align: right;\n",
|
|||
|
" }\n",
|
|||
|
"</style>\n",
|
|||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|||
|
" <thead>\n",
|
|||
|
" <tr style=\"text-align: right;\">\n",
|
|||
|
" <th></th>\n",
|
|||
|
" <th>xmin</th>\n",
|
|||
|
" <th>ymin</th>\n",
|
|||
|
" <th>xmax</th>\n",
|
|||
|
" <th>ymax</th>\n",
|
|||
|
" <th>label</th>\n",
|
|||
|
" <th>file</th>\n",
|
|||
|
" </tr>\n",
|
|||
|
" </thead>\n",
|
|||
|
" <tbody>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>0</th>\n",
|
|||
|
" <td>79</td>\n",
|
|||
|
" <td>105</td>\n",
|
|||
|
" <td>109</td>\n",
|
|||
|
" <td>142</td>\n",
|
|||
|
" <td>without_mask</td>\n",
|
|||
|
" <td>maksssksksss0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>1</th>\n",
|
|||
|
" <td>185</td>\n",
|
|||
|
" <td>100</td>\n",
|
|||
|
" <td>226</td>\n",
|
|||
|
" <td>144</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>2</th>\n",
|
|||
|
" <td>325</td>\n",
|
|||
|
" <td>90</td>\n",
|
|||
|
" <td>360</td>\n",
|
|||
|
" <td>141</td>\n",
|
|||
|
" <td>without_mask</td>\n",
|
|||
|
" <td>maksssksksss0</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>3</th>\n",
|
|||
|
" <td>321</td>\n",
|
|||
|
" <td>34</td>\n",
|
|||
|
" <td>354</td>\n",
|
|||
|
" <td>69</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss1</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4</th>\n",
|
|||
|
" <td>224</td>\n",
|
|||
|
" <td>38</td>\n",
|
|||
|
" <td>261</td>\n",
|
|||
|
" <td>73</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss1</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>...</th>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" <td>...</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4067</th>\n",
|
|||
|
" <td>263</td>\n",
|
|||
|
" <td>62</td>\n",
|
|||
|
" <td>287</td>\n",
|
|||
|
" <td>85</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss98</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4068</th>\n",
|
|||
|
" <td>344</td>\n",
|
|||
|
" <td>80</td>\n",
|
|||
|
" <td>377</td>\n",
|
|||
|
" <td>106</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss98</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4069</th>\n",
|
|||
|
" <td>181</td>\n",
|
|||
|
" <td>54</td>\n",
|
|||
|
" <td>273</td>\n",
|
|||
|
" <td>162</td>\n",
|
|||
|
" <td>mask_weared_incorrect</td>\n",
|
|||
|
" <td>maksssksksss99</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4070</th>\n",
|
|||
|
" <td>99</td>\n",
|
|||
|
" <td>87</td>\n",
|
|||
|
" <td>176</td>\n",
|
|||
|
" <td>165</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss99</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" <tr>\n",
|
|||
|
" <th>4071</th>\n",
|
|||
|
" <td>289</td>\n",
|
|||
|
" <td>99</td>\n",
|
|||
|
" <td>355</td>\n",
|
|||
|
" <td>233</td>\n",
|
|||
|
" <td>with_mask</td>\n",
|
|||
|
" <td>maksssksksss99</td>\n",
|
|||
|
" </tr>\n",
|
|||
|
" </tbody>\n",
|
|||
|
"</table>\n",
|
|||
|
"<p>4072 rows × 6 columns</p>\n",
|
|||
|
"</div>"
|
|||
|
],
|
|||
|
"text/plain": [
|
|||
|
" xmin ymin xmax ymax label file\n",
|
|||
|
"0 79 105 109 142 without_mask maksssksksss0\n",
|
|||
|
"1 185 100 226 144 with_mask maksssksksss0\n",
|
|||
|
"2 325 90 360 141 without_mask maksssksksss0\n",
|
|||
|
"3 321 34 354 69 with_mask maksssksksss1\n",
|
|||
|
"4 224 38 261 73 with_mask maksssksksss1\n",
|
|||
|
"... ... ... ... ... ... ...\n",
|
|||
|
"4067 263 62 287 85 with_mask maksssksksss98\n",
|
|||
|
"4068 344 80 377 106 with_mask maksssksksss98\n",
|
|||
|
"4069 181 54 273 162 mask_weared_incorrect maksssksksss99\n",
|
|||
|
"4070 99 87 176 165 with_mask maksssksksss99\n",
|
|||
|
"4071 289 99 355 233 with_mask maksssksksss99\n",
|
|||
|
"\n",
|
|||
|
"[4072 rows x 6 columns]"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"metadata_df = pd.DataFrame(metadata)\n",
|
|||
|
"metadata_df"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 10,
|
|||
|
"id": "ac7bdedc",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"for i in range(len(metadata_df)):\n",
|
|||
|
" \n",
|
|||
|
" path = IMAGES_DIR + '/' + metadata_df['file'].iloc[i] + '.png'\n",
|
|||
|
"\n",
|
|||
|
" image = cv2.imread(path)\n",
|
|||
|
" \n",
|
|||
|
" cropped_name = str(i) + '.png'\n",
|
|||
|
" \n",
|
|||
|
" xmin = metadata_df['xmin'].iloc[i]\n",
|
|||
|
" ymin = metadata_df['ymin'].iloc[i]\n",
|
|||
|
" xmax = metadata_df['xmax'].iloc[i]\n",
|
|||
|
" ymax = metadata_df['ymax'].iloc[i]\n",
|
|||
|
"\n",
|
|||
|
" cropped_image = image[ymin:ymax, xmin:xmax]\n",
|
|||
|
" \n",
|
|||
|
" cropped_path = CROPPED_DIR + '/' + cropped_name\n",
|
|||
|
" \n",
|
|||
|
" cv2.imwrite(cropped_path, cropped_image)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 12,
|
|||
|
"id": "a2998898",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"metadata_df['label'].to_csv(r'./data/labels.txt', index=False, header=False)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": null,
|
|||
|
"id": "df86c02f",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": []
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"kernelspec": {
|
|||
|
"display_name": "Python 3 (ipykernel)",
|
|||
|
"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.9.7"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 5
|
|||
|
}
|