{"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":"markdown","source":"# Import required packages","metadata":{}},{"cell_type":"code","source":"import glob,os\nimport numpy as np\nfrom matplotlib import animation\nimport matplotlib.pyplot as plt\nfrom IPython import display\nimport cv2\nfrom ipywidgets import interact, widgets\nimport ipywidgets as widgets\nimport sys","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-09T21:25:07.747916Z","iopub.execute_input":"2023-06-09T21:25:07.748541Z","iopub.status.idle":"2023-06-09T21:25:07.758641Z","shell.execute_reply.started":"2023-06-09T21:25:07.748496Z","shell.execute_reply":"2023-06-09T21:25:07.756155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# get image and ground truth","metadata":{}},{"cell_type":"code","source":"BASE_DIR = '/kaggle/input/google-research-identify-contrails-reduce-global-warming/train/'\nrecord_ids= sorted(glob.glob(os.path.join(BASE_DIR, '*')))\ndef get_image(i):\n    #print(record_ids[i])\n    with open(os.path.join(record_ids[i], 'band_11.npy'), 'rb') as f:\n        band11 = np.load(f)\n    with open(os.path.join( record_ids[i], 'band_14.npy'), 'rb') as f:\n        band14 = np.load(f)\n    with open(os.path.join( record_ids[i] , 'band_15.npy'), 'rb') as f:\n        band15 = np.load(f)\n    with open(os.path.join(record_ids[i] , 'human_pixel_masks.npy'), 'rb') as f:\n        human_pixel_mask = np.load(f)\n    with open(os.path.join(record_ids[i] , 'human_individual_masks.npy'), 'rb') as f:\n        human_individual_mask = np.load(f)\n    \n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n\n    def normalize_range(data, bounds):\n        \"\"\"Maps data to the range [0, 1].\"\"\"\n        return (data - bounds[0]) / (bounds[1] - bounds[0])\n        #return data\n\n    r = normalize_range(band15 - band14, _TDIFF_BOUNDS)\n    g = normalize_range(band14 - band11, _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(band14, _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    return false_color[...,4] ,human_pixel_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-09T21:25:07.762018Z","iopub.execute_input":"2023-06-09T21:25:07.763239Z","iopub.status.idle":"2023-06-09T21:25:07.900278Z","shell.execute_reply.started":"2023-06-09T21:25:07.763185Z","shell.execute_reply":"2023-06-09T21:25:07.89906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hmin=widgets.FloatSlider(min=0,max=300,value=0,description=\"hmin\")\nhmax=widgets.FloatSlider(min=0,max=300,value=54,description=\"hmax\")\n\nsmin=widgets.FloatSlider(min=0,max=1,value=0,description=\"smin\")\nsmax=widgets.FloatSlider(min=0,max=1,value=1, description=\"smax\")\n\nvmin=widgets.FloatSlider(min=0,max=1,value=0 ,description=\"vmin\")\nvmax=widgets.FloatSlider(min=0,max=1,value=1, description=\"vmax\")\n\nimgindex=widgets.IntSlider(min=0,max=30000,value=1, description=\"imageindex\")\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T21:25:07.902465Z","iopub.execute_input":"2023-06-09T21:25:07.902973Z","iopub.status.idle":"2023-06-09T21:25:07.939734Z","shell.execute_reply.started":"2023-06-09T21:25:07.902938Z","shell.execute_reply":"2023-06-09T21:25:07.938439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_variables(hmin,hmax,smin,smax,vmin,vmax,imgindex):\n    \n    img,human_pixel_mask=get_image(imgindex)\n    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n    \n    lbmin=np.stack([np.min(hsv[i], axis=0)  for i in range(3)] )\n    upmax=np.stack([np.max(hsv[i], axis=0)  for i in range(3)] )\n    lbmin=np.min(lbmin,axis=0)\n    upmax=np.max(upmax,axis=0)\n    # print(\"#\"*8)\n    print(lbmin,upmax)\n    \n    \n    lb=np.array([hmin,smin,vmin])\n    up=np.array([hmax,smax,vmax])\n    print(lb,up)\n    \n    mask=cv2.inRange(hsv,lb,up)\n    plt.figure(figsize=(18, 6))\n    ax = plt.subplot(1, 4, 1)\n    ax.imshow(img, interpolation='none')\n    ax.set_title('img')\n    res=cv2.bitwise_and(img,img ,mask=mask)\n    ax = plt.subplot(1, 4,2)\n    ax.imshow(res, interpolation='none')\n    ax.set_title('hsvres')\n    ax = plt.subplot(1, 4, 3)\n    ax.imshow(mask, interpolation='none')\n    ax.set_title('hsvmask')\n    ax = plt.subplot(1, 4, 4)\n    ax.imshow(human_pixel_mask, interpolation='none')\n    ax.set_title('ground thruth')\n    sys.exit()\n\n        #print(lb,up)\n\n# Create the interactive interface using interact\ninteract_obj = widgets.interact(process_variables,hmin=hmin,hmax=hmax,smin=smin,smax=smax,vmin=vmin,vmax=vmax ,imgindex=imgindex)\n# Display the widgets\ndisplay.display(interact_obj)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T21:25:07.941926Z","iopub.execute_input":"2023-06-09T21:25:07.942324Z","iopub.status.idle":"2023-06-09T21:25:09.406529Z","shell.execute_reply.started":"2023-06-09T21:25:07.942291Z","shell.execute_reply":"2023-06-09T21:25:09.405182Z"},"trusted":true},"execution_count":null,"outputs":[]}]}