{"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 scipy.stats as stats\nfrom pathlib import Path\nimport glob\nimport pickle\nimport matplotlib.pyplot as plt\nimport joblib\n\nimport random\nimport os\n\nimport cv2\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This notebook is pretty messy and I wouldn't normally share it, but someone asked about how I created what I called \"threshold images\" in my notebook (https://www.kaggle.com/therocket290/indoor-navigation-push-to-hallway-post-process?scriptVersionId=57642694). I don't have time to edit it, so I am just sharing this as is. \n\nI wanted to create images of each floor that made it easy to push predicted locations to the hallways. I used cv2.threshold from OpenCV to create these images. The hallways are white and the rest is black, but I had trouble making the outside of the building black as well. You can see my failed attempt at drawing a countour around the entire building. I wish I had more time to work on it, but I am currently too busy with work and family.","metadata":{}},{"cell_type":"code","source":"def view(array):\n    img = Image.fromarray(array)\n    img.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_dir = '../input/indoor-location-navigation/metadata'\nbuilding_paths = os.listdir(meta_dir)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building_paths = []\nfor path in os.listdir('../input/wifi-features-all'):\n    if (len(path.split('_')[0])>20) and (not path.split('_')[0] in building_paths):\n        building_paths.append(path.split('_')[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building_paths = building_paths[0:1]\n\ngray_list = []\nthresh_list = []\nthresh_list2 = []\nmask_list = []\nfloor_list = []\nblur_list = []\ncombine_list = []\n#building_list = []\ncontours_list = []\n\n\nlight_white = (0, 0, 200)\ndark_white = (100, 100, 250)\nfor building in building_paths:\n    grays = []\n    threshes = []\n    threshes2 = []\n    masks = []\n    floors_in_building = []\n    blurs = []\n    combines = []\n    #buildings = []\n    floor_contours = []\n    \n    #buildings.append(building)\n    floors = os.listdir(os.path.join(meta_dir,building))\n    for floor in floors:\n        image_path = glob.glob(os.path.join(meta_dir,building,floor,'*.png'))\n        img = Image.open(image_path[0]).convert('LA')\n        gray = np.asarray(img)\n        blur = cv2.GaussianBlur(gray,(5,5),500)\n        im = cv2.imread(image_path[0], cv2.IMREAD_UNCHANGED) #cv2.IMREAD_UNCHANGED\n        #im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)\n        #gray = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY)\n        flag, thresh = cv2.threshold(gray, 245, 255, cv2.THRESH_BINARY)\n        flag2, thresh2 = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)\n        #contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n        #contours = sorted(contours, key=cv2.contourArea,reverse=True)\n        hsv_im = cv2.cvtColor(im, cv2.COLOR_RGB2HSV)\n        mask_white = cv2.inRange(hsv_im, light_white, dark_white)\n        result = cv2.bitwise_and(im, im, mask=mask_white)\n        grays.append(gray)\n        threshes.append(thresh)\n        #masks.append(mask_white)\n        floors_in_building.append(floor)\n        #blurs.append(blur)\n        threshes2.append(thresh2)\n        combine = np.maximum(thresh[:,:,1],thresh2[:,:,1])\n        combines.append(combine)\n        \n        contours, hierarchy = cv2.findContours(thresh[:,:,1],cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n        contours = sorted(contours, key=cv2.contourArea,reverse=True)\n        floor_contours.append(contours)\n    \n    gray_list.append(grays)\n    thresh_list.append(threshes)\n    thresh_list2.append(threshes2)\n    combine_list.append(combines)\n    #mask_list.append(masks)\n    floor_list.append(floors_in_building)\n    #blur_list.append(blurs)\n    contours_list.append(floor_contours)\n    joblib.dump(threshes, 'thresh_list_'+str(building)+'.pkl')\n    joblib.dump(floor_list, 'floor_list_'+str(building)+'.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(thresh_list[0][0] != thresh_list2[0][0]).sum()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#combine_ex = np.maximum(thresh_list[0][0][:,:,1], thresh_list2[0][0][:,:,1])\ncombine_ex = np.maximum(thresh_list[0][0], thresh_list2[0][0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.fromarray(combine_ex)\nplt.imshow(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.fromarray(thresh_list2[0][0])\nplt.imshow(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.fromarray(thresh_list[0][0])\nplt.imshow(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = cv2.imread(image_path[0], cv2.IMREAD_UNCHANGED)\ncnt = contours[0]\ntest_cnt = cv2.drawContours(im, [cnt], 0, (0,255,0), 3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12, 12))\nplt.imshow(test_cnt)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fig, axs = plt.subplots(12, 2)\ntotal_floors = []\nfor k, building in enumerate(building_paths):\n    num_floors = len(floor_list[k])\n    for e, floor in enumerate(floor_list[k]):\n        #print('floors: ', num_floors)\n        total_floors.append(1)\n        fig, ax = plt.subplots(1,4,figsize=(12, 12))\n        plt.subplot(1,4,1)\n        plt.title(building+' '+floor)\n        array = thresh_list[k][e]\n        img = Image.fromarray(array)\n        plt.imshow(img)\n        plt.subplot(1,4,2)\n        array2 = gray_list[k][e]\n        img2 = Image.fromarray(array2)\n        plt.imshow(img2)\n        plt.subplot(1,4,3)\n        array3 = thresh_list2[k][e]\n        img3 = Image.fromarray(array3)\n        plt.imshow(img3)\n        plt.subplot(1,4,4)\n        array3 = combine_list[k][e]\n        img3 = Image.fromarray(array3)\n        plt.imshow(img3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}