{"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":"!pip install imutils","metadata":{"execution":{"iopub.status.busy":"2021-05-21T02:57:57.509842Z","iopub.execute_input":"2021-05-21T02:57:57.510655Z","iopub.status.idle":"2021-05-21T02:58:04.125641Z","shell.execute_reply.started":"2021-05-21T02:57:57.510603Z","shell.execute_reply":"2021-05-21T02:58:04.124213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\nimport cv2\nimport pydicom\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom imutils import paths\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\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","_kg_hide-output":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-05-21T02:58:04.127567Z","iopub.execute_input":"2021-05-21T02:58:04.127885Z","iopub.status.idle":"2021-05-21T02:58:04.136069Z","shell.execute_reply.started":"2021-05-21T02:58:04.127850Z","shell.execute_reply":"2021-05-21T02:58:04.135075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = pydicom.dcmread('/kaggle/input/siim-covid19-detection/train/08bf9775ebfd/618c811827ef/f43ef9430a14.dcm')\n#print(image)\nplt.figure(figsize=(10,10))\nplt.imshow(image.pixel_array, cmap=plt.cm.bone)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T03:04:29.710869Z","iopub.execute_input":"2021-05-21T03:04:29.711413Z","iopub.status.idle":"2021-05-21T03:04:32.376774Z","shell.execute_reply.started":"2021-05-21T03:04:29.711377Z","shell.execute_reply":"2021-05-21T03:04:32.375836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-21T02:58:05.223627Z","iopub.execute_input":"2021-05-21T02:58:05.223960Z","iopub.status.idle":"2021-05-21T02:58:05.230385Z","shell.execute_reply.started":"2021-05-21T02:58:05.223929Z","shell.execute_reply":"2021-05-21T02:58:05.229436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = image.pixel_array","metadata":{"execution":{"iopub.status.busy":"2021-05-21T03:04:42.498169Z","iopub.execute_input":"2021-05-21T03:04:42.498539Z","iopub.status.idle":"2021-05-21T03:04:42.502886Z","shell.execute_reply.started":"2021-05-21T03:04:42.498505Z","shell.execute_reply":"2021-05-21T03:04:42.501859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]","metadata":{"execution":{"iopub.status.busy":"2021-05-21T03:04:44.944545Z","iopub.execute_input":"2021-05-21T03:04:44.945130Z","iopub.status.idle":"2021-05-21T03:04:44.965512Z","shell.execute_reply.started":"2021-05-21T03:04:44.945076Z","shell.execute_reply":"2021-05-21T03:04:44.964438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(thresh, cmap=plt.cm.bone)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T03:04:47.124384Z","iopub.execute_input":"2021-05-21T03:04:47.124731Z","iopub.status.idle":"2021-05-21T03:04:48.794671Z","shell.execute_reply.started":"2021-05-21T03:04:47.124702Z","shell.execute_reply":"2021-05-21T03:04:48.793728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimplePreprocessor:\n    def __init__(self, width, height, inter=cv2.INTER_AREA):\n        self.width = width\n        self.height = height\n        self.inter = inter\n    \n    def proprocess(self, image):\n        return cv2.resize(image, (self.width, self.height), interpolation=self.inter)\n        \n    def imagethresh(self, image):\n        thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]\n        return thresh","metadata":{"execution":{"iopub.status.busy":"2021-05-21T02:58:06.199149Z","iopub.execute_input":"2021-05-21T02:58:06.199533Z","iopub.status.idle":"2021-05-21T02:58:06.208116Z","shell.execute_reply.started":"2021-05-21T02:58:06.199495Z","shell.execute_reply":"2021-05-21T02:58:06.206950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleDatasetLoader:\n    def __init__(self, preprocessors=None):\n        self.preprocessors = preprocessors\n        \n        if self.preprocessors  is None:\n            self.preprocessors = []\n            \n    def load(self, imagePaths, verbose=-1):\n        data =[]\n        labels = []\n        \n        for (i, imagePath) in enumerate(imagePaths):\n            if imagePath.split(\".\")[-1] == 'dcm':\n                image = pydicom.dcmread(imagePath)\n                image = image.pixel_array            \n            \n            if self.preprocessors is not None:\n                for p in self.preprocessors:\n                    image = p.proprocess(image)\n                    image = p.imagethresh(image)\n                    \n            data.append(image)\n            \n            if i >15:\n                break\n            \n            if verbose > 0 and  i > 0 and (i+1)% verbose ==0:\n                print(\"[INFO] processed {} /{}\".format(i+1, len(imagePaths)))\n            \n        return (np.array(data))\n            ","metadata":{"execution":{"iopub.status.busy":"2021-05-21T03:25:38.528917Z","iopub.execute_input":"2021-05-21T03:25:38.529298Z","iopub.status.idle":"2021-05-21T03:25:38.538249Z","shell.execute_reply.started":"2021-05-21T03:25:38.529268Z","shell.execute_reply":"2021-05-21T03:25:38.537175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"[INFO] loading image ...\")\nimagePaths = list(paths.list_files('/kaggle/input/siim-covid19-detection/train/'))  \n\nsp = SimplePreprocessor(64, 64)\nsdl = SimpleDatasetLoader(preprocessors=[sp])\ndata = sdl.load(imagePaths, verbose=100)\ndata = data.reshape((data.shape[0], 4096))","metadata":{"execution":{"iopub.status.busy":"2021-05-21T03:25:47.085466Z","iopub.execute_input":"2021-05-21T03:25:47.085857Z","iopub.status.idle":"2021-05-21T03:25:53.804080Z","shell.execute_reply.started":"2021-05-21T03:25:47.085791Z","shell.execute_reply":"2021-05-21T03:25:53.803129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}