{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "283f6e9c", "metadata": {}, "outputs": [], "source": [ "import os\n", "import numpy as np\n", "import pandas as pd\n", "import glob\n", "import json\n", "from tqdm.notebook import tqdm\n", "\n", "path_dataset = r'D:\\downloads\\Nurburg-karussel'\n", "with open(os.path.join(path_dataset, 'meta.json'), 'r') as j:\n", " meta = json.load(j)\n", "\n", "imgs = glob.glob(path_dataset + '\\\\images\\\\img\\\\*', recursive=True)\n", "anns = glob.glob(path_dataset + '\\\\images\\\\ann\\\\*', recursive=True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "7af948d4", "metadata": {}, "outputs": [], "source": [ "def label2hash(meta_json, last):\n", " for clss in meta_json['classes']:\n", " if clss['title'] == last['classTitle']:\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" ] }, { "cell_type": "code", "execution_count": 4, "id": "7ea0771e", "metadata": {}, "outputs": [], "source": [ "def annotations(meta_json, obj):\n", " nodes = obj['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, obj)[str(i)]]['loc']\n", "\n", " keypoints_2d['v'] = 2\n", " keypoints_2d = keypoints_2d.astype(float).round().astype(int)\n", " return keypoints_2d[:24]" ] }, { "cell_type": "code", "execution_count": 5, "id": "26b6abf4", "metadata": {}, "outputs": [], "source": [ "def ann_json(keypoints, img_id, obj):\n", " \n", " annotation = {\n", " \"id\": obj['id'],\n", " \"segmentation\": [],\n", " \"num_keypoints\": len(keypoints),\n", " \"area\": 0,\n", " \"iscrowd\": 0,\n", " \"image_id\": img_id,\n", " \"bbox\": [],\n", " \"category_id\": 1,\n", " \"keypoints\": keypoints.values.flatten().tolist()}\n", "\n", " return annotation\n", "\n", "def img_json(ann, name, id):\n", " height, width = ann['size'].values()\n", " image = {\n", " \"id\": id,\n", " \"width\": width,\n", " \"height\": height,\n", " \"file_name\": name,\n", " }\n", " return image" ] }, { "cell_type": "code", "execution_count": 6, "id": "155be42d", "metadata": {}, "outputs": [], "source": [ "def ann_img_list(anns, imgs, meta):\n", " annotations_list = []\n", " image_list = []\n", " for i in tqdm(range(len(anns))):\n", "\n", " with open(anns[i], 'r') as j:\n", " ann = json.load(j)\n", " \n", " image_name = os.path.basename(anns[i])[:-5]\n", " image = img_json(ann, image_name, i)\n", " image_list.append(image)\n", "\n", " for obj in ann['objects']:\n", " keypoints = annotations(meta, obj)\n", " annotations_list.append(ann_json(keypoints, i, obj))\n", " return image_list, annotations_list" ] }, { "cell_type": "code", "execution_count": 7, "id": "6593f677", "metadata": {}, "outputs": [], "source": [ "def COCO(image_list, annotations_list):\n", " coco = {\n", "\n", " \"info\": {\n", " \"description\": \"karusel Dataset\", \"version\": \"2.0\"\n", " },\n", "\n", " \"categories\": [\n", " {\n", " \"supercategory\": \"NurburgRing\",\n", " \"id\": 1,\n", " \"name\": \"karusel\",\n", " \"keypoints\": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\n", " 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],\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", " coco['images'] = image_list\n", " coco['annotations'] = annotations_list\n", " return coco" ] }, { "cell_type": "code", "execution_count": 8, "id": "38880d13", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "96021bfc91ce425dbef57ae348321005", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/448 [00:00