{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom skimage.util import montage as montage2d\nfrom skimage.color import rgb2hsv, gray2rgb, rgb2gray, label2rgb\nfrom skimage.feature import greycomatrix, greycoprops\nfrom skimage.io import imread\nfrom skimage import img_as_ubyte\nfrom skimage.filters import threshold_otsu\nfrom skimage.measure import regionprops\nimport matplotlib.patches as mpatches\nfrom skimage.morphology import label\nfrom skimage.segmentation import slic\nfrom collections import namedtuple\nfrom skimage import measure\nimport regex as re\nimport os\nfrom glob import glob\n\nimport warnings  \nwarnings.filterwarnings('ignore')\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nn_samples = 50\n\nbase_img_dir = os.path.join('..', 'input')\nall_tails = glob(os.path.join(base_img_dir, '*', 'train', 'train', '*.jpg'))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def create_binary_image_with_threshold(image):\n    threshold = threshold_otsu(image)\n    return (image < threshold).astype(np.float32)\n\ndef add_box(labelled_image, color='red'):\n        maxArea = 0\n        for region in regionprops(labelled_image):\n            if region.area > maxArea:\n                maxArea = region.area\n                maxRegion = region\n            \n        minr, minc, maxr, maxc = maxRegion.bbox\n        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,\n                          fill=False, edgecolor=color, linewidth=2)\n        return rect\n    \ndef image_name_parser(c_path):\n    file_name_pattern = re.compile(r'\\.\\.\\/input\\/whale-categorization-playground\\/train/train\\/(?P<name>.+\\.jpg)')\n    match = file_name_pattern.match(c_path)\n    if match is not None:\n        return match.group('name')\n\ndef flatten_superpixel_image(color_image, superpixel_image):\n    flat_image = color_image.copy()\n    for s_idx in np.unique(superpixel_image.ravel()):\n            flat_image[superpixel_image == s_idx] = np.mean(\n            flat_image[superpixel_image == s_idx])\n    return rgb2gray(flat_image.copy())\n\ndef create_hsv_dict(hsv_image):\n    hsv_dict = {}\n    hsv_dict['h'] = hsv_image[:, :, 0]\n    hsv_dict['s'] = hsv_image[:, :, 1]\n    hsv_dict['v'] = hsv_image[:, :, 2]\n    return hsv_dict\n\ndef create_binary_with_hsv_threshold(hsv_dict):\n    try:\n        value_threshold = threshold_otsu(hsv_dict['v'])\n        hue_threshold = threshold_otsu(hsv_dict['h'])\n        binary_image = 1.0*((hsv_dict['v'] < value_threshold) | (hsv_dict['h'] < hue_threshold))\n        return binary_image\n    except Exception as e:\n        print(e)\n        return None\ndef bounding_rectangle(list):\n    x0, y0 = list[0]\n    x1, y1 = x0, y0\n    for x,y in list[1:]:\n        x0 = min(x0, x)\n        y0 = min(y0, y)\n        x1 = max(x1, x)\n        y1 = max(y1, y)\n    return x0,y0,x1,y1\n\ndef bb_intersection_over_union(rectA, rectB):\n    if rectB is None or rectA is None:\n        return 0\n    boxA = rectA.get_bbox().get_points().flatten()\n    boxB = rectB.get_bbox().get_points().flatten()\n    # determine the (x, y)-coordinates of the intersection rectangle\n    xA = max(boxA[0], boxB[0])\n    yA = max(boxA[1], boxB[1])\n    xB = min(boxA[2], boxB[2])\n    yB = min(boxA[3], boxB[3])\n \n    # compute the area of intersection rectangle\n    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)\n \n    # compute the area of both the prediction and ground-truth\n    # rectangles\n    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)\n    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)\n \n    # compute the intersection over union by taking the intersection\n    # area and dividing it by the sum of prediction + ground-truth\n    # areas - the interesection area\n    iou = interArea / float(boxAArea + boxBArea - interArea)\n    \n    # return the intersection over union value\n    return iou\n\ndef check_array_for_all_zeros(hsv_dict):\n    return not np.any(hsv_dict['v']) or not np.any(hsv_dict['h']) or not np.any(hsv_dict['s'])\n\ndef crop(image, rect):\n    return image[rect.get_y():rect.get_y()+rect.get_height() , rect.get_x():rect.get_x()+rect.get_width(), :]\n\ndef montage_nd(in_img):\n    if len(in_img.shape) > 3:\n        return montage2d(np.stack([montage_nd(x_slice) for x_slice in in_img], 0))\n    elif len(in_img.shape) == 3:\n        return montage2d(in_img)\n    else:\n        warn('Input less than 3d image, returning original', RuntimeWarning)\n        return in_img\n    \ndef calc_coomatrix(in_img):\n    return greycomatrix(image=in_img,\n                        distances=dist_list,\n                        angles=angle_list,\n                        levels=4)\n\n\ndef coo_tensor_to_df(x): return pd.DataFrame(\n    np.stack([x.ravel()]+[c_vec.ravel() for c_vec in np.meshgrid(range(x.shape[0]),\n                                                                 range(\n                                                                     x.shape[1]),\n                                                                 dist_list,\n                                                                 angle_list,\n                                                                 indexing='xy')], -1),\n    columns=['E', 'i', 'j', 'd', 'theta'])\n\ndef calculate_glcm_properties(image):\n    grayco_prop_list = ['contrast', 'dissimilarity',\n                    'homogeneity', 'energy',\n                    'correlation', 'ASM']\n    \n    out_row = {}\n    glcm = greycomatrix(image, [5], [0], 256, symmetric=True, normed=True)\n    for c_prop in grayco_prop_list:\n        out_row[c_prop] = greycoprops(glcm, c_prop)[0, 0]\n    out_row['bw_ratio'] = np.mean(image)\n    return out_row\n    \nDetection = namedtuple(\"Detection\", [\"image_name\",\"image\", \"ml\", \"color\", \"hsv\", \"iou_color\", \"iou_hsv\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"all_tails_dict = {}\nml_box_dict = {}\nfor c_path in all_tails:\n    image_name = image_name_parser(c_path)\n    all_tails_dict[image_name] = c_path\n\nwith open('../input/humpback-whale-identification-fluke-location/cropping.txt', 'rt') as f: data = f.read().split('\\n')[:-1]\n\ndata = [line.split(',') for line in data]\ndata = [(p,[(int(coord[i]),int(coord[i+1])) for i in range(0,len(coord),2)]) for p,*coord in data]\nfor filename, coordinates in data:\n    x0,y0,x1,y1 = bounding_rectangle(coordinates)\n    box = mpatches.Rectangle((x0,y0), x1-x0, y1-y0, fill=False, color='green')\n    ml_box_dict[filename] = box","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"detection_list = []\nout_df_list = []\n\n\nfor image_name in np.random.choice(list(ml_box_dict.keys()), size=n_samples):\n#for image_name in list(ml_box_dict.keys()):\n    c_path = all_tails_dict[image_name]\n    image = imread(c_path)\n    if len(np.shape(image)) == 3:\n        pass\n    else:\n        image = gray2rgb(image)\n    # bounding box analysis\n    working_img = label(create_binary_image_with_threshold(flatten_superpixel_image(image, slic(image, n_segments=250))))\n    color_box = add_box(working_img)\n    hsv_dict = create_hsv_dict(rgb2hsv(image)[:, :, :])\n\n    if check_array_for_all_zeros(hsv_dict):\n        hsv_box = None\n    else: \n        hsv_box = add_box(label(create_binary_with_hsv_threshold(hsv_dict)), 'orange')\n\n    detect = Detection(image_name, \n                                image, \n                                ml_box_dict[image_name], \n                                color_box, \n                                hsv_box, \n                                bb_intersection_over_union(ml_box_dict[image_name], color_box), \n                                bb_intersection_over_union(ml_box_dict[image_name], hsv_box))\n\n\n\n#image analaysis\n    image_ml = img_as_ubyte(rgb2gray(crop(detect.image, detect.ml)))\n    image_color = img_as_ubyte(rgb2gray(crop(detect.image, detect.color)))\n\n\n    \n    ml_out_row = calculate_glcm_properties(image_ml)\n    ml_out_row['type'] = 'ML'\n    ml_out_row['iou'] = 1\n    out_df_list += [ml_out_row]\n\n\n    color_out_row = calculate_glcm_properties(image_color)\n    color_out_row['type'] = 'Color'\n    color_out_row['iou'] = detect.iou_color\n    out_df_list += [color_out_row]\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"out_df = pd.DataFrame(out_df_list)\nsns.pairplot(out_df,\n             x_vars=['contrast', 'dissimilarity',\n                    'homogeneity', 'energy',\n                    'correlation', 'ASM', 'bw_ratio'],\n             y_vars=['iou'],\n             hue='type',\n             kind=\"reg\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"detection_list = []\nout_df_list = []\nimage_list = []\nfor image_name in np.random.choice(list(ml_box_dict.keys()), size=5):\n#for image_name in list(ml_box_dict.keys()):\n    c_path = all_tails_dict[image_name]\n    image = imread(c_path)\n    if len(np.shape(image)) == 3:\n        pass\n    else:\n        image = gray2rgb(image)\n    image_list.append(image)\n    x_list=[]\n    y_list=[]\n    # bounding box analysis\n    for segments in np.arange(50,1000,50):\n        working_img = label(create_binary_image_with_threshold(flatten_superpixel_image(image, slic(image, n_segments=segments))))\n        color_box = add_box(working_img)\n        hsv_dict = create_hsv_dict(rgb2hsv(image)[:, :, :])\n\n        if check_array_for_all_zeros(hsv_dict):\n            hsv_box = None\n        else: \n            hsv_box = add_box(label(create_binary_with_hsv_threshold(hsv_dict)), 'orange')\n\n        detect = Detection(image_name, \n                                    image, \n                                    ml_box_dict[image_name], \n                                    color_box, \n                                    hsv_box, \n                                    bb_intersection_over_union(ml_box_dict[image_name], color_box), \n                                    bb_intersection_over_union(ml_box_dict[image_name], hsv_box))\n        \n        image_color = img_as_ubyte(rgb2gray(crop(detect.image, detect.color)))\n        #print(detect.image_name, detect.iou_color, np.mean(image_color))\n        \n        x_list.append(segments)\n        y_list.append(detect.iou_color)\n    \n    plt.plot(x_list, y_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for image in image_list: \n    fig, ax = plt.subplots()\n    ax.imshow(image, cmap='bone')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"    #if detect.hsv is not None:\n    #    image_hsv = img_as_ubyte(rgb2gray(crop(detect.image, detect.hsv)))\n    #    hsv_out_row = calculate_glcm_properties(image_hsv)\n    #    hsv_out_row['type'] = 'HSV'\n    #    hsv_out_row['iou'] = detect.iou_hsv\n    #    out_df_list += [hsv_out_row]\n\n    \n    #fig, ax = plt.subplots()\n    #ax.imshow(image_hsv, cmap='bone')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}