init-GrandPrix+dataset_v1-15kps

This commit is contained in:
2022-08-14 23:37:05 +03:00
parent 998aec2b80
commit 6e22f83077
166 changed files with 716 additions and 0 deletions

View File

@@ -0,0 +1,227 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "07bdc0da",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import numpy as np\n",
"from tqdm.notebook import tqdm\n",
"path_dataset = r'D:\\GrandPrix\\Grand-Prix'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ca890aa0",
"metadata": {},
"outputs": [],
"source": [
"def find_image(id):\n",
" for row in coco['images']:\n",
" if row['id'] == id:\n",
" return row"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b8135031",
"metadata": {},
"outputs": [],
"source": [
"def ltrb_from_cloud(cloud_2d, imgSize, expansion=0.1):\n",
" height, width = imgSize\n",
" ltrb = np.round((cloud_2d[:, 0].min(), cloud_2d[:, 1].min(),\n",
" cloud_2d[:, 0].max(), cloud_2d[:, 1].max())).astype(int)\n",
" \n",
" if expansion > 0:\n",
" dx = np.round((ltrb[2]-ltrb[0])*expansion/2)\n",
" dy = np.round((ltrb[3]-ltrb[1])*expansion/2)\n",
" ltrb += np.array([-dx, -2*dy, dx, dy], dtype=int)\n",
" \n",
" ltrb[[0,2]] = np.clip(ltrb[[0,2]], 0, width)\n",
" ltrb[[1,3]] = np.clip(ltrb[[1,3]], 0, height)\n",
" \n",
" return ltrb\n",
"\n",
"def ltrb2ltwh(ltrb):\n",
" return np.array([ltrb[0], ltrb[1], ltrb[2]-ltrb[0], ltrb[3]-ltrb[1]], dtype=int)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "79762c08",
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.join(path_dataset, 'GrandPrix_COCO.json'), 'r') as file:\n",
" coco = json.load(file)"
]
},
{
"cell_type": "markdown",
"id": "a1cfe0e2",
"metadata": {},
"source": [
"### Bboxes from pose"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f9440b54",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "758138a872a9485885387a5ffe5a9696",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/148 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for obj in tqdm(coco['annotations']):\n",
" keypoints_2d = np.array(obj['keypoints']).reshape((-1, 3))[:, :2]\n",
" id, width, height, file_name = find_image(obj['image_id']).values()\n",
" \n",
" bbox_ltrb = ltrb_from_cloud(keypoints_2d, (height, width), 0.4)\n",
" bbox_ltwh = ltrb2ltwh(bbox_ltrb).tolist()\n",
" obj['bbox'] = bbox_ltwh\n",
" obj['area'] = bbox_ltwh[2]*bbox_ltwh[3]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dfcb675b",
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.join(path_dataset, 'GrandPrix_COCO.json'), 'w') as file:\n",
" json.dump(coco, file)"
]
},
{
"cell_type": "markdown",
"id": "e8ff698d",
"metadata": {},
"source": [
"### Split COCO json to train/val"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3aec7a2b",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"import copy\n",
"\n",
"def split_coco_json(coco, test_size=0.2, random_state=0):\n",
" \n",
" train_idx, test_idx = train_test_split([i['id'] for i in coco['images']],\n",
" test_size=test_size, random_state=random_state)\n",
"\n",
"\n",
" train = copy.deepcopy(coco)\n",
" test = copy.deepcopy(coco)\n",
"\n",
" test['images'] = [x for x in coco['images'] if x['id'] in test_idx]\n",
" train['images'] = [x for x in coco['images'] if x['id'] in train_idx]\n",
"\n",
" test['annotations'] = [x for x in coco['annotations'] if x['image_id'] in test_idx]\n",
" train['annotations'] = [x for x in coco['annotations'] if x['image_id'] in train_idx]\n",
" return train, test"
]
},
{
"cell_type": "markdown",
"id": "1cdd1a3c",
"metadata": {},
"source": [
"### Create new splited dataset"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "aafd54fb",
"metadata": {},
"outputs": [],
"source": [
"train, test = split_coco_json(coco, 0.1, random_state=77)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f6af98fc",
"metadata": {},
"outputs": [],
"source": [
"test_path_images = [os.path.join(path_dataset, 'images', 'img', x['file_name']) for x in test['images']]\n",
"train_path_images = [os.path.join(path_dataset, 'images', 'img', x['file_name']) for x in train['images']]\n",
"\n",
"import shutil\n",
"path_new_dataset = r'C:\\Users\\Kir\\Jupiter\\Drivecast\\place6D_Nurburgring\\Grand_Prix\\Training\\GrandPrix_dataset_v1_15'\n",
"\n",
"path_train_img = os.path.join(path_new_dataset, 'images', 'train')\n",
"path_test_img = os.path.join(path_new_dataset, 'images', 'val')\n",
"path_ann = os.path.join(path_new_dataset, 'annotations')\n",
"\n",
"os.makedirs(path_train_img, exist_ok=True)\n",
"os.makedirs(path_test_img, exist_ok=True)\n",
"os.makedirs(path_ann, exist_ok=True)\n",
"\n",
"with open(os.path.join(path_ann, 'train.json'), 'w') as file:\n",
" json.dump(train, file)\n",
" \n",
"with open(os.path.join(path_ann, 'val.json'), 'w') as file:\n",
" json.dump(test, file)\n",
"\n",
"for path in train_path_images:\n",
" shutil.copy(path, path_train_img)\n",
"\n",
"for path in test_path_images:\n",
" shutil.copy(path, path_test_img)"
]
}
],
"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
}