387 lines
12 KiB
Plaintext
387 lines
12 KiB
Plaintext
{
|
|
"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
|
|
}
|