{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"*This Notebook opens DICOM the train set, provides a view into the CSV, applies the annotations to the images, and visualizes it\n","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pydicom\nfrom PIL import Image, ImageDraw\n\n# File paths\ncsv_path = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'\nimage_dir = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images'\n\n# Read CSV file\ndf = pd.read_csv(csv_path)\n\n# Get the first 10 image files\nimage_files = os.listdir(image_dir)[:5]\n\n# Function to read DICOM image\ndef read_dicom_image(file_path):\n    dicom = pydicom.dcmread(file_path)\n    image = dicom.pixel_array\n    return Image.fromarray(np.uint8(image))\n\n# Function to draw bounding box\ndef draw_bounding_box(image, x, y, width, height):\n    draw = ImageDraw.Draw(image)\n    draw.rectangle([x, y, x + width, y + height], outline=\"red\", width=2)\n    return image\n\n# Process images\nfor img_file in image_files:\n    patient_id = img_file.split('.')[0]\n    img_path = os.path.join(image_dir, img_file)\n    \n    # Read DICOM image\n    img = read_dicom_image(img_path)\n    \n    # Find matching row in CSV\n    row = df[df['patientId'] == patient_id]\n    \n    # If there's a match and it's a positive case (Target == 1)\n    if not row.empty and row['Target'].values[0] == 1:\n        x, y, width, height = row[['x', 'y', 'width', 'height']].values[0]\n        \n        # Draw bounding box\n        img_with_box = draw_bounding_box(img, x, y, width, height)\n        \n        # Display the image\n        plt.figure(figsize=(10, 10))\n        plt.imshow(img_with_box, cmap='gray')\n        plt.title(f\"Patient ID: {patient_id}\")\n        plt.axis('off')\n        plt.show()\n    else:\n        # Display the image without bounding box\n        plt.figure(figsize=(10, 10))\n        plt.imshow(img, cmap='gray')\n        plt.title(f\"Patient ID: {patient_id} (No pneumonia detected)\")\n        plt.axis('off')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T01:06:05.508514Z","iopub.execute_input":"2024-09-10T01:06:05.509544Z","iopub.status.idle":"2024-09-10T01:06:07.981537Z","shell.execute_reply.started":"2024-09-10T01:06:05.509494Z","shell.execute_reply":"2024-09-10T01:06:07.980455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pydicom\nfrom PIL import Image, ImageDraw\n\n# File paths\ncsv_path = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'\nimage_dir = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images'\n\n# Read CSV file\ndf = pd.read_csv(csv_path)\n\n# Get the first 10 image files\nimage_files = os.listdir(image_dir)[:10]\n\n# Function to read DICOM image\ndef read_dicom_image(file_path):\n    dicom = pydicom.dcmread(file_path)\n    image = dicom.pixel_array\n    return Image.fromarray(np.uint8(image))\n\n# Function to draw bounding box\ndef draw_bounding_box(image, x, y, width, height):\n    draw = ImageDraw.Draw(image)\n    draw.rectangle([x, y, x + width, y + height], outline=\"red\", width=2)\n    return image\n\n# Process images\nfor img_file in image_files:\n    patient_id = img_file.split('.')[0]\n    img_path = os.path.join(image_dir, img_file)\n    \n    # Read DICOM image\n    img = read_dicom_image(img_path)\n    \n    # Find matching rows in CSV\n    rows = df[df['patientId'] == patient_id]\n    \n    # Print the rows for this patient ID\n    print(f\"\\nPatient ID: {patient_id}\")\n    print(rows)\n    \n    # If there's a match and it's a positive case (Target == 1)\n    if not rows.empty and (rows['Target'] == 1).any():\n        # There might be multiple boxes for one image, so we'll draw all of them\n        for _, row in rows[rows['Target'] == 1].iterrows():\n            x, y, width, height = row[['x', 'y', 'width', 'height']]\n            \n            # Draw bounding box\n            img = draw_bounding_box(img, x, y, width, height)\n        \n        # Display the image\n        plt.figure(figsize=(10, 10))\n        plt.imshow(img, cmap='gray')\n        plt.title(f\"Patient ID: {patient_id} (Pneumonia detected)\")\n        plt.axis('off')\n        plt.show()\n    else:\n        # Display the image without bounding box\n        plt.figure(figsize=(10, 10))\n        plt.imshow(img, cmap='gray')\n        plt.title(f\"Patient ID: {patient_id} (No pneumonia detected)\")\n        plt.axis('off')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T01:02:32.369220Z","iopub.execute_input":"2024-09-10T01:02:32.370315Z","iopub.status.idle":"2024-09-10T01:02:37.314095Z","shell.execute_reply.started":"2024-09-10T01:02:32.370244Z","shell.execute_reply":"2024-09-10T01:02:37.312814Z"},"trusted":true},"execution_count":null,"outputs":[]}]}