{"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":71549,"databundleVersionId":8561470,"sourceType":"competition"},{"sourceId":184645698,"sourceType":"kernelVersion"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pickle\n\n# Replace 'your_file.pkl' with the path to your pickle file\nwith open('/kaggle/input/rsna-dataset/meta_lab.pkl', 'rb') as file:\n    meta_lab = pickle.load(file)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:12:21.721076Z","iopub.execute_input":"2024-06-23T17:12:21.72157Z","iopub.status.idle":"2024-06-23T17:12:21.767316Z","shell.execute_reply.started":"2024-06-23T17:12:21.721524Z","shell.execute_reply":"2024-06-23T17:12:21.766305Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\nall_train_data = pd.read_csv('/kaggle/input/rsna-dataset/detailed_label.csv',index_col=0)\n\nall_train_data = all_train_data.dropna(subset=['series_id','instance_number','severe_level'])\n\nall_train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:12:21.769338Z","iopub.execute_input":"2024-06-23T17:12:21.769735Z","iopub.status.idle":"2024-06-23T17:12:22.365954Z","shell.execute_reply.started":"2024-06-23T17:12:21.769703Z","shell.execute_reply":"2024-06-23T17:12:22.36478Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"detailed_diagnose = all_train_data.detailed_diagnose.unique()\n\ndetailed_diagnose_dict = {}\n\nfor i,d in enumerate(detailed_diagnose):\n    detailed_diagnose_dict[d] = i\n    \nprint(detailed_diagnose_dict)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:12:22.367282Z","iopub.execute_input":"2024-06-23T17:12:22.368041Z","iopub.status.idle":"2024-06-23T17:12:22.380206Z","shell.execute_reply.started":"2024-06-23T17:12:22.368004Z","shell.execute_reply":"2024-06-23T17:12:22.379067Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport pydicom\nimport numpy as np\nimport os\nimport glob\nfrom tqdm import tqdm\nimport warnings\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\ndef plot_square_on_dcm(dicom_file_path, center_x, center_y, side_length):\n    # Read the DICOM file\n    dicom_data = pydicom.dcmread(dicom_file_path)\n\n    # Extract the image data\n    image_data = dicom_data.pixel_array\n\n    # Plot the DICOM image\n    plt.imshow(image_data, cmap=plt.cm.gray)\n    plt.colorbar()\n    plt.title('DICOM Image with Square')\n    plt.xlabel('X-axis')\n    plt.ylabel('Y-axis')\n    \n    # Calculate the corners of the square\n    half_side = side_length / 2\n    square_x = [center_x - half_side, center_x + half_side, center_x + half_side, center_x - half_side, center_x - half_side]\n    square_y = [center_y - half_side, center_y - half_side, center_y + half_side, center_y + half_side, center_y - half_side]\n\n    # Plot the square\n    plt.plot(square_x, square_y, 'r-')\n    #plt.scatter([center_x], [center_y], color='blue')  # Mark the center point\n\n    plt.show()\n    \ndef plot_image_with_boxes(image_path, yolo_bbox, img_width, img_height):\n    image = Image.open(image_path)\n    fig, ax = plt.subplots(1)\n    ax.imshow(image)\n\n    x_center, y_center, width, height = yolo_bbox\n    # Convert normalized coordinates to image coordinates\n    x_center *= img_width\n    y_center *= img_height\n    width *= img_width\n    height *= img_height\n    # Calculate the top-left corner of the bounding box\n    xmin = x_center - width / 2\n    ymin = y_center - height / 2\n    # Create a rectangle patch\n    rect = patches.Rectangle((xmin, ymin), width, height, linewidth=1, edgecolor='r', facecolor='none')\n    # Add the patch to the Axes\n    ax.add_patch(rect)\n    plt.show()\n    \ndef convert_dicom_to_jpg(dicom_path, output_path):\n    # Load DICOM file\n    dicom = pydicom.dcmread(dicom_path)\n    # Extract pixel array from DICOM file\n    pixel_array = dicom.pixel_array\n    # Normalize pixel values to the range [0, 255]\n    pixel_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255\n    pixel_array = pixel_array.astype(np.uint8)\n    # Convert to a PIL image\n    image = Image.fromarray(pixel_array)\n    # Save as JPG\n    image.save(output_path)\n    return image.size\n\ndef convert_to_yolo_format(class_id, box_size, x_center, y_center, img_width, img_height):\n\n    x_center /= img_width\n    y_center /= img_height\n    bbox_width = box_size / img_width\n    bbox_height = box_size / img_height\n    return (class_id, x_center, y_center, bbox_width, bbox_height)\n\ndef save_yolo_label(filename, yolo_bboxes):\n    with open(filename, 'w') as file:\n        for bbox in yolo_bboxes:\n            file.write(\" \".join(map(str, bbox)) + '\\n')\n\nval_percet = 0.3\ntrain_img_path = 'dataset/images/train/'\nvalid_img_path = 'dataset/images/val/'\n\ntrain_labels_path = 'dataset/labels/train/'\nvalid_labels_path = 'dataset/labels/val/'\n\nos.makedirs(train_img_path, exist_ok=True)\nos.makedirs(valid_img_path, exist_ok=True)\n\nos.makedirs(train_labels_path, exist_ok=True)\nos.makedirs(valid_labels_path, exist_ok=True)\n\n\nbox_size = 10\n\nunique_imgs = all_train_data[['series_id','study_id','instance_number']].drop_duplicates()\n# print(unique_imgs)\n# raise ValueError\nfor idx,row in tqdm(unique_imgs.iterrows()):\n    study_id = int(row.study_id)\n    series_id = int(row.series_id)\n    instance_number = int(row.instance_number)\n    \n    folder_path = meta_lab[str(study_id)]['folder_path']\n    \n    dcm_path = f'{folder_path}/{series_id}/{instance_number}.dcm'\n\n    \n    #plot_square_on_dcm(dcm_path,x,y,20)\n    \n    seed = np.random.rand()\n    \n    output_image_path_val = valid_img_path + f'{study_id}_{series_id}_{instance_number}.jpg'\n    output_image_path_train = train_img_path + f'{study_id}_{series_id}_{instance_number}.jpg'\n    \n    # if seed < val_percet:\n    #     output_image_path =  output_image_path_val\n    # else:\n    output_image_path = output_image_path_train\n            \n    img_width, img_height = convert_dicom_to_jpg(dcm_path, output_image_path)\n\n    \n    all_diagnose_df = all_train_data[(all_train_data['study_id'] == study_id) & (all_train_data['series_id'] == series_id) & (all_train_data['instance_number'] == instance_number)].copy()\n    \n    yolo_bbox_list = []\n    \n    for idx,row in all_diagnose_df.iterrows():\n        x_center = row.x\n        y_center = row.y\n        diagnose = row.detailed_diagnose\n        diagnose_class = detailed_diagnose_dict[diagnose]\n\n        yolo_bbox = convert_to_yolo_format(diagnose_class,box_size,x_center, y_center, img_width, img_height)\n        yolo_bbox_list.append(yolo_bbox)\n    \n    output_label_path_val = valid_labels_path + f'{str(row.study_id)}_{int(row.series_id)}_{int(row.instance_number)}.txt'\n    output_label_path_train = train_labels_path + f'{str(row.study_id)}_{int(row.series_id)}_{int(row.instance_number)}.txt'\n\n\n    # if seed < val_percet:\n    #     output_label_path =  output_label_path_val\n    # else:\n    output_label_path = output_label_path_train\n            \n    save_yolo_label(output_label_path, yolo_bbox_list)\n\n    #raise ValueError\n    #plot_image_with_boxes('/kaggle/working/dataset/images/val/11.jpg', yolo_bbox, img_width, img_height)\n    \n#     print(img_width, img_height)\n#     print(yolo_bbox)\n#     raise ValueError","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-23T17:18:47.264132Z","iopub.execute_input":"2024-06-23T17:18:47.264563Z","iopub.status.idle":"2024-06-23T17:18:55.999162Z","shell.execute_reply.started":"2024-06-23T17:18:47.264533Z","shell.execute_reply":"2024-06-23T17:18:55.997466Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}