{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm_notebook","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-25T17:57:19.997009Z","iopub.execute_input":"2021-05-25T17:57:19.997355Z","iopub.status.idle":"2021-05-25T17:57:20.001533Z","shell.execute_reply.started":"2021-05-25T17:57:19.997322Z","shell.execute_reply":"2021-05-25T17:57:20.000695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nfrom glob import glob\n\ndef jsons_to_df(folder):\n    imgs = glob(f\"{folder}/*.json\")\n    \n    df = []\n    for i in imgs:\n        i = json.load(open(i, 'r'))\n        df.append(pd.DataFrame({\n            'image': i['imagePath'],\n            'bbox': [[int(j) for j in i['shapes'][0]['points'][1]] + [int(j) for j in i['shapes'][0]['points'][0]]],\n            'height': i['imageHeight'], \n            'width': i['imageWidth']\n        }))\n        \n    return pd.concat(df).reset_index(drop=True)\n        \n    \ntrain_df = jsons_to_df('../input/plant2021-bboxes')\n\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:58:06.677786Z","iopub.execute_input":"2021-05-25T17:58:06.678096Z","iopub.status.idle":"2021-05-25T17:58:06.766591Z","shell.execute_reply.started":"2021-05-25T17:58:06.678066Z","shell.execute_reply":"2021-05-25T17:58:06.765912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:10:15.530229Z","iopub.execute_input":"2021-05-25T17:10:15.530549Z","iopub.status.idle":"2021-05-25T17:10:15.54507Z","shell.execute_reply.started":"2021-05-25T17:10:15.530519Z","shell.execute_reply":"2021-05-25T17:10:15.543645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['width'].unique(), train_df['height'].unique()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:58:48.235267Z","iopub.execute_input":"2021-05-25T17:58:48.235621Z","iopub.status.idle":"2021-05-25T17:58:48.242297Z","shell.execute_reply.started":"2021-05-25T17:58:48.235586Z","shell.execute_reply":"2021-05-25T17:58:48.241501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for g in train_df.groupby('image'):\n    b = g[1]['bbox'].values\n    print(type(b),b)\n    break","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:59:03.658982Z","iopub.execute_input":"2021-05-25T17:59:03.659296Z","iopub.status.idle":"2021-05-25T17:59:03.666845Z","shell.execute_reply.started":"2021-05-25T17:59:03.659266Z","shell.execute_reply":"2021-05-25T17:59:03.665998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_data = []\nfor g in tqdm_notebook(train_df.groupby('image')):\n    data = {}\n    data['filename'] = g[0]\n    data['bbox'] = g[1]['bbox'].values[0]\n    data['height'] = g[1]['height'].values[0]\n    data['width'] = g[1]['width'].values[0]\n    total_data.append(data)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:59:27.686489Z","iopub.execute_input":"2021-05-25T17:59:27.686811Z","iopub.status.idle":"2021-05-25T17:59:27.741083Z","shell.execute_reply.started":"2021-05-25T17:59:27.686781Z","shell.execute_reply":"2021-05-25T17:59:27.74026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(total_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:59:30.979897Z","iopub.execute_input":"2021-05-25T17:59:30.980239Z","iopub.status.idle":"2021-05-25T17:59:30.986211Z","shell.execute_reply.started":"2021-05-25T17:59:30.980207Z","shell.execute_reply":"2021-05-25T17:59:30.985264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_data[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:59:39.663133Z","iopub.execute_input":"2021-05-25T17:59:39.663479Z","iopub.status.idle":"2021-05-25T17:59:39.66909Z","shell.execute_reply.started":"2021-05-25T17:59:39.663447Z","shell.execute_reply":"2021-05-25T17:59:39.667959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install cython pyyaml==5.1\n!pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'\nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:10:24.993751Z","iopub.execute_input":"2021-05-25T17:10:24.994061Z","iopub.status.idle":"2021-05-25T17:10:49.463963Z","shell.execute_reply.started":"2021-05-25T17:10:24.994031Z","shell.execute_reply":"2021-05-25T17:10:49.462889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install detectron2:\n!pip install detectron2==0.1.2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:10:51.759646Z","iopub.execute_input":"2021-05-25T17:10:51.759973Z","iopub.status.idle":"2021-05-25T17:11:02.029361Z","shell.execute_reply.started":"2021-05-25T17:10:51.759942Z","shell.execute_reply":"2021-05-25T17:11:02.028379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import detectron2\nfrom detectron2.utils.logger import setup_logger\nsetup_logger()\n\nimport numpy as np\nimport cv2\nimport random\n\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom detectron2.utils.visualizer import Visualizer\nfrom detectron2.data import MetadataCatalog","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:11:05.0022Z","iopub.execute_input":"2021-05-25T17:11:05.002545Z","iopub.status.idle":"2021-05-25T17:11:05.445389Z","shell.execute_reply.started":"2021-05-25T17:11:05.002511Z","shell.execute_reply":"2021-05-25T17:11:05.444576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://i.ytimg.com/vi/dcXMf6rmafs/maxresdefault.jpg  -O input.jpg\nim = cv2.imread(\"./input.jpg\")\n\nimport matplotlib.pyplot as plt\n\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:13:43.837921Z","iopub.execute_input":"2021-05-25T17:13:43.838264Z","iopub.status.idle":"2021-05-25T17:13:44.823065Z","shell.execute_reply.started":"2021-05-25T17:13:43.838229Z","shell.execute_reply":"2021-05-25T17:13:44.822103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://raw.githubusercontent.com/facebookresearch/detectron2/master/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:17:05.558298Z","iopub.execute_input":"2021-05-25T17:17:05.558625Z","iopub.status.idle":"2021-05-25T17:17:06.447899Z","shell.execute_reply.started":"2021-05-25T17:17:05.558594Z","shell.execute_reply":"2021-05-25T17:17:06.447119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://raw.githubusercontent.com/facebookresearch/detectron2/master/configs/Base-RCNN-FPN.yaml","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:18:27.875961Z","iopub.execute_input":"2021-05-25T17:18:27.876386Z","iopub.status.idle":"2021-05-25T17:18:28.809487Z","shell.execute_reply.started":"2021-05-25T17:18:27.876342Z","shell.execute_reply":"2021-05-25T17:18:28.808739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = get_cfg()\n#cfg.merge_from_file(\"mask_rcnn_R_50_FPN_3x.yaml\")\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\"))\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5\ncfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\")  \n\n#\"detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl\"","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:27:10.004306Z","iopub.execute_input":"2021-05-25T17:27:10.004684Z","iopub.status.idle":"2021-05-25T17:27:10.027604Z","shell.execute_reply.started":"2021-05-25T17:27:10.004652Z","shell.execute_reply":"2021-05-25T17:27:10.026852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictor = DefaultPredictor(cfg)\noutputs = predictor(im)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:27:11.746205Z","iopub.execute_input":"2021-05-25T17:27:11.746553Z","iopub.status.idle":"2021-05-25T17:27:13.148107Z","shell.execute_reply.started":"2021-05-25T17:27:11.746518Z","shell.execute_reply":"2021-05-25T17:27:13.147249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:27:14.57928Z","iopub.execute_input":"2021-05-25T17:27:14.579621Z","iopub.status.idle":"2021-05-25T17:27:14.607088Z","shell.execute_reply.started":"2021-05-25T17:27:14.57959Z","shell.execute_reply":"2021-05-25T17:27:14.606096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\nv = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)\nv = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\nv","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:27:24.177825Z","iopub.execute_input":"2021-05-25T17:27:24.17816Z","iopub.status.idle":"2021-05-25T17:27:24.343181Z","shell.execute_reply.started":"2021-05-25T17:27:24.178117Z","shell.execute_reply":"2021-05-25T17:27:24.342457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 15))\nplt.imshow(v.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T17:27:25.233289Z","iopub.execute_input":"2021-05-25T17:27:25.233632Z","iopub.status.idle":"2021-05-25T17:27:26.095195Z","shell.execute_reply.started":"2021-05-25T17:27:25.233602Z","shell.execute_reply":"2021-05-25T17:27:26.088208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_data[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-25T15:47:14.411646Z","iopub.execute_input":"2021-05-25T15:47:14.411966Z","iopub.status.idle":"2021-05-25T15:47:14.417661Z","shell.execute_reply.started":"2021-05-25T15:47:14.411935Z","shell.execute_reply":"2021-05-25T15:47:14.41669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\n\nfrom detectron2.structures import BoxMode\n\ndef get_dicts(total_data):\n    \n    dataset_dicts = []\n    for idx, v in enumerate(total_data):\n        record = {}\n        \n        filename = os.path.join('../input/plant2021-downscaled-images-dataset/', v[\"filename\"])\n        \n        record[\"file_name\"] = filename\n        record[\"image_id\"] = idx\n        record[\"height\"] = v['height']\n        record[\"width\"] = v['width']\n            \n        record[\"annotations\"] = [{\n                'bbox': v['bbox'],\n                'bbox_mode': BoxMode.XYXY_ABS,\n                'category_id':0,\n            }]\n        dataset_dicts.append(record)\n    return dataset_dicts","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:05:11.586714Z","iopub.execute_input":"2021-05-25T18:05:11.587034Z","iopub.status.idle":"2021-05-25T18:05:11.59529Z","shell.execute_reply.started":"2021-05-25T18:05:11.587003Z","shell.execute_reply":"2021-05-25T18:05:11.594433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.data import DatasetCatalog, MetadataCatalog\n\nindex = int(0.9 * len(total_data))\ntrain_data = total_data[:index]\nval_data = total_data[index:]\n\nfolders = ['train', 'val']\nfor i, d in enumerate([train_data,val_data]):\n    DatasetCatalog.register(\"plant_\" + folders[i], lambda d=d: get_dicts(d))\n    MetadataCatalog.get(\"plant_\" + folders[i]).set(thing_classes=[\"plant\"])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:05:28.4219Z","iopub.execute_input":"2021-05-25T18:05:28.422213Z","iopub.status.idle":"2021-05-25T18:05:28.429587Z","shell.execute_reply.started":"2021-05-25T18:05:28.422182Z","shell.execute_reply":"2021-05-25T18:05:28.428491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(val_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:05:35.44025Z","iopub.execute_input":"2021-05-25T18:05:35.440604Z","iopub.status.idle":"2021-05-25T18:05:35.447613Z","shell.execute_reply.started":"2021-05-25T18:05:35.440575Z","shell.execute_reply":"2021-05-25T18:05:35.446701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plant_metadata = MetadataCatalog.get(\"plant_train\")","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:05:41.461933Z","iopub.execute_input":"2021-05-25T18:05:41.462242Z","iopub.status.idle":"2021-05-25T18:05:41.466343Z","shell.execute_reply.started":"2021-05-25T18:05:41.462213Z","shell.execute_reply":"2021-05-25T18:05:41.4655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[0]['bbox'][0]","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:06:32.205344Z","iopub.execute_input":"2021-05-25T18:06:32.205716Z","iopub.status.idle":"2021-05-25T18:06:32.210833Z","shell.execute_reply.started":"2021-05-25T18:06:32.205687Z","shell.execute_reply":"2021-05-25T18:06:32.209984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_dicts = get_dicts(train_data)\nfor d in random.sample(dataset_dicts, 3):\n    img = cv2.imread(d[\"file_name\"])\n    visualizer = Visualizer(img[:, :, ::-1], metadata=plant_metadata, scale=1)\n    vis = visualizer.draw_dataset_dict(d)\n    plt.figure(figsize=(15,15))\n    plt.imshow(vis.get_image()[:,:,::-1])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:07:52.389378Z","iopub.execute_input":"2021-05-25T18:07:52.389715Z","iopub.status.idle":"2021-05-25T18:07:54.011656Z","shell.execute_reply.started":"2021-05-25T18:07:52.389686Z","shell.execute_reply":"2021-05-25T18:07:54.010874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.engine import DefaultTrainer\nfrom detectron2.config import get_cfg\n\ncfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\"))\ncfg.DATASETS.TRAIN = (\"plant_train\",)\ncfg.DATASETS.TEST = ()\ncfg.DATALOADER.NUM_WORKERS = 4\ncfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\")  \ncfg.SOLVER.IMS_PER_BATCH = 1\ncfg.SOLVER.BASE_LR = 0.0003  \ncfg.SOLVER.MAX_ITER = 400   \ncfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 12   \ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  \n\nos.makedirs(cfg.OUTPUT_DIR, exist_ok=True)\ntrainer = DefaultTrainer(cfg) \ntrainer.resume_or_load(resume=False)\ntrainer.train()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:12:46.970865Z","iopub.execute_input":"2021-05-25T18:12:46.971206Z","iopub.status.idle":"2021-05-25T18:16:25.166444Z","shell.execute_reply.started":"2021-05-25T18:12:46.971163Z","shell.execute_reply":"2021-05-25T18:16:25.16521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, \"model_final.pth\")\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7   \ncfg.DATASETS.TEST = (\"plant_val\", )\npredictor = DefaultPredictor(cfg)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:16:41.151661Z","iopub.execute_input":"2021-05-25T18:16:41.152014Z","iopub.status.idle":"2021-05-25T18:16:43.602488Z","shell.execute_reply.started":"2021-05-25T18:16:41.151979Z","shell.execute_reply":"2021-05-25T18:16:43.601703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.utils.visualizer import ColorMode\ndataset_dicts = get_dicts(val_data)\nfor d in dataset_dicts:    \n    im = cv2.imread(d[\"file_name\"])\n    outputs = predictor(im)\n    v = Visualizer(im[:, :, ::-1],\n                   metadata=plant_metadata, \n                   scale=0.8, \n    )\n    v = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n    plt.imshow(v.get_image()[:,:,::-1])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:17:31.124702Z","iopub.execute_input":"2021-05-25T18:17:31.125018Z","iopub.status.idle":"2021-05-25T18:17:33.141849Z","shell.execute_reply.started":"2021-05-25T18:17:31.124989Z","shell.execute_reply":"2021-05-25T18:17:33.141157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = cv2.imread('../input/plant2021-downscaled-images-dataset/832d2ee485c5ff11.jpg')\noutputs = predictor(im)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:20:25.58511Z","iopub.execute_input":"2021-05-25T18:20:25.585463Z","iopub.status.idle":"2021-05-25T18:20:25.824709Z","shell.execute_reply.started":"2021-05-25T18:20:25.585428Z","shell.execute_reply":"2021-05-25T18:20:25.823679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:20:26.634283Z","iopub.execute_input":"2021-05-25T18:20:26.634697Z","iopub.status.idle":"2021-05-25T18:20:26.642835Z","shell.execute_reply.started":"2021-05-25T18:20:26.634657Z","shell.execute_reply":"2021-05-25T18:20:26.641796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"v = Visualizer(im[:, :, ::-1],\n                   metadata=plant_metadata, \n                   scale=0.8, \n    )\nv = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\nplt.imshow(v.get_image()[:,:,::-1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:27:34.610769Z","iopub.execute_input":"2021-05-25T18:27:34.611077Z","iopub.status.idle":"2021-05-25T18:27:34.854667Z","shell.execute_reply.started":"2021-05-25T18:27:34.611049Z","shell.execute_reply":"2021-05-25T18:27:34.85397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box = outputs[\"instances\"].get_fields()['pred_boxes'].tensor.cpu()[0]\nbox","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:28:49.29261Z","iopub.execute_input":"2021-05-25T18:28:49.292925Z","iopub.status.idle":"2021-05-25T18:28:49.299995Z","shell.execute_reply.started":"2021-05-25T18:28:49.292896Z","shell.execute_reply":"2021-05-25T18:28:49.299097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box = box.long()\nbox","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:30:28.969743Z","iopub.execute_input":"2021-05-25T18:30:28.970056Z","iopub.status.idle":"2021-05-25T18:30:28.978124Z","shell.execute_reply.started":"2021-05-25T18:30:28.970026Z","shell.execute_reply":"2021-05-25T18:30:28.977202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:29:14.06466Z","iopub.execute_input":"2021-05-25T18:29:14.064975Z","iopub.status.idle":"2021-05-25T18:29:14.070077Z","shell.execute_reply.started":"2021-05-25T18:29:14.064946Z","shell.execute_reply":"2021-05-25T18:29:14.069244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im[85:527, 18:716])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:32:23.340065Z","iopub.execute_input":"2021-05-25T18:32:23.340394Z","iopub.status.idle":"2021-05-25T18:32:23.551251Z","shell.execute_reply.started":"2021-05-25T18:32:23.340363Z","shell.execute_reply":"2021-05-25T18:32:23.550244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image(img):\n    outputs = predictor(img)\n    box = outputs[\"instances\"].get_fields()['pred_boxes'].tensor.cpu()[0].long()\n    return im[box[1]:box[3], box[0]:box[2]]","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:36:04.679811Z","iopub.execute_input":"2021-05-25T18:36:04.680123Z","iopub.status.idle":"2021-05-25T18:36:04.686618Z","shell.execute_reply.started":"2021-05-25T18:36:04.680096Z","shell.execute_reply":"2021-05-25T18:36:04.685465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop_image(im)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:36:10.491852Z","iopub.execute_input":"2021-05-25T18:36:10.492161Z","iopub.status.idle":"2021-05-25T18:36:10.732742Z","shell.execute_reply.started":"2021-05-25T18:36:10.49213Z","shell.execute_reply":"2021-05-25T18:36:10.731864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import YouTubeVideo, display\nvideo = YouTubeVideo(\"lWqg010_90I\", width=500) #ll8TgCZ0plk\ndisplay(video)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:42:36.845843Z","iopub.execute_input":"2021-05-25T18:42:36.846302Z","iopub.status.idle":"2021-05-25T18:42:37.236225Z","shell.execute_reply.started":"2021-05-25T18:42:36.846257Z","shell.execute_reply":"2021-05-25T18:42:37.235512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install dependencies, download the video, and crop 5 seconds for processing\n!pip install youtube-dl\n!pip uninstall -y opencv-python opencv-contrib-python -y\n!apt install python3-opencv -y\n!youtube-dl https://www.youtube.com/watch?v=lWqg010_90I -f 22 -o video.mp4\n!ffmpeg -i video.mp4 -t 00:00:06 -c:v copy video-clip.mp4","metadata":{"execution":{"iopub.status.busy":"2021-05-25T18:45:31.1075Z","iopub.execute_input":"2021-05-25T18:45:31.107831Z","iopub.status.idle":"2021-05-25T18:45:53.310558Z","shell.execute_reply.started":"2021-05-25T18:45:31.1078Z","shell.execute_reply":"2021-05-25T18:45:53.309727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}