{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import numpy as np\n", "import zipfile\n", "import requests\n", "import jsonlines\n", "from tqdm import tqdm\n", "from pathlib import Path\n", "from pycocotools.coco import COCO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Download Annotations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url = 'http://images.cocodataset.org/annotations/'\n", "files = [\n", " 'annotations_trainval2017.zip'\n", "]\n", "for file in files:\n", " if not Path(f'./{file}').exists():\n", " response = requests.get(url + file)\n", " with open(file, 'wb') as f:\n", " f.write(response.content)\n", "\n", " with zipfile.ZipFile(file, 'r') as zipf:\n", " zipf.extractall(Path())\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Keypoint Detection Task" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_data = COCO('annotations/person_keypoints_train2017.json')\n", "val_data = COCO('annotations/person_keypoints_val2017.json')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for split, data in zip(['train', 'validation'], [train_data, val_data]):\n", " with jsonlines.open(f'data/keypoints_{split}.jsonl', mode='w') as writer:\n", " for image_id, image_info in tqdm(data.imgs.items()):\n", " bboxes, keypoints = [], []\n", " anns = data.imgToAnns[image_id]\n", " if len(anns) > 0:\n", " \n", " for ann in anns:\n", " bboxes.append(ann['bbox'])\n", " keypoints.append(ann['keypoints'])\n", " writer.write({\n", " 'image': image_info['file_name'],\n", " 'bboxes': bboxes,\n", " 'keypoints': np.array(keypoints).reshape(\n", " len(bboxes), -1, 3\n", " ).tolist()\n", " })" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for split in ['train', 'validation']:\n", " file_path = f'data/keypoints_{split}.jsonl'\n", " with zipfile.ZipFile(f'data/keypoints_{split}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:\n", " zipf.write(file_path, os.path.basename(file_path))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }