{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":24800,"datasetId":1042002,"databundleVersionId":1831594}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:41:20.47469Z","iopub.execute_input":"2026-02-24T17:41:20.474913Z","iopub.status.idle":"2026-02-24T17:41:41.645231Z","shell.execute_reply.started":"2026-02-24T17:41:20.47489Z","shell.execute_reply":"2026-02-24T17:41:41.644379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Kaggle dataset path\nBASE_PATH = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection\"\nTRAIN_DICOM_PATH = os.path.join(BASE_PATH, \"train\")\nCSV_PATH = os.path.join(BASE_PATH, \"train.csv\")\n\n# Working directory\nWORK_PATH = \"/kaggle/working/CliniScan\"\nIMAGE_SAVE_PATH = os.path.join(WORK_PATH, \"images\")\nLABEL_SAVE_PATH = os.path.join(WORK_PATH, \"labels\")\n\nos.makedirs(IMAGE_SAVE_PATH, exist_ok=True)\nos.makedirs(LABEL_SAVE_PATH, exist_ok=True)\n\nprint(\"Working directory created.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:41:41.64662Z","iopub.execute_input":"2026-02-24T17:41:41.647018Z","iopub.status.idle":"2026-02-24T17:41:41.652779Z","shell.execute_reply.started":"2026-02-24T17:41:41.646993Z","shell.execute_reply":"2026-02-24T17:41:41.652016Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Convert DICOM to JPG","metadata":{}},{"cell_type":"code","source":"import pydicom\nimport cv2\nimport numpy as np\nfrom tqdm import tqdm\n\nimage_ids = [f.replace(\".dicom\", \"\") for f in os.listdir(TRAIN_DICOM_PATH)]\n\nfor image_id in tqdm(image_ids):\n    \n    dicom_path = os.path.join(TRAIN_DICOM_PATH, image_id + \".dicom\")\n    save_path = os.path.join(IMAGE_SAVE_PATH, image_id + \".jpg\")\n    \n    try:\n        dicom = pydicom.dcmread(dicom_path)\n        image = dicom.pixel_array.astype(np.float32)\n        \n        # Normalize to 0–255\n        image = (image - image.min()) / (image.max() - image.min())\n        image = (image * 255).astype(np.uint8)\n        \n        # Resize to 1024×1024\n        image = cv2.resize(image, (1024, 1024))\n        \n        # Save as JPG\n        cv2.imwrite(save_path, image, [cv2.IMWRITE_JPEG_QUALITY, 95])\n        \n    except Exception as e:\n        print(\"Error:\", image_id)\n\nprint(\"DICOM → JPG conversion completed.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:41:41.653773Z","iopub.execute_input":"2026-02-24T17:41:41.654095Z","iopub.status.idle":"2026-02-24T21:39:14.824717Z","shell.execute_reply.started":"2026-02-24T17:41:41.654062Z","shell.execute_reply":"2026-02-24T21:39:14.824062Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load CSV","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv(CSV_PATH)\n\nprint(\"CSV Loaded. Total Rows:\", len(df))\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:14.826273Z","iopub.execute_input":"2026-02-24T21:39:14.826531Z","iopub.status.idle":"2026-02-24T21:39:14.952732Z","shell.execute_reply.started":"2026-02-24T21:39:14.826509Z","shell.execute_reply":"2026-02-24T21:39:14.952165Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Remove \"No finding\"","metadata":{}},{"cell_type":"code","source":"df_filtered = df[df[\"class_name\"] != \"No finding\"]\n\nprint(\"After removing 'No finding':\", len(df_filtered))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:14.953575Z","iopub.execute_input":"2026-02-24T21:39:14.953853Z","iopub.status.idle":"2026-02-24T21:39:14.969066Z","shell.execute_reply.started":"2026-02-24T21:39:14.953827Z","shell.execute_reply":"2026-02-24T21:39:14.968428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm\n\n# Group by image_id\ngrouped = df_filtered.groupby(\"image_id\")\n\nfor image_id, group in tqdm(grouped):\n    \n    label_file_path = os.path.join(LABEL_SAVE_PATH, image_id + \".txt\")\n    \n    # Image size after resizing\n    img_w, img_h = 1024, 1024\n    \n    with open(label_file_path, \"w\") as f:\n        \n        for _, row in group.iterrows():\n            \n            class_id = int(row[\"class_id\"])\n            \n            x_min = row[\"x_min\"]\n            y_min = row[\"y_min\"]\n            x_max = row[\"x_max\"]\n            y_max = row[\"y_max\"]\n            \n            # Convert to YOLO format\n            x_center = ((x_min + x_max) / 2) / img_w\n            y_center = ((y_min + y_max) / 2) / img_h\n            width = (x_max - x_min) / img_w\n            height = (y_max - y_min) / img_h\n            \n            f.write(f\"{class_id} {x_center} {y_center} {width} {height}\\n\")\n\nprint(\"CSV → YOLO conversion completed.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:14.969938Z","iopub.execute_input":"2026-02-24T21:39:14.970212Z","iopub.status.idle":"2026-02-24T21:39:17.579979Z","shell.execute_reply.started":"2026-02-24T21:39:14.970191Z","shell.execute_reply":"2026-02-24T21:39:17.579058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_images = set(df[\"image_id\"].unique())\nlabeled_images = set(df_filtered[\"image_id\"].unique())\n\nnormal_images = all_images - labeled_images\n\nfor image_id in normal_images:\n    open(os.path.join(LABEL_SAVE_PATH, image_id + \".txt\"), \"w\").close()\n\nprint(\"Empty label files created for normal images.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:17.581151Z","iopub.execute_input":"2026-02-24T21:39:17.581646Z","iopub.status.idle":"2026-02-24T21:39:17.901913Z","shell.execute_reply.started":"2026-02-24T21:39:17.581622Z","shell.execute_reply":"2026-02-24T21:39:17.90127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Total JPG Images:\", len(os.listdir(IMAGE_SAVE_PATH)))\nprint(\"Total Label Files:\", len(os.listdir(LABEL_SAVE_PATH)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:17.902854Z","iopub.execute_input":"2026-02-24T21:39:17.903439Z","iopub.status.idle":"2026-02-24T21:39:17.925653Z","shell.execute_reply.started":"2026-02-24T21:39:17.9034Z","shell.execute_reply":"2026-02-24T21:39:17.925095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!du -sh /kaggle/working/CliniScan","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:17.926519Z","iopub.execute_input":"2026-02-24T21:39:17.926814Z","iopub.status.idle":"2026-02-24T21:39:18.183789Z","shell.execute_reply.started":"2026-02-24T21:39:17.926782Z","shell.execute_reply":"2026-02-24T21:39:18.182993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r CliniScan_images.zip /kaggle/working/CliniScan/images\n!zip -r CliniScan_labels.zip /kaggle/working/CliniScan/labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T21:39:18.186118Z","iopub.execute_input":"2026-02-24T21:39:18.186351Z"}},"outputs":[],"execution_count":null}]}