{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Let's be Fresh","metadata":{}},{"cell_type":"code","source":"! rm '/kaggle/working/' -r","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:22:00.658771Z","iopub.execute_input":"2024-02-11T16:22:00.659259Z","iopub.status.idle":"2024-02-11T16:22:02.073074Z","shell.execute_reply.started":"2024-02-11T16:22:00.659222Z","shell.execute_reply":"2024-02-11T16:22:02.071402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Here","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom collections import Counter\nfrom decimal import Decimal\nimport random\nfrom PIL import Image\nimport shutil\nimport json","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:21:15.242732Z","iopub.execute_input":"2024-02-11T16:21:15.243240Z","iopub.status.idle":"2024-02-11T16:21:15.250732Z","shell.execute_reply.started":"2024-02-11T16:21:15.243201Z","shell.execute_reply":"2024-02-11T16:21:15.249124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Support From Here","metadata":{}},{"cell_type":"code","source":"def yolo_to_coco(image_paths, yolo_labels_paths, coco_output_path, class_names, noannotation = 0):\n    coco_data = {\n        \"info\": {},\n        \"licenses\": [],\n        \"images\": [],\n        \"annotations\": [],\n        \"categories\": []\n    }\n    for class_id, class_name in class_names.items():\n        category = {\"id\": class_id, \"name\": class_name, \"supercategory\": \"none\"}\n        coco_data[\"categories\"].append(category)\n    for image_path in image_paths:\n        image = Image.open(image_path)\n        image_width, image_height = image.size\n        coco_image = {\n            \"id\": len(coco_data[\"images\"]) + 1,\n            \"width\": image_width,\n            \"height\": image_height,\n            \"file_name\": image_path.split(\"/\")[-1],\n            \"license\": 1,\n            \"flickr_url\": \"\",\n            \"coco_url\": \"\",\n            \"date_captured\": \"\"\n        }\n\n        coco_data[\"images\"].append(coco_image)\n    if noannotation == 1:\n        with open(coco_output_path, 'w') as json_file:\n            json.dump(coco_data, json_file, indent=2)\n        return\n    for image_path, yolo_labels_path in zip(image_paths, yolo_labels_paths):\n        try:\n            with open(yolo_labels_path, 'r') as f:\n                yolo_labels = f.readlines()\n\n            \n            for yolo_label in yolo_labels:\n                yolo_label = yolo_label.strip().split()\n                class_id, x_center, y_center, width, height = map(float, yolo_label)\n\n                x_min = max(0, int((x_center - width / 2) * image_width))\n                y_min = max(0, int((y_center - height / 2) * image_height))\n                x_max = min(image_width, int((x_center + width / 2) * image_width))\n                y_max = min(image_height, int((y_center + height / 2) * image_height))\n                annotation = {\n                    \"id\": len(coco_data[\"annotations\"]) + 1,\n                    \"image_id\": coco_image[\"id\"],\n                    \"category_id\": int(class_id),\n                    \"bbox\": [x_min, y_min, x_max - x_min, y_max - y_min],\n                    \"area\": (x_max - x_min) * (y_max - y_min),\n                    \"iscrowd\": 0,\n                    \"segmentation\": [],\n                    \"image_width\": image_width,\n                    \"image_height\": image_height\n                }\n\n                coco_data[\"annotations\"].append(annotation)\n\n        except FileNotFoundError:\n            continue\n\n    with open(coco_output_path, 'w') as json_file:\n        json.dump(coco_data, json_file, indent=2)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:19:15.028322Z","iopub.execute_input":"2024-02-11T16:19:15.029913Z","iopub.status.idle":"2024-02-11T16:19:15.055344Z","shell.execute_reply.started":"2024-02-11T16:19:15.029856Z","shell.execute_reply":"2024-02-11T16:19:15.053921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_list(path):\n    image_paths = []\n    for file in os.listdir(path):\n        image_paths.append(os.path.join(path, file))\n    return image_paths\ndef move_folder_contents(source_folder, destination_folder):\n    try:\n        os.makedirs(destination_folder, exist_ok=True)\n        shutil.rmtree(destination_folder, ignore_errors=True)\n        shutil.copytree(source_folder, destination_folder)\n    except Exception as e:\n        pass","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:19:15.058193Z","iopub.execute_input":"2024-02-11T16:19:15.059537Z","iopub.status.idle":"2024-02-11T16:19:15.090760Z","shell.execute_reply.started":"2024-02-11T16:19:15.059487Z","shell.execute_reply":"2024-02-11T16:19:15.088766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Change Here ","metadata":{}},{"cell_type":"code","source":"\nclass_names = {\n    0: \"auto_rickshaw\",\n    1: \"bicycle\",\n    2: \"bus\",\n    3: \"car\",\n    4: \"cart_vehicle\",\n    5: \"construction_vehicle\",\n    6: \"motorbike\",\n    7: \"person\",\n    8: \"priority_vehicle\",\n    9: \"three_wheeler\",\n    10: \"train\",\n    11: \"truck\",\n    12: \"wheelchair\"\n}\n\ntrain_image_path = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/train'\ntrain_label_path = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels/train'\ntest_image_path = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test'\n\ntest_coco_output_path = \"/kaggle/working/dataset/test/_annotations.coco.json\"\ntrain_coco_output_path = \"/kaggle/working/dataset/train/_annotations.coco.json\"\n\n#As There is No Validation Set Given\n# val_image_path = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/val'\n# val_label_path = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels/val'\n# val_coco_output_path = \"/kaggle/working/dataset/val/_annotations.coco.json\"\n","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:19:31.293832Z","iopub.execute_input":"2024-02-11T16:19:31.294315Z","iopub.status.idle":"2024-02-11T16:19:31.302440Z","shell.execute_reply.started":"2024-02-11T16:19:31.294277Z","shell.execute_reply":"2024-02-11T16:19:31.301206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Here","metadata":{}},{"cell_type":"code","source":"move_folder_contents(train_image_path, '/kaggle/working/dataset/train')\nmove_folder_contents(test_image_path, '/kaggle/working/dataset/test')\n# move_folder_contents(val_image_path, '/kaggle/working/dataset/val')","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:22:15.575294Z","iopub.execute_input":"2024-02-11T16:22:15.576799Z","iopub.status.idle":"2024-02-11T16:22:54.611831Z","shell.execute_reply.started":"2024-02-11T16:22:15.576747Z","shell.execute_reply":"2024-02-11T16:22:54.610912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yolo_to_coco(create_list(train_image_path), create_list(train_label_path), train_coco_output_path, class_names)\nprint('Train Set Image Created!')\n\n\nyolo_to_coco(create_list(test_image_path),None, test_coco_output_path, class_names, noannotation=1)\nprint('Test Set Image Created!')\n\n#As There is No Validation Set Given\n# yolo_to_coco(create_list(val_image_path), create_list(val_label_path), val_coco_output_path, class_names)\n# print('Validation Set Image Created!')","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:23:01.832771Z","iopub.execute_input":"2024-02-11T16:23:01.833258Z","iopub.status.idle":"2024-02-11T16:24:00.241990Z","shell.execute_reply.started":"2024-02-11T16:23:01.833227Z","shell.execute_reply":"2024-02-11T16:24:00.240971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Here","metadata":{}},{"cell_type":"code","source":"def analyze_coco_data(json_file_path):\n    with open(json_file_path, 'r') as json_file:\n        coco_data = json.load(json_file)\n\n    images = coco_data.get('images', [])\n    categories = coco_data.get('categories', [])\n    annotations = coco_data.get('annotations', [])\n\n    if images:\n        first_image_file_name = images[0].get('file_name', 'N/A')\n        print(f\"First image file name: {first_image_file_name}\")\n\n    if categories:\n        category_names = [category.get('name', 'N/A') for category in categories]\n        print(f\"All category names: {', '.join(category_names)}\")\n\n    num_annotations = len(annotations)\n    print(f\"Number of annotations: {num_annotations}\")\n\nprint('Analyze Train Data Json')\nanalyze_coco_data(train_coco_output_path)\n\nprint('Analyze Test Data Json')\nanalyze_coco_data(test_coco_output_path)\n\n#As There is No Validation Set Given\n# print('Analyze Validation Data Json')\n# analyze_coco_data(val_coco_output_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T16:24:00.243785Z","iopub.execute_input":"2024-02-11T16:24:00.244694Z","iopub.status.idle":"2024-02-11T16:24:00.764715Z","shell.execute_reply.started":"2024-02-11T16:24:00.244656Z","shell.execute_reply":"2024-02-11T16:24:00.763809Z"},"trusted":true},"execution_count":null,"outputs":[]}]}