{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"8f4a358d-3704-8dcc-0b8a-76fd2644233a"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"23391bdf-0875-8ddc-c2cb-9f9be6ee50d8"},"outputs":[],"source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport skimage.feature\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelBinarizer\n%matplotlib inline\n\n#from subprocess import check_output\n#print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))"},{"cell_type":"markdown","metadata":{"_cell_guid":"1d32c323-09e5-bfd8-5b66-40ecd4659aca"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d4bd9a0d-273f-c73a-8305-1790b2be33b4"},"outputs":[],"source":"file_names = os.listdir(\"../input/Train/\")\nfile_names = sorted(file_names, key=lambda \n                    item: (int(item.partition('.')[0]) if item[0].isdigit() else float('inf'), item))\n\n# select a subset of files to run on\nfile_names = file_names[0:1]"},{"cell_type":"markdown","metadata":{"_cell_guid":"a8b8965e-6c8f-6afb-8bfe-7604ef118211"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"0ea5d899-1205-bd3e-3c4b-5bf8d9eef8d6"},"outputs":[],"source":"Sub_Im_Size = (416,416)\n\nimage_tmp = cv2.imread(\"../input/TrainDotted/\" + file_names[0])\nimage_tmp = image_tmp[:Sub_Im_Size[1],:Sub_Im_Size[0],:]\nimage_tmp = cv2.absdiff(image_tmp,image_tmp)\n\nplt.imshow(cv2.cvtColor(image_tmp, cv2.COLOR_BGR2RGB))\ncv2.imwrite('sub_im_template.png',image_tmp)"},{"cell_type":"markdown","metadata":{"_cell_guid":"b7cb9afb-7da0-5557-a1b0-e40a862002a1"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ae85526b-c76e-5d82-49b8-519d6c527b5a"},"outputs":[],"source":"def get_blobs(filename):\n    # read the Train and Train Dotted images\n    image_1 = cv2.imread(\"../input/TrainDotted/\" + filename)\n    image_2 = cv2.imread(\"../input/Train/\" + filename)\n    \n    # absolute difference between Train and Train Dotted\n    image_3 = cv2.absdiff(image_1,image_2)\n    \n    # mask out blackened regions from Train Dotted\n    mask_1 = cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY)\n    mask_1[mask_1 < 20] = 0\n    mask_1[mask_1 > 0] = 255\n    \n    mask_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)\n    mask_2[mask_2 < 20] = 0\n    mask_2[mask_2 > 0] = 255\n    \n    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_1)\n    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_2) \n    \n    # convert to grayscale to be accepted by skimage.feature.blob_log\n    image_3 = cv2.cvtColor(image_3, cv2.COLOR_BGR2GRAY)\n    \n    # detect blobs\n    blobs = skimage.feature.blob_log(image_3, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)\n    \n    return blobs"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1bbb8716-90c4-efbf-893b-044d3b578bba"},"outputs":[],"source":"def get_xy_range_basic(size):\n    ### x_left, x_right, y_up, y_down\n    return (size,size,size,size)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"549b4a7d-bb87-4b4c-abf8-b0f566368d91"},"outputs":[],"source":"def parse_image(filename):\n    ### get original image\n    ori_image = cv2.imread(\"../input/Train/\" + filename)\n    cnt = 0\n    \n    ### get coordinate of all sea lions\n    Dict_range = {}\n    blobs = get_blobs(filename)\n    \n    for blob in blobs:\n        # get the coordinates for each blob\n        y, x, s = blob\n        \n        xy_range = get_xy_range_basic(size=16)\n        Dict_range[(x,y)] = xy_range\n    \n    ### output sub_image and annotation file for each blob\n    Delete_Key_List = []\n    for key in list(Dict_range.keys()):       \n        if(key in Dict_range):\n            # add cnt for new sub_image name\n            cnt += 1\n            \n            # get x, y, xy_range in original image\n            main_x = key[0]\n            main_y = key[1]\n            xy_range = Dict_range[key]\n            \n            ### get basic sub_image\n            sub_image = cv2.imread('sub_im_template.png')            \n            sub_x_center = sub_image.shape[1]/2\n            sub_y_center = sub_image.shape[0]/2\n            sub_image[sub_y_center-xy_range[2]:sub_y_center+xy_range[3], sub_x_center-xy_range[0]:sub_x_center+xy_range[1], :] = ori_image[main_y-xy_range[2]:main_y+xy_range[3], main_x-xy_range[0]:main_x+xy_range[1], :]\n            del Dict_range[key]\n            \n            ### include other sea lion\n            # max min coordinate for including image based on origin image\n            x_min = max(main_x - sub_image.shape[1]/2 + 1, 0)\n            x_max = min(main_x + sub_image.shape[1]/2 - 1, ori_image.shape[1])\n            y_min = max(main_y - sub_image.shape[0]/2 + 1, 0)\n            y_max = min(main_y + sub_image.shape[0]/2 - 1, ori_image.shape[0])\n            \n            for ex_key in list(Dict_range.keys()):\n                if(ex_key[0] > x_min and ex_key[0] < x_max and ex_key[1] > y_min and ex_key[1] < y_max):\n                    ### coordinate of ex_sea_lion in origin image\n                    ex_range = Dict_range[ex_key]\n                    ex_left  = ex_key[0] - ex_range[0]\n                    ex_right = ex_key[0] + ex_range[1]\n                    ex_up    = ex_key[1] - ex_range[2]\n                    ex_down  = ex_key[1] + ex_range[3]\n                    if(ex_left > x_min and ex_right < x_max and ex_up > y_min and ex_down < y_max):\n                        ### sub_image's coordinate where ex_sea_lion put  \n                        in_up    = sub_y_center - main_y + ex_key[1] - ex_range[2]\n                        in_down  = sub_y_center - main_y + ex_key[1] + ex_range[3]\n                        in_left  = sub_x_center - main_x + ex_key[0] - ex_range[0]\n                        in_right = sub_x_center - main_x + ex_key[0] + ex_range[1]\n                        sub_image[ in_up:in_down, in_left:in_right, :] = ori_image[ex_up:ex_down, ex_left:ex_right, :]\n                        del Dict_range[ex_key]\n            \n            #cv2.imwrite('/home/paperspace/Project/Sealion/JPEGImages/{0}_{1}.png'.formate(filename[:-4], cnt),sub_image)\n            cv2.imwrite('./JPEGImages/{0}_{1}.png'.format(filename[:-4], cnt),sub_image)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"77c82dcc-738e-e35c-0983-4c2ae61c1d3f"},"outputs":[],"source":"parse_image(file_names[0])"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}