{"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-23T19:38:33.978072Z","iopub.execute_input":"2023-06-23T19:38:33.978434Z","iopub.status.idle":"2023-06-23T19:38:34.316438Z","shell.execute_reply.started":"2023-06-23T19:38:33.978406Z","shell.execute_reply":"2023-06-23T19:38:34.315136Z"},"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-23T19:38:38.661511Z","iopub.execute_input":"2023-06-23T19:38:38.661921Z","iopub.status.idle":"2023-06-23T19:38:38.993491Z","shell.execute_reply.started":"2023-06-23T19:38:38.66189Z","shell.execute_reply":"2023-06-23T19:38:38.992373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import MeanShift, estimate_bandwidth\n# filter to reduce noise\nimgindex2=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    img = cv2.medianBlur(gray, 3)\n    print(img.shape)\n    img1=img*255\n    # flatten the image\n    flat_image = img1.reshape((-1,1))\n    flat_image = np.float32(flat_image)\n\n    # meanshift\n    bandwidth = estimate_bandwidth(flat_image, quantile=.06, n_samples=3000)\n    print(bandwidth)\n    ms = MeanShift(bandwidth=bandwidth, max_iter=5, bin_seeding=True)\n    ms.fit(flat_image)\n    labeled=ms.labels_\n\n\n    # get number of segments\n    segments = np.unique(labeled)\n    print('Number of segments: ', segments.shape)\n\n    segmented_image = labeled.reshape(img.shape[:2])\n    \n    filtered_image=np.uint8(segmented_image)\n    segmented_image=segmented_image/segments.shape[0]\n    print(segmented_image.min(),segmented_image.max())\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=img.copy()\n    \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    \n    plt.figure(figsize=(18, 6))\n    ax = plt.subplot(1, 3,1)\n    ax.imshow(img, interpolation='none')\n    ax.set_title('blur1')\n\n    ax = plt.subplot(1, 3,2)\n    ax.imshow(1-segmented_image, interpolation='none')\n    ax.set_title('mean shif ')\n    \n    ax = plt.subplot(1, 3,3)\n    ax.imshow(human_pixel_mask, interpolation='none')\n    ax.set_title('target')\n    sys.exit()\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-23T19:44:30.264819Z","iopub.execute_input":"2023-06-23T19:44:30.26525Z","iopub.status.idle":"2023-06-23T19:44:32.662402Z","shell.execute_reply.started":"2023-06-23T19:44:30.265218Z","shell.execute_reply":"2023-06-23T19:44:32.661262Z"},"trusted":true},"execution_count":null,"outputs":[]}]}