{"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-11-23T12:41:20.606353Z","iopub.execute_input":"2021-11-23T12:41:20.606760Z","iopub.status.idle":"2021-11-23T12:41:33.273952Z","shell.execute_reply.started":"2021-11-23T12:41:20.606688Z","shell.execute_reply":"2021-11-23T12:41:33.272841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Due to inavalaibility of resoucres I am using [matterport/Mask_RCNN](https://github.com/matterport/Mask_RCNN)","metadata":{}},{"cell_type":"code","source":"import os \nimport sys\nimport random\nimport math\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport json\nimport pydicom\nfrom imgaug import augmenters as iaa\nfrom tqdm import tqdm\nimport pandas as pd \nimport glob\nimport imutils\nfrom sklearn.model_selection import KFold","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:41:33.277648Z","iopub.execute_input":"2021-11-23T12:41:33.278114Z","iopub.status.idle":"2021-11-23T12:41:35.117015Z","shell.execute_reply.started":"2021-11-23T12:41:33.277958Z","shell.execute_reply":"2021-11-23T12:41:35.115295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"id":"yP0XLJx_x_6o","_uuid":"6e5764759e6a0a9b698b44645658f66873edd807","execution":{"iopub.status.busy":"2021-11-23T12:41:35.119217Z","iopub.execute_input":"2021-11-23T12:41:35.119898Z","iopub.status.idle":"2021-11-23T12:41:35.132756Z","shell.execute_reply.started":"2021-11-23T12:41:35.119819Z","shell.execute_reply":"2021-11-23T12:41:35.131241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images=[]\nfor i in os.listdir(\"../input/sampleimages\"):\n    image1 = cv2.imread(\"../input/sampleimages/\"+str(i))\n    images.append(image1)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:41:37.704023Z","iopub.execute_input":"2021-11-23T12:41:37.704497Z","iopub.status.idle":"2021-11-23T12:41:37.848308Z","shell.execute_reply.started":"2021-11-23T12:41:37.704399Z","shell.execute_reply":"2021-11-23T12:41:37.847353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cloning github repository\n!git clone https://www.github.com/matterport/Mask_RCNN.git\nos.chdir('Mask_RCNN')","metadata":{"id":"KgllzLnDr7kF","outputId":"6c978df7-2013-437e-acd1-5011048dfb53","_uuid":"b37d22551d332f0f7b722cc7204eb614524b6c21","execution":{"iopub.status.busy":"2021-11-23T12:41:39.879592Z","iopub.execute_input":"2021-11-23T12:41:39.879959Z","iopub.status.idle":"2021-11-23T12:41:50.213828Z","shell.execute_reply.started":"2021-11-23T12:41:39.879875Z","shell.execute_reply":"2021-11-23T12:41:50.212603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import Mask RCNN\nsys.path.append(os.path.join('/kaggle/working', 'Mask_RCNN'))  # To find local version of the library\nfrom mrcnn.config import Config\nfrom mrcnn import utils\nimport mrcnn.model as modellib\nfrom mrcnn import visualize\nfrom mrcnn.model import log","metadata":{"id":"-KZXyWwhzOVU","outputId":"2576cc17-7484-4311-ad72-3c5643dcb5bb","_uuid":"3acbbbe055b6a409d3c50ae0f893acf51b5ae7ba","execution":{"iopub.status.busy":"2021-11-23T12:41:50.216841Z","iopub.execute_input":"2021-11-23T12:41:50.217228Z","iopub.status.idle":"2021-11-23T12:41:51.364394Z","shell.execute_reply.started":"2021-11-23T12:41:50.217156Z","shell.execute_reply":"2021-11-23T12:41:51.363242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#downloading pretrained model\n!wget --quiet https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5\n\nCOCO_WEIGHTS_PATH = \"./mask_rcnn_coco.h5\"","metadata":{"_uuid":"c3ee0cd0ee0b1defdec97b94bc736587c1f7631f","execution":{"iopub.status.busy":"2021-11-23T12:41:51.366136Z","iopub.execute_input":"2021-11-23T12:41:51.366750Z","iopub.status.idle":"2021-11-23T12:41:55.547219Z","shell.execute_reply.started":"2021-11-23T12:41:51.366682Z","shell.execute_reply":"2021-11-23T12:41:55.545700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASS_NAMES = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',\n               'bus', 'train', 'truck', 'boat', 'traffic light',\n               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',\n               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',\n               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',\n               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',\n               'kite', 'baseball bat', 'baseball glove', 'skateboard',\n               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',\n               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',\n               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',\n               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',\n               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',\n               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',\n               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',\n               'teddy bear', 'hair drier', 'toothbrush']","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:41:55.551202Z","iopub.execute_input":"2021-11-23T12:41:55.551611Z","iopub.status.idle":"2021-11-23T12:41:55.565929Z","shell.execute_reply.started":"2021-11-23T12:41:55.551536Z","shell.execute_reply":"2021-11-23T12:41:55.564705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleConfig(Config):\n    # give the configuration a recognizable name\n    NAME = \"coco_inference\"\n    # set the number of GPUs to use along with the number of images\n    # per GPU\n    GPU_COUNT = 1\n    IMAGES_PER_GPU = 1\n    # number of classes on COCO dataset\n    NUM_CLASSES = 81","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:41:55.569825Z","iopub.execute_input":"2021-11-23T12:41:55.570363Z","iopub.status.idle":"2021-11-23T12:41:55.583401Z","shell.execute_reply.started":"2021-11-23T12:41:55.570285Z","shell.execute_reply":"2021-11-23T12:41:55.582336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config = SimpleConfig()\nconfig.display()\nmodel = modellib.MaskRCNN(mode=\"inference\", config=config, model_dir=os.getcwd())\nmodel.load_weights(\"./mask_rcnn_coco.h5\", by_name=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:41:55.588529Z","iopub.execute_input":"2021-11-23T12:41:55.594381Z","iopub.status.idle":"2021-11-23T12:42:09.070752Z","shell.execute_reply.started":"2021-11-23T12:41:55.594266Z","shell.execute_reply":"2021-11-23T12:42:09.069252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"./images/3862500489_6fd195d183_z.jpg\")\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\nimage = cv2.resize(image, (0,0), fx=500/400, fy=500/400)\nprint(\"[INFO] making predictions with Mask R-CNN...\")\nresult = model.detect([image], verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:42:09.073876Z","iopub.execute_input":"2021-11-23T12:42:09.074502Z","iopub.status.idle":"2021-11-23T12:42:14.789756Z","shell.execute_reply.started":"2021-11-23T12:42:09.074433Z","shell.execute_reply":"2021-11-23T12:42:14.788227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r1 = result[0]\nvisualize.display_instances(image, r1['rois'], r1['masks'],\n                            r1['class_ids'], CLASS_NAMES, r1['scores'])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:42:14.797785Z","iopub.execute_input":"2021-11-23T12:42:14.798438Z","iopub.status.idle":"2021-11-23T12:42:15.867918Z","shell.execute_reply.started":"2021-11-23T12:42:14.798169Z","shell.execute_reply":"2021-11-23T12:42:15.866836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in images:\n    image = cv2.cvtColor(i, cv2.COLOR_BGR2RGB)\n    image = cv2.resize(image, (0,0), fx=500/400, fy=500/400)\n    print(\"[INFO] making predictions with Mask R-CNN...\")\n    result = model.detect([image], verbose=1)\n    r1 = result[0]\n    visualize.display_instances(image, r1['rois'], r1['masks'],\n                                r1['class_ids'], CLASS_NAMES, r1['scores'])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T12:42:23.178242Z","iopub.execute_input":"2021-11-23T12:42:23.178639Z","iopub.status.idle":"2021-11-23T12:42:34.125579Z","shell.execute_reply.started":"2021-11-23T12:42:23.178566Z","shell.execute_reply":"2021-11-23T12:42:34.124366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}