{"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"},{"sourceId":7541469,"sourceType":"datasetVersion","datasetId":4391569},{"sourceId":163242302,"sourceType":"kernelVersion"},{"sourceId":164033832,"sourceType":"kernelVersion"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The `BadODD` and `RSUD20K` both provide object detection dataset on the same enironment. The class labels are almost same, that makes possibility to merge them. \n\n- [BadODD dataset](https://www.kaggle.com/competitions/dl-enigma-10-sust-cse-carnival-2024)\n- [BadOOD: Create Data Frame](https://www.kaggle.com/code/ipythonx/badodd-create-data-frame/notebook)\n- [RSUD20k dataset](https://www.kaggle.com/datasets/hasibzunair/rsud20k-bangladesh-road-scene-understanding) dataset\n- [RSUD20K: Create Data Frame](https://www.kaggle.com/code/ipythonx/rsud20k-create-data-frame)\n- [RSUD20K: Modelling with YOLOv8](https://www.kaggle.com/code/ipythonx/rsud20k-modelling-with-yolov8)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nimport colorsys","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:07:44.705794Z","iopub.execute_input":"2024-02-23T18:07:44.706242Z","iopub.status.idle":"2024-02-23T18:07:44.711742Z","shell.execute_reply.started":"2024-02-23T18:07:44.706207Z","shell.execute_reply":"2024-02-23T18:07:44.710508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsud_train_df  = pd.read_csv('/kaggle/input/rsud20k-create-data-frame/train.csv')\nbadod_train_df = pd.read_csv('/kaggle/input/badodd-create-data-frame/train.csv')\n\nrsud_train_df.shape, badod_train_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:58.018631Z","iopub.execute_input":"2024-02-23T18:06:58.019599Z","iopub.status.idle":"2024-02-23T18:06:59.291308Z","shell.execute_reply.started":"2024-02-23T18:06:58.019537Z","shell.execute_reply":"2024-02-23T18:06:59.289800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_root = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD'\nbadod_train_df['image_path'] = badod_train_df['image_id'].apply(\n    lambda x: f'{data_root}/images/train/{x}' + '.jpg'\n)\n\ndata_root = '/kaggle/input/rsud20k-bangladesh-road-scene-understanding/rsud20k'\nrsud_train_df['image_path'] = rsud_train_df['image_id'].apply(\n    lambda x: f'{data_root}/images/train/{x}' + '.jpg'\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.293196Z","iopub.execute_input":"2024-02-23T18:06:59.293959Z","iopub.status.idle":"2024-02-23T18:06:59.426878Z","shell.execute_reply.started":"2024-02-23T18:06:59.293891Z","shell.execute_reply":"2024-02-23T18:06:59.425724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unified_dataset = pd.concat([badod_train_df, rsud_train_df], ignore_index=True)\nunified_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.429962Z","iopub.execute_input":"2024-02-23T18:06:59.431158Z","iopub.status.idle":"2024-02-23T18:06:59.470147Z","shell.execute_reply.started":"2024-02-23T18:06:59.431105Z","shell.execute_reply":"2024-02-23T18:06:59.468756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unified_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.472055Z","iopub.execute_input":"2024-02-23T18:06:59.472507Z","iopub.status.idle":"2024-02-23T18:06:59.497322Z","shell.execute_reply.started":"2024-02-23T18:06:59.472464Z","shell.execute_reply":"2024-02-23T18:06:59.494365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Update labels","metadata":{}},{"cell_type":"code","source":"unified_mapping = {\n    'person': 0,\n    'rickshaw': 1,\n    'rickshaw van': 2,\n    'auto rickshaw': 3,\n    'truck': 4,\n    'pickup truck': 5,\n    'private car': 6,\n    'motorcycle': 7,\n    'bicycle': 8,\n    'bus': 9,\n    'micro bus': 10,\n    'covered van': 11,\n    'human hauler': 12,\n    \n    # exit in one dataset and not second dataset\n    'cart vehicle': 13,\n    'construction vehicle': 14,\n    'priority vehicle': 15,\n    'three wheeler': 16,\n    'train': 17,\n    'wheelchair': 18\n}","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.502170Z","iopub.execute_input":"2024-02-23T18:06:59.502573Z","iopub.status.idle":"2024-02-23T18:06:59.511388Z","shell.execute_reply.started":"2024-02-23T18:06:59.502537Z","shell.execute_reply":"2024-02-23T18:06:59.508854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def map_class_label(class_string):\n    if class_string == 'motorbike':\n        return unified_mapping.get('motorcycle', -1)\n    \n    if class_string == 'car':\n        return unified_mapping.get('private car', -1)\n\n    return unified_mapping.get(class_string, -1)\n\nunified_dataset['unified_class_label'] = unified_dataset['class_name'].apply(map_class_label)","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.513682Z","iopub.execute_input":"2024-02-23T18:06:59.514721Z","iopub.status.idle":"2024-02-23T18:06:59.693580Z","shell.execute_reply.started":"2024-02-23T18:06:59.514646Z","shell.execute_reply":"2024-02-23T18:06:59.691680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = unified_dataset.class_name.unique().tolist()\ncl = unified_dataset.unified_class_label.unique().tolist()\nsorted(list(zip(cm, cl)), key=lambda x: x[1])","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.695271Z","iopub.execute_input":"2024-02-23T18:06:59.695637Z","iopub.status.idle":"2024-02-23T18:06:59.735120Z","shell.execute_reply.started":"2024-02-23T18:06:59.695606Z","shell.execute_reply":"2024-02-23T18:06:59.733969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"class_labels = unified_dataset.class_name.unique().tolist()\nthickness = 5\nfont_scale = 1.2\nfont_thickness = 2\n\ndef generate_colors(num_classes):\n    hsv_tuples = [(x / num_classes, 1., 1.) for x in range(num_classes)]\n    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))\n    colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))\n    return colors\n\ndef draw_boxes(image_path, df):\n    image = cv2.imread(image_path)\n    colors = generate_colors(len(class_labels))\n    class_color_map = {class_labels[i]: colors[i] for i in range(len(class_labels))}\n    \n    for _, row in df.iterrows():\n        box = eval(row['voc_bbox'])\n        class_name = row['class_name']\n        color = class_color_map[class_name]\n        box = [int(coord) for coord in box]\n        \n        cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), color, thickness)\n        cv2.putText(\n            image, \n            class_name, \n            (box[0], box[1] - 5), \n            cv2.FONT_HERSHEY_SIMPLEX, \n            font_scale, \n            color, \n            font_thickness\n        )\n\n    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.figure(figsize=(20,10))\n    plt.imshow(image_rgb)\n    plt.axis('off')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.737155Z","iopub.execute_input":"2024-02-23T18:06:59.737885Z","iopub.status.idle":"2024-02-23T18:06:59.772274Z","shell.execute_reply.started":"2024-02-23T18:06:59.737842Z","shell.execute_reply":"2024-02-23T18:06:59.771223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = unified_dataset.sample().reset_index()\nimage_path = sample_df.loc[0, 'image_path']\nimage_id = sample_df.loc[0, 'image_id']\ndf_image = unified_dataset[unified_dataset['image_id'] == image_id]\ndraw_boxes(image_path, df_image)","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:06:59.776100Z","iopub.execute_input":"2024-02-23T18:06:59.777152Z","iopub.status.idle":"2024-02-23T18:07:01.146475Z","shell.execute_reply.started":"2024-02-23T18:06:59.777103Z","shell.execute_reply":"2024-02-23T18:07:01.145157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = unified_dataset.sample().reset_index().loc[0, 'image_id']\nimage_path = '/kaggle/input/rsud20k-bangladesh-road-scene-understanding/rsud20k/images/train'\nimage_path = os.path.join(image_path, image_id + '.jpg')\ndf_image = unified_dataset[unified_dataset['image_id'] == image_id]\ndraw_boxes(image_path, df_image)","metadata":{"execution":{"iopub.status.busy":"2024-02-23T18:07:47.636003Z","iopub.execute_input":"2024-02-23T18:07:47.636515Z","iopub.status.idle":"2024-02-23T18:07:48.877700Z","shell.execute_reply.started":"2024-02-23T18:07:47.636478Z","shell.execute_reply":"2024-02-23T18:07:48.875933Z"},"trusted":true},"execution_count":null,"outputs":[]}]}