{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\n# for 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","execution":{"iopub.status.busy":"2022-11-30T12:03:10.271301Z","iopub.execute_input":"2022-11-30T12:03:10.271797Z","iopub.status.idle":"2022-11-30T12:03:10.3047Z","shell.execute_reply.started":"2022-11-30T12:03:10.27169Z","shell.execute_reply":"2022-11-30T12:03:10.303719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## YOLO MODEL","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:03:10.331729Z","iopub.execute_input":"2022-11-30T12:03:10.332436Z","iopub.status.idle":"2022-11-30T12:03:10.338115Z","shell.execute_reply.started":"2022-11-30T12:03:10.332397Z","shell.execute_reply":"2022-11-30T12:03:10.336819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport cv2\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:03:10.340822Z","iopub.execute_input":"2022-11-30T12:03:10.341622Z","iopub.status.idle":"2022-11-30T12:03:10.75813Z","shell.execute_reply.started":"2022-11-30T12:03:10.341575Z","shell.execute_reply":"2022-11-30T12:03:10.756409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_id = '1.2.826.0.1.3680043.4740'\npatient_path = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/'+ patient_id+'/'\npatient_path_list = [ f for f in  os.listdir(patient_path)]\noutdir = '/kaggle/working/' + patient_id + '/'\n\n# 1.2.826.0.1.3680043.10579\n# 1.2.826.0.1.3680043.10230","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:07:56.406108Z","iopub.execute_input":"2022-11-30T12:07:56.406608Z","iopub.status.idle":"2022-11-30T12:07:56.504483Z","shell.execute_reply.started":"2022-11-30T12:07:56.40657Z","shell.execute_reply":"2022-11-30T12:07:56.503572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.isdir(outdir) == False:\n    os.mkdir(outdir)\nfor f in patient_path_list: \n    ds = pydicom.read_file(patient_path + f) \n    img = ds.pixel_array #\n    cv2.imwrite(outdir + f.replace('.dcm','.png'),img) # write png image","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:07:56.575045Z","iopub.execute_input":"2022-11-30T12:07:56.575988Z","iopub.status.idle":"2022-11-30T12:08:01.935291Z","shell.execute_reply.started":"2022-11-30T12:07:56.57595Z","shell.execute_reply":"2022-11-30T12:08:01.933265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.isdir('/kaggle/working/yolov5') == False:\n    shutil.copytree('/kaggle/input/v2-balloon-detection-dataset/yolov5', '/kaggle/working/yolov5')\nif os.path.isdir('/kaggle/working/best.pt') == False:\n    shutil.copyfile('/kaggle/input/finalmod/best.pt', '/kaggle/working/best.pt')","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:08:01.937738Z","iopub.execute_input":"2022-11-30T12:08:01.938149Z","iopub.status.idle":"2022-11-30T12:08:02.175173Z","shell.execute_reply.started":"2022-11-30T12:08:01.938108Z","shell.execute_reply":"2022-11-30T12:08:02.174088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/working/yolov5')\ntest_images_path = '/kaggle/working/' + patient_id\nyolo_model_path = '/kaggle/working/best.pt'\nIMAGE_SIZE = 512\n\n!python detect.py --source $test_images_path --weights $yolo_model_path --img $IMAGE_SIZE --save-txt --save-conf --exist-ok","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:08:02.177145Z","iopub.execute_input":"2022-11-30T12:08:02.178013Z","iopub.status.idle":"2022-11-30T12:13:33.473056Z","shell.execute_reply.started":"2022-11-30T12:08:02.177957Z","shell.execute_reply":"2022-11-30T12:13:33.471444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"txt_files_list = os.listdir('runs/detect/exp/labels')\n\nprint(len(txt_files_list))\nprint(txt_files_list)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:33.476286Z","iopub.execute_input":"2022-11-30T12:13:33.476752Z","iopub.status.idle":"2022-11-30T12:13:33.485301Z","shell.execute_reply.started":"2022-11-30T12:13:33.476709Z","shell.execute_reply":"2022-11-30T12:13:33.483805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"txt_files_list = os.listdir('runs/detect/exp/labels')\n\nfor i, txt_file in enumerate(txt_files_list):\n    path = f'runs/detect/exp/labels/{txt_file}'\n    cols = ['class', 'x-center', 'y-center', 'bbox_width', 'bbox_height', 'conf-score']\n    df = pd.read_csv(path, sep=\" \", header=None)\n    df.columns = cols\n    fname = txt_file.replace(\".txt\",\"\")\n    df['id'] = fname\n    if i == 0:\n        df_test_preds = df\n    else:\n        df_test_preds = pd.concat([df_test_preds, df], axis=0)\n       \n    \n\nslice_no_array = df_test_preds['id']","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:33.487374Z","iopub.execute_input":"2022-11-30T12:13:33.488379Z","iopub.status.idle":"2022-11-30T12:13:34.606001Z","shell.execute_reply.started":"2022-11-30T12:13:33.48831Z","shell.execute_reply":"2022-11-30T12:13:34.604633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_no_array = slice_no_array.to_numpy() ","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:34.608105Z","iopub.execute_input":"2022-11-30T12:13:34.609313Z","iopub.status.idle":"2022-11-30T12:13:34.616076Z","shell.execute_reply.started":"2022-11-30T12:13:34.609256Z","shell.execute_reply":"2022-11-30T12:13:34.614569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(slice_no_array)):\n    slice_no_array[i] = int(slice_no_array[i])\nslice_no_array = np.sort(slice_no_array)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:34.618212Z","iopub.execute_input":"2022-11-30T12:13:34.618688Z","iopub.status.idle":"2022-11-30T12:13:34.628466Z","shell.execute_reply.started":"2022-11-30T12:13:34.618638Z","shell.execute_reply":"2022-11-30T12:13:34.626802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##RANDOM FOREST","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:34.630388Z","iopub.execute_input":"2022-11-30T12:13:34.630916Z","iopub.status.idle":"2022-11-30T12:13:34.639499Z","shell.execute_reply.started":"2022-11-30T12:13:34.630868Z","shell.execute_reply":"2022-11-30T12:13:34.638054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_slices = len(os.listdir(patient_path))","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:34.640965Z","iopub.execute_input":"2022-11-30T12:13:34.641454Z","iopub.status.idle":"2022-11-30T12:13:34.655429Z","shell.execute_reply.started":"2022-11-30T12:13:34.641416Z","shell.execute_reply":"2022-11-30T12:13:34.653855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom as dicom\nimport pickle","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:34.658803Z","iopub.execute_input":"2022-11-30T12:13:34.659192Z","iopub.status.idle":"2022-11-30T12:13:34.665105Z","shell.execute_reply.started":"2022-11-30T12:13:34.65916Z","shell.execute_reply":"2022-11-30T12:13:34.663835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest_model = pickle.load(open('/kaggle/input/random-forest-model/randomforest_model.sav', 'rb'))\nfeatures = ['SliceRatio','SliceThickness','ImagePositionPatient_x','ImagePositionPatient_y','ImagePositionPatient_z']","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:34.6669Z","iopub.execute_input":"2022-11-30T12:13:34.6674Z","iopub.status.idle":"2022-11-30T12:13:36.276073Z","shell.execute_reply.started":"2022-11-30T12:13:34.667337Z","shell.execute_reply":"2022-11-30T12:13:36.274417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_final = np.array([0.0,0.0,0.0,0.0,0.0,0.0,0.0])\nans_final = np.array([0,0,0,0,0,0,0])","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:36.278008Z","iopub.execute_input":"2022-11-30T12:13:36.278477Z","iopub.status.idle":"2022-11-30T12:13:36.285856Z","shell.execute_reply.started":"2022-11-30T12:13:36.278441Z","shell.execute_reply":"2022-11-30T12:13:36.2843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in slice_no_array:\n    x = dicom.dcmread(patient_path + str(i) + \".dcm\")\n    slice_no = i\n    slice_ratio = int(slice_no)/no_of_slices\n    x_arr =  [slice_ratio , x.SliceThickness ,x.ImagePositionPatient[0] , x.ImagePositionPatient[1],x.ImagePositionPatient[2] ]\n    x_pd = pd.DataFrame(columns=features)\n    x_pd.loc[len(x_pd)] = x_arr\n    y = random_forest_model.predict(x_pd)\n    for j in range(7):\n        y_final[j] = y_final[j] + y[0][j]","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:36.287995Z","iopub.execute_input":"2022-11-30T12:13:36.288441Z","iopub.status.idle":"2022-11-30T12:13:45.223289Z","shell.execute_reply.started":"2022-11-30T12:13:36.288398Z","shell.execute_reply":"2022-11-30T12:13:45.222223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(y_final)):\n    print(y_final[i] / len(slice_no_array))\n    if(y_final[i] / len(slice_no_array) > 0.2):\n        ans_final[i] = int(1)\n    else:\n        ans_final[i] = int(0)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:14:14.450534Z","iopub.execute_input":"2022-11-30T12:14:14.450998Z","iopub.status.idle":"2022-11-30T12:14:14.458778Z","shell.execute_reply.started":"2022-11-30T12:14:14.450962Z","shell.execute_reply":"2022-11-30T12:14:14.457549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_final","metadata":{"execution":{"iopub.status.busy":"2022-11-30T12:13:56.310479Z","iopub.execute_input":"2022-11-30T12:13:56.310921Z","iopub.status.idle":"2022-11-30T12:13:56.318093Z","shell.execute_reply.started":"2022-11-30T12:13:56.310886Z","shell.execute_reply":"2022-11-30T12:13:56.317182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}