File size: 4,059 Bytes
87a14dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
 "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:F5KO000006J<D;E<C7I010O1N2M3L4N20000000aIW_Oo5m`0iIY_OU6Ua001O0002111J4M1RJa^Oa5_a0_Jo^OS5Ra0kJ]_Og4d`0XKj_OY4W`0gKW@k3Yb0B>B?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
}