Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update annotation rescale notebook #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions notebooks/annotation_rescale.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "bbd4ddb3",
"metadata": {},
"outputs": [],
Expand All @@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "2990b3fe",
"metadata": {},
"outputs": [],
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "8fb0b90f",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -127,18 +127,25 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "a63042c5",
"metadata": {},
"outputs": [],
"source": [
"# create new XML file for the tracks\n",
"xml_page = etree.Element(\"annotations\")\n",
"etree.SubElement(xml_page, \"version\").text = \"1.1\"\n",
"xml_page.text = \"\\n\"\n",
"\n",
"# add version\n",
"xml_version = etree.SubElement(xml_page, \"version\")\n",
"xml_version.text = \"1.1\"\n",
"xml_version.tail = \"\\n\"\n",
"\n",
"# iterate over all tracks and store the bounding boxes\n",
"for track_id in annotated.keys():\n",
" xml_track = etree.Element(\"track\", id=str(track_id), label=\"Grevy\", source=\"manual\")\n",
" xml_track.text = \"\\n\"\n",
" xml_track.tail = \"\\n\"\n",
"\n",
" for frame_id in annotated[track_id].keys():\n",
" if frame_id == sorted(annotated[track_id].keys())[-1]:\n",
Expand All @@ -152,6 +159,7 @@
" ytl=f\"{annotated[track_id][frame_id][1]:.2f}\",\n",
" xbr=f\"{annotated[track_id][frame_id][2]:.2f}\",\n",
" ybr=f\"{annotated[track_id][frame_id][3]:.2f}\", z_order=\"0\")\n",
" xml_box.tail = \"\\n\"\n",
"\n",
" xml_track.append(xml_box)\n",
"\n",
Expand All @@ -164,14 +172,29 @@
"original_root = original_tree.getroot()\n",
"\n",
"# Extract the 'meta' element\n",
"meta = original_root.find('meta')\n",
"meta = original_root.find(\"meta\")\n",
"\n",
"# Update height & width\n",
"height = int(meta.find(\"task\").find(\"original_size\").find(\"height\").text) * scaling_factor\n",
"meta.find(\"task\").find(\"original_size\").find(\"height\").text = str(height)\n",
"\n",
"width = int(meta.find(\"task\").find(\"original_size\").find(\"width\").text) * scaling_factor\n",
"meta.find(\"task\").find(\"original_size\").find(\"width\").text = str(width)\n",
"\n",
"# Append 'meta' to the new XML document\n",
"# Note: 'meta' should be appended before the 'track' elements, manually check this if errors occur\n",
"xml_page.append(meta)\n",
"xml_document = etree.ElementTree(xml_page)\n",
"track = xml_page.find(\"track\")\n",
"parent = track.getparent()\n",
"\n",
"if parent is not None:\n",
" index = parent.index(track)\n",
" parent.insert(index, meta)\n",
"else:\n",
" xml_page.append(meta)\n",
"\n",
"# Write the new XML document to file\n",
"etree.indent(xml_page, space=' ', level=0)\n",
"xml_document = etree.ElementTree(xml_page)\n",
"xml_document.write(f\"{tracks_location}/tracks_.xml\", xml_declaration=True, pretty_print=True, encoding=\"utf-8\")"
]
},
Expand Down Expand Up @@ -199,7 +222,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "ml",
"display_name": "kabr",
"language": "python",
"name": "python3"
},
Expand Down