{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import csv\nimport numpy as np\nimport json\nimport ast\nimport pandas as pd\nimport datetime","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:20.089012Z","iopub.execute_input":"2021-05-25T11:21:20.089502Z","iopub.status.idle":"2021-05-25T11:21:20.094743Z","shell.execute_reply.started":"2021-05-25T11:21:20.089458Z","shell.execute_reply":"2021-05-25T11:21:20.093755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ts = pd.read_csv('../input/split-all/meta.csv')\nts.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:21.767949Z","iopub.execute_input":"2021-05-25T11:21:21.768464Z","iopub.status.idle":"2021-05-25T11:21:21.801139Z","shell.execute_reply.started":"2021-05-25T11:21:21.768433Z","shell.execute_reply":"2021-05-25T11:21:21.800458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst = ts[ts['split']=='test']\ntst.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:23.983813Z","iopub.execute_input":"2021-05-25T11:21:23.984361Z","iopub.status.idle":"2021-05-25T11:21:23.998176Z","shell.execute_reply.started":"2021-05-25T11:21:23.984323Z","shell.execute_reply":"2021-05-25T11:21:23.997155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Labels for classification\n# Метки для классификации\nlabels = {\n0:\"Negative for Pneumonia\",\n1:\"Typical Appearance\",\n2:\"Indeterminate Appearance\",\n3:\"Atypical Appearance\"}","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:28.732223Z","iopub.execute_input":"2021-05-25T11:21:28.732599Z","iopub.status.idle":"2021-05-25T11:21:28.737337Z","shell.execute_reply.started":"2021-05-25T11:21:28.732564Z","shell.execute_reply":"2021-05-25T11:21:28.736123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"now = datetime.datetime.now()\n\ndata = dict(\n    images=[\n        # license, url, file_name, height, width, date_captured, id\n    ],\n    type='instances',\n    annotations=[\n        # segmentation, area, iscrowd, image_id, bbox, category_id, id\n    ],\n    categories=[\n        # supercategory, id, name\n    ],\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:31.013370Z","iopub.execute_input":"2021-05-25T11:21:31.013717Z","iopub.status.idle":"2021-05-25T11:21:31.018859Z","shell.execute_reply.started":"2021-05-25T11:21:31.013684Z","shell.execute_reply":"2021-05-25T11:21:31.017750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_name_to_id = {}\nfor i, each_label in enumerate(labels):\n    class_name = each_label\n    class_id = i\n    class_name_to_id[class_name] = class_id\n    data['categories'].append(dict(\n        supercategory=None,\n        id=class_id,\n        name=str(class_name),\n    ))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:33.332636Z","iopub.execute_input":"2021-05-25T11:21:33.332973Z","iopub.status.idle":"2021-05-25T11:21:33.337896Z","shell.execute_reply.started":"2021-05-25T11:21:33.332945Z","shell.execute_reply":"2021-05-25T11:21:33.336981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:35.597496Z","iopub.execute_input":"2021-05-25T11:21:35.597883Z","iopub.status.idle":"2021-05-25T11:21:35.603934Z","shell.execute_reply.started":"2021-05-25T11:21:35.597849Z","shell.execute_reply":"2021-05-25T11:21:35.603124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_out_file = './test_annotations.json'\ndata_test = data.copy()\ndata_test['images'] = []\ndata_test['annotations'] = []","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:21:39.012797Z","iopub.execute_input":"2021-05-25T11:21:39.013134Z","iopub.status.idle":"2021-05-25T11:21:39.017235Z","shell.execute_reply.started":"2021-05-25T11:21:39.013105Z","shell.execute_reply":"2021-05-25T11:21:39.016477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor idx in range(len(tst)):\n    image_id = tst.iloc[idx].image_id\n    #class_id = tr.iloc[idx].label_encoded\n    width = int(tst.iloc[idx].dim1)\n    height = int(tst.iloc[idx].dim0)\n    data_test['images'].append(dict(\n            file_name = image_id+'.jpg',\n            width = width,\n            height = height,\n            date_captured=None,\n            id=int(idx)))\n    \n#     with open(train_out_file, 'w') as f:\n#         json.dump(data_train, f, indent=4)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:22:09.039632Z","iopub.execute_input":"2021-05-25T11:22:09.040012Z","iopub.status.idle":"2021-05-25T11:22:09.549163Z","shell.execute_reply.started":"2021-05-25T11:22:09.039980Z","shell.execute_reply":"2021-05-25T11:22:09.548138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def myconverter(obj):\n    if isinstance(obj, np.integer):\n        return int(obj)\n    elif isinstance(obj, np.floating):\n        return float(obj)\n    elif isinstance(obj, np.ndarray):\n        return obj.tolist()\n    elif isinstance(obj, datetime.datetime):\n        return obj.__str__()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:22:24.445601Z","iopub.execute_input":"2021-05-25T11:22:24.445937Z","iopub.status.idle":"2021-05-25T11:22:24.451798Z","shell.execute_reply.started":"2021-05-25T11:22:24.445906Z","shell.execute_reply":"2021-05-25T11:22:24.450689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(test_out_file, 'w') as f:\n    json.dump(data_test, f, default=myconverter)#indent=4)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:22:26.942021Z","iopub.execute_input":"2021-05-25T11:22:26.942415Z","iopub.status.idle":"2021-05-25T11:22:26.967699Z","shell.execute_reply.started":"2021-05-25T11:22:26.942378Z","shell.execute_reply":"2021-05-25T11:22:26.966873Z"},"trusted":true},"execution_count":null,"outputs":[]}]}