{ "cells": [ { "cell_type": "code", "execution_count": 45, "id": "8ed070be-f7e8-49dd-bc82-41bbf94c2a31", "metadata": {}, "outputs": [], "source": [ "from glob import glob\n", "\n", "path = \"/workspace/Archives/Training/*.json\"\n", "\n", "datasetFiles = sorted(glob(path))" ] }, { "cell_type": "code", "execution_count": 46, "id": "8f2aff86-c2dc-438c-95ea-badebbd02693", "metadata": {}, "outputs": [], "source": [ "from pascal_voc_writer import Writer\n", "from tqdm import tqdm\n", "import json\n", "\n", "for file in tqdm(datasetFiles):\n", " try:\n", " fileLoad = open(path+file, \"r\", encoding=\"utf8\")\n", " fileLoad = json.load(fileLoad)\n", " except:\n", " print(file)\n", " \n", " for num in range(len(fileLoad['images'])):\n", " fileName = str(fileLoad['images'][num]['file_name']).split(\".\")[0]\n", " \n", " image_w = fileLoad['images'][num]['width']\n", " image_h = fileLoad['images'][num]['height']\n", " \n", " writer = Writer(database='X-ray_multi_object_recognition_data', \n", " path=fileLoad['images'][num]['file_name'], \n", " width=image_w, height=image_h, \n", " depth=3, segmented=len(fileLoad['annotations']))\n", " \n", " for objectNum in range(len(fileLoad['annotations'])):\n", " x, y, w, h = fileLoad['annotations'][objectNum]['bbox']\n", " label = fileLoad['categories'][objectNum]['name']\n", " \n", " writer.addObject(label, x, y, x+w, y+h)\n", " \n", " writer.save(f'{fileName}.xml')" ] }, { "cell_type": "code", "execution_count": 47, "id": "7139d6cc-1e86-4797-8bf4-23ee33836259", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'images': [{'id': 0,\n", " 'file_name': 'E3S690_20220810_012120_S_Pistol_002-001_1.png',\n", " 'angle': 0,\n", " 'height': 760,\n", " 'width': 896}],\n", " 'annotations': [{'id': 0,\n", " 'image_id': 0,\n", " 'iscrowd': 1,\n", " 'category_id': 1,\n", " 'bbox': [365.0, 222.0, 130.0, 232.0],\n", " 'area': 13110.0,\n", " 'segmentation': {'size': [760, 896],\n", " 'counts': '_U_89mf0c0]Oc0E9O2N2000000001O01O01O001O01O01O00010O001O00010O00001O010O00001O01O01O00010O001O00010O00001O0100O1O100O3M4M>A>C1N2N1O2N2M^Og[OhMXd0U2m[OiMSd0V2P\\\\OhMoc0X2R\\\\OiMlc0X2U\\\\OgMic0[2W\\\\OfMgc0[2Y\\\\OeMec0\\\\2]\\\\OcMac0_2_\\\\ObMXc0f2h\\\\OZMQc0m2n\\\\OTMRc0l2n\\\\OTMRc0m2l\\\\OTMSc0m2m\\\\OSMTc0l2k\\\\OUMUc0l2i\\\\OUMWc0n2`\\\\OXM`c0a3100023O5H8F:F5KO000006JB?A`0\\\\Od0\\\\Od0_O9G2N2N2N[VY9'}}],\n", " 'categories': [{'id': 1, 'name': 'Pistol', 'supercategory': None}],\n", " 'meta': [{'object_id': '002-001',\n", " 'material_outside': 'X',\n", " 'material_inside': 'Plastic',\n", " 'size': '24',\n", " 'unit': 'cm'}]}" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path = \"/workspace/Archives/Training/E3S690_20220810_012120_S_Pistol_002-001_1.json\"\n", "\n", "fileLoad = open(path, \"r\", encoding=\"utf8\")\n", "json.load(fileLoad)" ] } ], "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.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }