dataset_88kps+some-updates

This commit is contained in:
2022-08-14 09:59:01 +03:00
parent ac04526fe1
commit 998aec2b80
943 changed files with 945 additions and 938 deletions

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 1,
"id": "07bdc0da", "id": "07bdc0da",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -11,17 +11,17 @@
"import json\n", "import json\n",
"import numpy as np\n", "import numpy as np\n",
"from tqdm.notebook import tqdm\n", "from tqdm.notebook import tqdm\n",
"path_dataset = r'D:\\downloads\\Nurburg-karussel'\n", "path_dataset = r'D:\\Karusel\\Nurburg-karussel_88'\n",
"from SuperviselyKeypointsGUI.SuperviselyKeypointsGUI import *\n", "from SuperviselyKeypointsGUI.SuperviselyKeypointsGUI import *\n",
"\n", "\n",
"\n", "\n",
"keypoints_3d_path = r'SuperviselyKeypointsGUI\\karussel_24kps.csv'\n", "keypoints_3d_path = r'C:\\Users\\Kir\\Jupiter\\Nurburg\\keypoints_88\\karussel_88kps.csv'\n",
"keypoints_3d = pd.read_csv(keypoints_3d_path, index_col=0).astype(float)" "keypoints_3d = pd.read_csv(keypoints_3d_path, index_col=0).astype(float)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 2,
"id": "ca890aa0", "id": "ca890aa0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -34,7 +34,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 3,
"id": "b8135031", "id": "b8135031",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -60,7 +60,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 4,
"id": "79762c08", "id": "79762c08",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -79,10 +79,25 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"id": "f9440b54", "id": "f9440b54",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1a27ecccf8054a5082d0a51178c0f2a9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/448 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"for obj in tqdm(coco['annotations']):\n", "for obj in tqdm(coco['annotations']):\n",
" keypoints_2d = np.array(obj['keypoints']).reshape((-1, 3))[:, :2]\n", " keypoints_2d = np.array(obj['keypoints']).reshape((-1, 3))[:, :2]\n",
@@ -96,13 +111,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 7,
"id": "dfcb675b", "id": "dfcb675b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# with open(os.path.join(path_dataset, 'karusel_COCO.json'), 'w') as file:\n", "with open(os.path.join(path_dataset, 'karusel_COCO.json'), 'w') as file:\n",
"# json.dump(coco, file)" " json.dump(coco, file)"
] ]
}, },
{ {
@@ -115,7 +130,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 8,
"id": "3aec7a2b", "id": "3aec7a2b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -150,17 +165,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 9,
"id": "aafd54fb", "id": "aafd54fb",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"train, test = split_coco_json(coco, 0.1, random_state=777)" "train, test = split_coco_json(coco, 0.1, random_state=77)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 10,
"id": "f6af98fc", "id": "f6af98fc",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -169,7 +184,7 @@
"train_path_images = [os.path.join(path_dataset, 'images', 'img', x['file_name']) for x in train['images']]\n", "train_path_images = [os.path.join(path_dataset, 'images', 'img', x['file_name']) for x in train['images']]\n",
"\n", "\n",
"import shutil\n", "import shutil\n",
"path_new_dataset = r'C:\\Users\\Kir\\Jupiter\\Nurburg\\OpenPifPaf\\Training\\Karusel_dataset'\n", "path_new_dataset = r'C:\\Users\\Kir\\Jupiter\\Drivecast\\place6D_Nurbsurgring\\Karussell\\Training\\Karusel_dataset_v2_88'\n",
"\n", "\n",
"path_train_img = os.path.join(path_new_dataset, 'images', 'train')\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_test_img = os.path.join(path_new_dataset, 'images', 'val')\n",

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 3,
"id": "283f6e9c", "id": "283f6e9c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -14,7 +14,7 @@
"import json\n", "import json\n",
"from tqdm.notebook import tqdm\n", "from tqdm.notebook import tqdm\n",
"\n", "\n",
"path_dataset = r'D:\\downloads\\Nurburg-karussel'\n", "path_dataset = r'D:\\Karusel\\Nurburg-karussel_88'\n",
"with open(os.path.join(path_dataset, 'meta.json'), 'r') as j:\n", "with open(os.path.join(path_dataset, 'meta.json'), 'r') as j:\n",
" meta = json.load(j)\n", " meta = json.load(j)\n",
"\n", "\n",
@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 5,
"id": "7af948d4", "id": "7af948d4",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -41,7 +41,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 6,
"id": "7ea0771e", "id": "7ea0771e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -54,13 +54,14 @@
" keypoints_2d.loc[i] = nodes[label2hash(meta_json, obj)[str(i)]]['loc']\n", " keypoints_2d.loc[i] = nodes[label2hash(meta_json, obj)[str(i)]]['loc']\n",
"\n", "\n",
" keypoints_2d['v'] = 2\n", " keypoints_2d['v'] = 2\n",
" keypoints_2d[(keypoints_2d.x==0)&(keypoints_2d.y==0)] = 0\n",
" keypoints_2d = keypoints_2d.astype(float).round().astype(int)\n", " keypoints_2d = keypoints_2d.astype(float).round().astype(int)\n",
" return keypoints_2d[:24]" " return keypoints_2d"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 7,
"id": "26b6abf4", "id": "26b6abf4",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -93,7 +94,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 8,
"id": "7c4d21a7",
"metadata": {},
"outputs": [],
"source": [
"def skeleton_supervisely(meta):\n",
" meta_nodes = meta['classes'][0]['geometry_config']['nodes']\n",
" label2hash = {}\n",
" for name in meta_nodes:\n",
" label2hash[meta_nodes[name]['label']] = name\n",
"\n",
" skeleton_supervisely = []\n",
" for edge in meta['classes'][0]['geometry_config']['edges']:\n",
" skeleton_supervisely.append([meta_nodes[edge['src']]['label'], meta_nodes[edge['dst']]['label']])\n",
" return skeleton_supervisely"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "155be42d", "id": "155be42d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -113,21 +133,22 @@
" for obj in ann['objects']:\n", " for obj in ann['objects']:\n",
" keypoints = annotations(meta, obj)\n", " keypoints = annotations(meta, obj)\n",
" annotations_list.append(ann_json(keypoints, i, obj))\n", " annotations_list.append(ann_json(keypoints, i, obj))\n",
" return image_list, annotations_list" " return image_list, annotations_list, meta"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 10,
"id": "6593f677", "id": "6593f677",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def COCO(image_list, annotations_list):\n", "def COCO(image_list, annotations_list, meta):\n",
" num_kpts = len(meta['classes'][0]['geometry_config']['nodes'])\n",
" coco = {\n", " coco = {\n",
"\n", "\n",
" \"info\": {\n", " \"info\": {\n",
" \"description\": \"karusel Dataset\", \"version\": \"2.0\"\n", " \"description\": \"karusel Dataset\", \"version\": \"2.0\", \"keypoints\":num_kpts\n",
" },\n", " },\n",
"\n", "\n",
" \"categories\": [\n", " \"categories\": [\n",
@@ -135,13 +156,8 @@
" \"supercategory\": \"NurburgRing\",\n", " \"supercategory\": \"NurburgRing\",\n",
" \"id\": 1,\n", " \"id\": 1,\n",
" \"name\": \"karusel\",\n", " \"name\": \"karusel\",\n",
" \"keypoints\": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\n", " \"keypoints\": list(range(num_kpts)),\n",
" 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],\n", " \"skeleton\": skeleton_supervisely(meta)\n",
" \"skeleton\": [\n",
" [1, 2],[2, 3],[3, 4],[4, 5],[5, 6],[6, 7],[7, 8],[8, 9],[9, 10],[10, 11],\n",
" [11, 12],[12, 13],[13, 14],[14, 15],[15, 16],[16, 17],[17, 18],[18, 19],[19, 20],\n",
" [20, 21],[21, 22],[22, 23],[23, 24],[24, 1],[24, 3],[1, 5]\n",
" ]\n",
" }\n", " }\n",
" ]\n", " ]\n",
" }\n", " }\n",
@@ -153,14 +169,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 11,
"id": "38880d13", "id": "38880d13",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "96021bfc91ce425dbef57ae348321005", "model_id": "219ba034d89346f188cf37dc9a722263",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@@ -178,7 +194,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 12,
"id": "314565c2", "id": "314565c2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 1,
"id": "07bdc0da", "id": "07bdc0da",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -11,17 +11,17 @@
"import json\n", "import json\n",
"import numpy as np\n", "import numpy as np\n",
"from tqdm.notebook import tqdm\n", "from tqdm.notebook import tqdm\n",
"path_dataset = r'D:\\downloads\\Nurburg-karussel'\n", "path_dataset = r'D:\\Karusel\\Nurburg-karussel_88'\n",
"from SuperviselyKeypointsGUI.SuperviselyKeypointsGUI import *\n", "from SuperviselyKeypointsGUI.SuperviselyKeypointsGUI import *\n",
"\n", "\n",
"\n", "\n",
"keypoints_3d_path = r'SuperviselyKeypointsGUI\\karussel_24kps.csv'\n", "keypoints_3d_path = r'C:\\Users\\Kir\\Jupiter\\Nurburg\\keypoints_88\\karussel_88kps.csv'\n",
"keypoints_3d = pd.read_csv(keypoints_3d_path, index_col=0).astype(float)" "keypoints_3d = pd.read_csv(keypoints_3d_path, index_col=0).astype(float)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 2,
"id": "ca890aa0", "id": "ca890aa0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -34,7 +34,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 3,
"id": "b8135031", "id": "b8135031",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -60,7 +60,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 4,
"id": "79762c08", "id": "79762c08",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -79,14 +79,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 5,
"id": "f9440b54", "id": "f9440b54",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "36c51c895f4e4f6b806c2af9e7bc14b9", "model_id": "1a27ecccf8054a5082d0a51178c0f2a9",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@@ -111,13 +111,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 7,
"id": "dfcb675b", "id": "dfcb675b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# with open(os.path.join(path_dataset, 'karusel_COCO.json'), 'w') as file:\n", "with open(os.path.join(path_dataset, 'karusel_COCO.json'), 'w') as file:\n",
"# json.dump(coco, file)" " json.dump(coco, file)"
] ]
}, },
{ {
@@ -130,7 +130,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 8,
"id": "3aec7a2b", "id": "3aec7a2b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -165,7 +165,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 9,
"id": "aafd54fb", "id": "aafd54fb",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -175,7 +175,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 10,
"id": "f6af98fc", "id": "f6af98fc",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -184,7 +184,7 @@
"train_path_images = [os.path.join(path_dataset, 'images', 'img', x['file_name']) for x in train['images']]\n", "train_path_images = [os.path.join(path_dataset, 'images', 'img', x['file_name']) for x in train['images']]\n",
"\n", "\n",
"import shutil\n", "import shutil\n",
"path_new_dataset = r'C:\\Users\\Kir\\Jupiter\\Drivecast\\place6D_Nurburgring\\Karussell\\Training\\Karusel_dataset_v2'\n", "path_new_dataset = r'C:\\Users\\Kir\\Jupiter\\Drivecast\\place6D_Nurbsurgring\\Karussell\\Training\\Karusel_dataset_v2_88'\n",
"\n", "\n",
"path_train_img = os.path.join(path_new_dataset, 'images', 'train')\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_test_img = os.path.join(path_new_dataset, 'images', 'val')\n",

View File

@@ -0,0 +1,386 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4c948dcd",
"metadata": {},
"outputs": [],
"source": [
"import supervisely_lib as sly\n",
"import pandas as pd\n",
"import cv2 as cv\n",
"import os\n",
"import numpy as np\n",
"from utils import *\n",
"from tqdm.notebook import tqdm\n",
"import plotly.express as px\n",
"import plotly.graph_objects as go\n",
"\n",
"keypoints_3d_24 = pd.read_csv('karussel_24kps.csv', index_col=0).astype(float)\n",
"keypoints_3d_88 = pd.read_csv('keypoints_88/karussel_88kps.csv', index_col=0).astype(float)\n",
"keypoints_3d_88.z = 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2ff23cb1",
"metadata": {},
"outputs": [],
"source": [
"def load_ann(img_id):\n",
" ann_info = api.annotation.download(img_id)\n",
" return ann_info\n",
"\n",
"def label2hash(meta_json, last):\n",
" for clss in meta_json['classes']:\n",
" if clss['title'] == last['classTitle'].values[0]:\n",
" meta_nodes = clss['geometry_config']['nodes']\n",
" label2hash = {}\n",
" for name in meta_nodes:\n",
" label2hash[meta_nodes[name]['label']] = name\n",
" return label2hash\n",
"\n",
"def new_annotations(ann_info, new_keypoints, flag=False):\n",
" last = last_keypoints_on_img(ann_info)\n",
" nodes = ann_info[2]['objects'][last.index[0]]['nodes']\n",
" for i in new_keypoints.index:\n",
" if (not label2hash(meta_json, last)[str(i)] in nodes.keys()) and flag:\n",
" nodes[label2hash(meta_json, last)[str(i)]] = {'loc': [0, 0]}\n",
" \n",
" nodes[label2hash(meta_json, last)[str(i)]]['loc'] = new_keypoints.loc[i].tolist()\n",
" return ann_info\n",
"\n",
"def annotations(ann_info):\n",
"\n",
" last = last_keypoints_on_img(ann_info)\n",
" if len(last) == 0:\n",
" return \n",
" nodes = ann_info[2]['objects'][last.index[0]]['nodes']\n",
" keypoints_2d = pd.DataFrame(columns=['x', 'y'])\n",
"\n",
" for i in range(1, len(nodes)+1):\n",
" keypoints_2d.loc[i] = nodes[label2hash(meta_json, last)[str(i)]]['loc']\n",
"\n",
" return keypoints_2d\n",
"\n",
"def last_keypoints_on_img(ann_info):\n",
" updated = []\n",
" for obj in ann_info[2]['objects']:\n",
" updated.append([obj['classTitle'], obj['updatedAt']])\n",
" updated = pd.DataFrame(updated, columns=['classTitle', 'updatedAt'])\n",
" updated.updatedAt = pd.to_datetime(updated.updatedAt)\n",
" last = updated[updated.updatedAt == updated.updatedAt.max()]\n",
" return last"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e49d4952",
"metadata": {},
"outputs": [],
"source": [
"adress = 'https://app.supervise.ly/'\n",
"token = '8N4spdUrCH0BxeQvv1nicgj7VDEsbpo1XUB7SKDk87woYPZPYkDo05o2jt1gpiCaiTaC1ePcWoYEi2Q2ehW5t9mghoQBShn5a6LUjfjhVRIXZ2ol2otf5HADq2EAszQP'\n",
"project_id = 192775\n",
"dataset_id = 658585\n",
"api = sly.Api(adress, token)\n",
"meta_json = api.project.get_meta(project_id)\n",
"meta = sly.ProjectMeta.from_json(meta_json)\n",
"images = pd.DataFrame(api.image.get_list(dataset_id)).sort_values('name', ignore_index=True)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "558309c6",
"metadata": {},
"outputs": [],
"source": [
"# name = 'NLS4[00_04_07]006.png'\n",
"# index = images.loc[images.name==name].index[0]\n",
"\n",
"\n",
"# img = cv.imread(os.path.join(\n",
"# r'C:\\Users\\Kir\\Jupiter\\Nurburg\\OpenPifPaf\\Training\\Karusel_dataset_v2\\images\\val', name))\n",
"# img = cv.cvtColor(img, cv.COLOR_BGR2RGB)\n",
"\n",
"# img_id = int(images.loc[index, 'id'])\n",
"# img_name = images.loc[index, 'name']\n",
"# ann_info = load_ann(img_id)\n",
"# keypoints_24 = annotations(ann_info)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "50bf81ff",
"metadata": {},
"outputs": [],
"source": [
"# a['objects'][0]['classTitle'] = '88kps-bot'\n",
"# ann_info[2]['objects'] = a['objects']\n",
"# keypoints_24 = annotations(ann_info)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "f510305b",
"metadata": {},
"outputs": [],
"source": [
"rvec, tvec, camMatrx, dist = fit((1920, 1080), keypoints_24, keypoints_3d_24, focus=1)\n",
"imgpts_24 = cv.projectPoints(keypoints_3d_24.values, rvec, tvec, camMatrx, dist)[0][:, 0].astype(int)\n",
"imgpts_88 = cv.projectPoints(keypoints_3d_88.values, rvec, tvec, camMatrx, dist)[0][:, 0].astype(int)\n",
"\n",
"new_keypoints = pd.DataFrame(imgpts_88, columns=['x', 'y'], index=range(1, len(keypoints_3d_88)+1))\n",
"new_keypoints[(new_keypoints.x<0) | (new_keypoints.y<0) \\\n",
" | (new_keypoints.x>=1920) | (new_keypoints.y>=1080)] *= 0"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f619f420",
"metadata": {},
"outputs": [],
"source": [
"# fig = px.imshow(img)\n",
"# fig.add_trace(go.Scatter(x=imgpts_24[:,0], y=imgpts_24[:,1],\n",
"# marker=dict(color='white', size=5), mode='markers'))\n",
"# fig.add_trace(go.Scatter(x=new_keypoints.values[:,0], y=new_keypoints.values[:,1],\n",
"# marker=dict(color='blue', size=5), mode='markers'))\n",
"# fig.add_trace(go.Scatter(x=keypoints_24.values[:,0], y=keypoints_24.values[:,1],\n",
"# marker=dict(color='black', size=5), mode='markers'))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "88c81371",
"metadata": {},
"outputs": [],
"source": [
"new_ann = new_annotations(ann_info, new_keypoints, flag=True)\n",
"new_ann = sly.Annotation.from_json(new_ann.annotation, meta)\n",
"api.annotation.upload_ann(img_id, new_ann)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45d35b7c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c2a287f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d0dfe8b4",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dd8d392171e14a2281945b8575b435df",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/448 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"error = []\n",
"for index in tqdm(range(len(images))):\n",
" img_id = int(images.loc[index, 'id'])\n",
" img_name = images.loc[index, 'name']\n",
" ann_info = load_ann(img_id)\n",
" keypoints_24 = annotations(ann_info)\n",
" \n",
" if len(keypoints_24) != 24:\n",
" error.append(img_name)\n",
" continue\n",
" \n",
" rvec, tvec, camMatrx, dist = fit((1920, 1080), keypoints_24, keypoints_3d_24, focus=1)\n",
" imgpts_88 = cv.projectPoints(keypoints_3d_88.values, rvec, tvec, camMatrx, dist)[0][:, 0].astype(int)\n",
" new_keypoints = pd.DataFrame(imgpts_88, columns=['x', 'y'], index=range(1, len(keypoints_3d_88)+1))\n",
" new_keypoints[(new_keypoints.x<0) | (new_keypoints.y<0) \\\n",
" | (new_keypoints.x>=1920) | (new_keypoints.y>=1080)] *= 0\n",
"\n",
" new_ann = new_annotations(ann_info, new_keypoints, flag=True)\n",
" new_ann = sly.Annotation.from_json(new_ann.annotation, meta)\n",
" api.annotation.upload_ann(img_id, new_ann)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0e32ce4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b70bdcf",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d04e0a8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "6713e0ed",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# no_ann = []\n",
"# for index in tqdm(range(len(images))):\n",
"# img_id = int(images.loc[index, 'id'])\n",
"# img_name = images.loc[index, 'name']\n",
"# ann_info = load_ann(img_id)\n",
"\n",
"# json_df = pd.read_json(os.path.join(folder,img_name+'.predictions.json'))\n",
"# if len(json_df)>0:\n",
"# keypoints_2d = json_df.sort_values('score').reset_index().loc[0, 'keypoints']\n",
"# keypoints_2d = pd.DataFrame(np.array(keypoints_2d).reshape(-1, 3),\n",
"# columns=['x','y','conf'], index=range(1, 25))\n",
"\n",
"# conf_keypoints_2d = keypoints_2d[keypoints_2d['conf']>0.15]\n",
"# if len(conf_keypoints_2d)>5:\n",
"# rvec, tvec, camMatrx, dist = fit((1920, 1080), conf_keypoints_2d, keypoints_3d, focus=1)\n",
"\n",
"# imgpts = cv.projectPoints(keypoints_3d.values, rvec, tvec, camMatrx, dist)[0][:, 0].astype(int)\n",
"# new_keypoints = pd.DataFrame(imgpts, columns=['x', 'y'], index=range(1, len(keypoints_3d)+1))\n",
"# else: \n",
"# new_keypoints = keypoints_2d[['x', 'y']].astype(int)\n",
"\n",
"# else:\n",
"# no_ann.append(img_name)\n",
"\n",
"\n",
"# new_ann = new_annotations(ann_info, new_keypoints)\n",
"# new_ann = sly.Annotation.from_json(new_ann.annotation, meta)\n",
"# api.annotation.upload_ann(img_id, new_ann)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ab1e73a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d632830",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a97bbbc3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8598689",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0974e494",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"id": "18a70aa4",
"metadata": {},
"outputs": [],
"source": [
"# \n",
"# import glob\n",
"# folder = r'C:\\Users\\Kir\\Downloads\\NLS\\images'\n",
"# list_png = glob.glob(folder+'\\*.png')\n",
"# list_json = glob.glob(folder+'\\*.json')\n",
"\n",
"# df = pd.DataFrame(np.array([list_png, list_json]).T, columns=['png', 'json'])\n",
"\n",
"\n",
"# def funk(str):\n",
"# str = os.path.basename(str)\n",
"# return os.path.join(folder, str[4:8]+'['+str[27:35]+']'+str[41:44]+'.png')\n",
"# funk(df.png[40])\n",
"\n",
"# df['png_new'] = df['png'].apply(funk) \n",
"# df['json_new'] = df['json'].apply(funk) \n",
"\n",
"# for i in range(len(df)):\n",
"# os.rename(df['png'][i], df['png_new'][i])\n",
"# os.rename(df['json'][i], df['json_new'][i])"
]
}
],
"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
}

View File

@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 3,
"id": "283f6e9c", "id": "283f6e9c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -14,7 +14,7 @@
"import json\n", "import json\n",
"from tqdm.notebook import tqdm\n", "from tqdm.notebook import tqdm\n",
"\n", "\n",
"path_dataset = r'D:\\downloads\\Nurburg-karussel'\n", "path_dataset = r'D:\\Karusel\\Nurburg-karussel_88'\n",
"with open(os.path.join(path_dataset, 'meta.json'), 'r') as j:\n", "with open(os.path.join(path_dataset, 'meta.json'), 'r') as j:\n",
" meta = json.load(j)\n", " meta = json.load(j)\n",
"\n", "\n",
@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 5,
"id": "7af948d4", "id": "7af948d4",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -41,7 +41,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 6,
"id": "7ea0771e", "id": "7ea0771e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -54,13 +54,14 @@
" keypoints_2d.loc[i] = nodes[label2hash(meta_json, obj)[str(i)]]['loc']\n", " keypoints_2d.loc[i] = nodes[label2hash(meta_json, obj)[str(i)]]['loc']\n",
"\n", "\n",
" keypoints_2d['v'] = 2\n", " keypoints_2d['v'] = 2\n",
" keypoints_2d[(keypoints_2d.x==0)&(keypoints_2d.y==0)] = 0\n",
" keypoints_2d = keypoints_2d.astype(float).round().astype(int)\n", " keypoints_2d = keypoints_2d.astype(float).round().astype(int)\n",
" return keypoints_2d[:24]" " return keypoints_2d"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 7,
"id": "26b6abf4", "id": "26b6abf4",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -93,7 +94,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 8,
"id": "7c4d21a7",
"metadata": {},
"outputs": [],
"source": [
"def skeleton_supervisely(meta):\n",
" meta_nodes = meta['classes'][0]['geometry_config']['nodes']\n",
" label2hash = {}\n",
" for name in meta_nodes:\n",
" label2hash[meta_nodes[name]['label']] = name\n",
"\n",
" skeleton_supervisely = []\n",
" for edge in meta['classes'][0]['geometry_config']['edges']:\n",
" skeleton_supervisely.append([meta_nodes[edge['src']]['label'], meta_nodes[edge['dst']]['label']])\n",
" return skeleton_supervisely"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "155be42d", "id": "155be42d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -113,21 +133,22 @@
" for obj in ann['objects']:\n", " for obj in ann['objects']:\n",
" keypoints = annotations(meta, obj)\n", " keypoints = annotations(meta, obj)\n",
" annotations_list.append(ann_json(keypoints, i, obj))\n", " annotations_list.append(ann_json(keypoints, i, obj))\n",
" return image_list, annotations_list" " return image_list, annotations_list, meta"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 10,
"id": "6593f677", "id": "6593f677",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def COCO(image_list, annotations_list):\n", "def COCO(image_list, annotations_list, meta):\n",
" num_kpts = len(meta['classes'][0]['geometry_config']['nodes'])\n",
" coco = {\n", " coco = {\n",
"\n", "\n",
" \"info\": {\n", " \"info\": {\n",
" \"description\": \"karusel Dataset\", \"version\": \"2.0\"\n", " \"description\": \"karusel Dataset\", \"version\": \"2.0\", \"keypoints\":num_kpts\n",
" },\n", " },\n",
"\n", "\n",
" \"categories\": [\n", " \"categories\": [\n",
@@ -135,13 +156,8 @@
" \"supercategory\": \"NurburgRing\",\n", " \"supercategory\": \"NurburgRing\",\n",
" \"id\": 1,\n", " \"id\": 1,\n",
" \"name\": \"karusel\",\n", " \"name\": \"karusel\",\n",
" \"keypoints\": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\n", " \"keypoints\": list(range(num_kpts)),\n",
" 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],\n", " \"skeleton\": skeleton_supervisely(meta)\n",
" \"skeleton\": [\n",
" [1, 2],[2, 3],[3, 4],[4, 5],[5, 6],[6, 7],[7, 8],[8, 9],[9, 10],[10, 11],\n",
" [11, 12],[12, 13],[13, 14],[14, 15],[15, 16],[16, 17],[17, 18],[18, 19],[19, 20],\n",
" [20, 21],[21, 22],[22, 23],[23, 24],[24, 1],[24, 3],[1, 5]\n",
" ]\n",
" }\n", " }\n",
" ]\n", " ]\n",
" }\n", " }\n",
@@ -153,14 +169,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 11,
"id": "38880d13", "id": "38880d13",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "96021bfc91ce425dbef57ae348321005", "model_id": "219ba034d89346f188cf37dc9a722263",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@@ -178,7 +194,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 12,
"id": "314565c2", "id": "314565c2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],

View File

@@ -0,0 +1,100 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "9b13894c",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import pandas as pd\n",
"import numpy as np\n",
"import cv2 as cv\n",
"import plotly.express as px\n",
"import plotly.graph_objects as go\n",
"%cd ..\n",
"from utils import *\n",
"%cd Karusell_implantation\n",
"from __init__ import *\n",
"box_pict = pict_points(425)\n",
"logo, pts_pict = readShield(\"mercedes2.png\")\n",
"%cd -\n",
"%cd keypoints_88\n",
"\n",
"def roration(x):\n",
" x = np.deg2rad(x)\n",
" return np.array([[np.cos(x), -np.sin(x)],\n",
" [np.sin(x), np.cos(x)]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3fc452a2",
"metadata": {},
"outputs": [],
"source": [
"with open(r'sputnik_88_kps.json', 'r') as file:\n",
" img_json = json.load(file)\n",
" \n",
"img = cv.imread(r'sputnik.png')\n",
"\n",
"df = pd.DataFrame([img_json['objects'][i]['points']['exterior'][0]\\\n",
" for i in range(len(img_json['objects']))], columns=['x','y'], index=range(1,89))\n",
"df_raw = df.copy()\n",
"center = (732, 758)\n",
"df -= center\n",
"# df[['x', 'y']] = df.values[:, :2]@roration(90) # поворот x и y в плоскости земли\n",
"df['z'] = 0\n",
"\n",
"\n",
"\n",
"df = df.astype(float)\n",
"\n",
"# img = cv.circle(img, center, 5, (255,255,255), -1)\n",
"# fig = px.imshow(img)\n",
"# fig.add_trace(go.Scatter(x=df.x+center[0], y=df.y+center[1], text=df.index,\n",
"# marker=dict(color='red', size=5), mode='markers'))\n",
"\n",
"df[['x', 'y']] = df.values[:, :2]@roration(-3)\n",
"\n",
"rvec, tvec, camMatrx, dist = fit(img.shape[:2], df_raw, df)\n",
"imgpts = cv.projectPoints(box_pict, rvec, tvec, camMatrx, dist)[0][:, 0].astype(int)\n",
"img = overlaying(logo, img, pts_pict, imgpts[1:])\n",
"px.imshow(img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12fa21fb",
"metadata": {},
"outputs": [],
"source": [
"# df.to_csv('karussel_88kps.csv')"
]
}
],
"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
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More