{"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 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-23T05:41:30.150516Z","iopub.execute_input":"2023-06-23T05:41:30.150933Z","iopub.status.idle":"2023-06-23T05:41:30.157609Z","shell.execute_reply.started":"2023-06-23T05:41:30.150901Z","shell.execute_reply":"2023-06-23T05:41:30.156037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n\n#get_image(0)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:34:14.321392Z","iopub.execute_input":"2023-06-23T05:34:14.321964Z","iopub.status.idle":"2023-06-23T05:34:14.634177Z","shell.execute_reply.started":"2023-06-23T05:34:14.321909Z","shell.execute_reply":"2023-06-23T05:34:14.632883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot(filtername,img,blur,edges,img_l,human_pixel_mask):\n    \n    \n    plt.figure(figsize=(18, 6))\n    ax = plt.subplot(1, 5,1)\n    ax.imshow(img, interpolation='none')\n    ax.set_title(\"img\")\n\n    ax = plt.subplot(1, 5,2)\n    ax.imshow(blur, cmap='gray',interpolation='none')\n    ax.set_title(filtername)\n\n    ax = plt.subplot(1, 5,3)\n    ax.imshow(edges, interpolation='none')\n    ax.set_title('edges')\n    \n    ax = plt.subplot(1, 5,4)\n    ax.imshow(img_l, interpolation='none')\n    ax.set_title('imglines')\n    \n    ax = plt.subplot(1, 5,5)\n    ax.imshow(human_pixel_mask, interpolation='none')\n    ax.set_title('target')","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:40:01.29655Z","iopub.execute_input":"2023-06-23T05:40:01.297025Z","iopub.status.idle":"2023-06-23T05:40:01.308281Z","shell.execute_reply.started":"2023-06-23T05:40:01.296988Z","shell.execute_reply":"2023-06-23T05:40:01.306495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgindex2=widgets.IntSlider(min=0,max=30000,value=1, description=\"imageindex\")\ndef process_variables(imgindex2):\n    img,human_pixel_mask=get_image(imgindex2)\n    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    #gray=1-gray\n    \n    #################Gaussian###################\n    blur = cv2.GaussianBlur(gray, (3, 3), 10)\n    blur=blur*255\n    blur=np.uint8(blur)\n    edges = cv2.Canny(blur, 0, 50)\n    lines=cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=2,maxLineGap=10)\n    img_l=blur.copy()\n    #print(lines)\n    if type(lines).__name__ != 'ndarray':\n        lines=[]\n    for line in lines:\n        x1,y1,x2,y2=line[0]\n        cv2.line(img_l,(x1,y1),(x2,y2),(0,0,255),3)\n    \n    plot(\"GaussianBlur\",img,blur,edges,img_l,human_pixel_mask)\n    \n    #################boxFilter###################\n    filtered_image = cv2.boxFilter(gray, -1, (3, 3))\n    filtered_image=filtered_image*255\n    filtered_image=np.uint8(filtered_image)\n    edges = cv2.Canny(filtered_image, 0, 50)\n    lines=cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=2,maxLineGap=10)\n    img_l=filtered_image.copy()\n    #print(type(lines).__name__,type(lines).__name__!='ndarray')\n    if type(lines).__name__ != 'ndarray':\n        lines=[]\n    for line in lines:\n        x1,y1,x2,y2=line[0]\n        cv2.line(img_l,(x1,y1),(x2,y2),(0,0,255),3)\n    \n    plot(\"boxFilter\",img,filtered_image,edges,img_l,human_pixel_mask)\n    \n    #################medianBlur###################\n    filtered_image = cv2.medianBlur(gray, 3)\n    filtered_image=filtered_image*255\n    filtered_image=np.uint8(filtered_image)\n    edges = cv2.Canny(filtered_image, 0, 50)\n    lines=cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=2,maxLineGap=10)\n    img_l=filtered_image.copy()\n    #print(lines)\n    if type(lines).__name__ != 'ndarray':\n        lines=[]\n    for line in lines:\n        x1,y1,x2,y2=line[0]\n        cv2.line(img_l,(x1,y1),(x2,y2),(0,0,255),3)\n    \n    plot(\"medianBlur\",img,filtered_image,edges,img_l,human_pixel_mask)\n    \n    \n    \n    #################bilateral filter###################\n    filtered_image = cv2.bilateralFilter(gray, d=9, sigmaColor=75, sigmaSpace=75)\n    filtered_image=filtered_image*255\n    filtered_image=np.uint8(filtered_image)\n    edges = cv2.Canny(filtered_image, 0, 50)\n    lines=cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=2,maxLineGap=10)\n    img_l=filtered_image.copy()\n    #print(lines)\n    if type(lines).__name__ != 'ndarray':\n        lines=[]\n    for line in lines:\n        x1,y1,x2,y2=line[0]\n        cv2.line(img_l,(x1,y1),(x2,y2),(0,0,255),3)\n    \n    plot(\"bilateralFilter\",img,filtered_image,edges,img_l,human_pixel_mask)\n    sys.exit()\n    \n    \n# Create the interactive interface using interact\ninteract_obj = widgets.interact(process_variables,imgindex2=imgindex2)\n# Display the widgets\ndisplay.display(interact_obj)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:41:35.725781Z","iopub.execute_input":"2023-06-23T05:41:35.726179Z","iopub.status.idle":"2023-06-23T05:41:40.883762Z","shell.execute_reply.started":"2023-06-23T05:41:35.726148Z","shell.execute_reply":"2023-06-23T05:41:40.882548Z"},"trusted":true},"execution_count":null,"outputs":[]}]}