{"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\nfor 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-06-13T02:13:49.008822Z","iopub.execute_input":"2022-06-13T02:13:49.009299Z","iopub.status.idle":"2022-06-13T02:13:49.036703Z","shell.execute_reply.started":"2022-06-13T02:13:49.009186Z","shell.execute_reply":"2022-06-13T02:13:49.035781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nfrom shapely.wkt import loads as wkt_loads\nimport tifffile as tiff","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.037872Z","iopub.execute_input":"2022-06-13T02:13:49.038475Z","iopub.status.idle":"2022-06-13T02:13:49.408431Z","shell.execute_reply.started":"2022-06-13T02:13:49.038442Z","shell.execute_reply":"2022-06-13T02:13:49.407502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.410179Z","iopub.execute_input":"2022-06-13T02:13:49.411006Z","iopub.status.idle":"2022-06-13T02:13:49.431726Z","shell.execute_reply.started":"2022-06-13T02:13:49.41096Z","shell.execute_reply":"2022-06-13T02:13:49.430237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _get_image_names(base_path, imageId):\n    '''\n    Get the names of the tiff files\n    '''\n    d = {'3': path.join(base_path,'three_band/{}.tif'.format(imageId)),             # (3, 3348, 3403)\n         'A': path.join(base_path,'sixteen_band/{}_A.tif'.format(imageId)),         # (8, 134, 137)\n         'M': path.join(base_path,'sixteen_band/{}_M.tif'.format(imageId)),         # (8, 837, 851)\n         'P': path.join(base_path,'sixteen_band/{}_P.tif'.format(imageId)),         # (3348, 3403)\n         }\n    return d\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.433288Z","iopub.execute_input":"2022-06-13T02:13:49.433612Z","iopub.status.idle":"2022-06-13T02:13:49.440095Z","shell.execute_reply.started":"2022-06-13T02:13:49.433581Z","shell.execute_reply":"2022-06-13T02:13:49.439195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _convert_coordinates_to_raster(coords, img_size, xymax):\n    Xmax,Ymax = xymax\n    H,W = img_size\n    W1 = 1.0*W*W/(W+1)\n    H1 = 1.0*H*H/(H+1)\n    xf = W1/Xmax\n    yf = H1/Ymax\n    coords[:,1] *= yf\n    coords[:,0] *= xf\n    coords_int = np.round(coords).astype(np.int32)\n    return coords_int","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.442634Z","iopub.execute_input":"2022-06-13T02:13:49.443363Z","iopub.status.idle":"2022-06-13T02:13:49.450141Z","shell.execute_reply.started":"2022-06-13T02:13:49.443322Z","shell.execute_reply":"2022-06-13T02:13:49.449486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _get_xmax_ymin(grid_sizes_panda, imageId):\n    xmax, ymin = grid_sizes_panda[grid_sizes_panda.ImageId == imageId].iloc[0,1:].astype(float)\n    return (xmax,ymin)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.451491Z","iopub.execute_input":"2022-06-13T02:13:49.452347Z","iopub.status.idle":"2022-06-13T02:13:49.463644Z","shell.execute_reply.started":"2022-06-13T02:13:49.452307Z","shell.execute_reply":"2022-06-13T02:13:49.462881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _get_polygon_list(wkt_list_pandas, imageId, cType):\n    df_image = wkt_list_pandas[wkt_list_pandas.ImageId == imageId]\n    multipoly_def = df_image[df_image.ClassType == cType].MultipolygonWKT\n    polygonList = None\n    if len(multipoly_def) > 0:\n        assert len(multipoly_def) == 1\n        polygonList = wkt_loads(multipoly_def.values[0])\n    return polygonList\n","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.464777Z","iopub.execute_input":"2022-06-13T02:13:49.465693Z","iopub.status.idle":"2022-06-13T02:13:49.473909Z","shell.execute_reply.started":"2022-06-13T02:13:49.465652Z","shell.execute_reply":"2022-06-13T02:13:49.472908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _get_and_convert_contours(polygonList, raster_img_size, xymax):\n    perim_list = []\n    interior_list = []\n    if polygonList is None:\n        return None\n    for k in range(len(polygonList)):\n        poly = polygonList[k]\n        perim = np.array(list(poly.exterior.coords))\n        perim_c = _convert_coordinates_to_raster(perim, raster_img_size, xymax)\n        perim_list.append(perim_c)\n        for pi in poly.interiors:\n            interior = np.array(list(pi.coords))\n            interior_c = _convert_coordinates_to_raster(interior, raster_img_size, xymax)\n            interior_list.append(interior_c)\n    return perim_list,interior_list","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.475327Z","iopub.execute_input":"2022-06-13T02:13:49.475688Z","iopub.status.idle":"2022-06-13T02:13:49.485865Z","shell.execute_reply.started":"2022-06-13T02:13:49.475633Z","shell.execute_reply":"2022-06-13T02:13:49.485195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef _plot_mask_from_contours(raster_img_size, contours, class_value = 1):\n    img_mask = np.zeros(raster_img_size,np.uint8)\n    if contours is None:\n        return img_mask\n    perim_list,interior_list = contours\n    cv2.fillPoly(img_mask,perim_list,class_value)\n    cv2.fillPoly(img_mask,interior_list,0)\n    return img_mask","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.48761Z","iopub.execute_input":"2022-06-13T02:13:49.488199Z","iopub.status.idle":"2022-06-13T02:13:49.498148Z","shell.execute_reply.started":"2022-06-13T02:13:49.488157Z","shell.execute_reply":"2022-06-13T02:13:49.497456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_mask_for_image_and_class(raster_size, imageId, class_type, grid_sizes_panda,\n                                     wkt_list_pandas):\n    xymax = _get_xmax_ymin(grid_sizes_panda,imageId)\n    polygon_list = _get_polygon_list(wkt_list_pandas,imageId,class_type)\n    contours = _get_and_convert_contours(polygon_list,raster_size,xymax)\n    mask = _plot_mask_from_contours(raster_size,contours,1)\n    return mask","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.499512Z","iopub.execute_input":"2022-06-13T02:13:49.500223Z","iopub.status.idle":"2022-06-13T02:13:49.509779Z","shell.execute_reply.started":"2022-06-13T02:13:49.500156Z","shell.execute_reply":"2022-06-13T02:13:49.509025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inDir = '../input/dstl-satellite-imagery-feature-detection'\ndf = pd.read_csv(inDir + '/train_wkt_v4.csv.zip')\ngs = pd.read_csv(inDir + '/grid_sizes.csv.zip', names=['ImageId', 'Xmax', 'Ymin'], skiprows=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:49.511692Z","iopub.execute_input":"2022-06-13T02:13:49.512586Z","iopub.status.idle":"2022-06-13T02:13:50.465404Z","shell.execute_reply.started":"2022-06-13T02:13:49.512542Z","shell.execute_reply":"2022-06-13T02:13:50.464549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gs.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:50.469917Z","iopub.execute_input":"2022-06-13T02:13:50.470298Z","iopub.status.idle":"2022-06-13T02:13:50.491356Z","shell.execute_reply.started":"2022-06-13T02:13:50.470236Z","shell.execute_reply":"2022-06-13T02:13:50.490405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:50.492531Z","iopub.execute_input":"2022-06-13T02:13:50.492907Z","iopub.status.idle":"2022-06-13T02:13:50.512391Z","shell.execute_reply.started":"2022-06-13T02:13:50.492878Z","shell.execute_reply":"2022-06-13T02:13:50.511489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gs.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:50.513552Z","iopub.execute_input":"2022-06-13T02:13:50.51386Z","iopub.status.idle":"2022-06-13T02:13:50.519639Z","shell.execute_reply.started":"2022-06-13T02:13:50.513833Z","shell.execute_reply":"2022-06-13T02:13:50.518667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:50.52082Z","iopub.execute_input":"2022-06-13T02:13:50.521451Z","iopub.status.idle":"2022-06-13T02:13:50.530348Z","shell.execute_reply.started":"2022-06-13T02:13:50.52141Z","shell.execute_reply":"2022-06-13T02:13:50.529298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    mask = generate_mask_for_image_and_class((500,500),df.ImageId[i],4,gs,df)\n    cv2.imwrite(\"mask.png\",mask*255)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-13T02:13:50.531529Z","iopub.execute_input":"2022-06-13T02:13:50.53206Z","iopub.status.idle":"2022-06-13T02:13:50.62463Z","shell.execute_reply.started":"2022-06-13T02:13:50.531979Z","shell.execute_reply":"2022-06-13T02:13:50.623932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}