{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1604240c-6f51-50e7-52e2-8127798f7baf"},"outputs":[],"source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport skimage.feature\n%matplotlib inline"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f32860bf-aee0-ec68-f0fb-8c5c1f54a421"},"outputs":[],"source":"classes = [\"adult_males\", \"subadult_males\", \"adult_females\", \"juveniles\", \"pups\", \"error\"]\n\nfile_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:2]\n\n# dataframe to store results in\ncount_df = pd.DataFrame(index=file_names, columns=classes).fillna(0)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e8a45a83-7a04-a38b-44df-c14cd7df16b0"},"outputs":[],"source":"for filename in file_names:\n    \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_4 = cv2.bitwise_or(image_3, image_3, mask=mask_1)\n    image_5 = cv2.bitwise_or(image_4, image_4, mask=mask_2) \n    \n    # convert to grayscale to be accepted by skimage.feature.blob_log\n    image_6 = cv2.cvtColor(image_5, cv2.COLOR_BGR2GRAY)\n    \n    # detect blobs\n    blobs = skimage.feature.blob_log(image_6, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)\n    \n    # prepare the image to plot the results on\n    image_7 = cv2.cvtColor(image_6, cv2.COLOR_GRAY2BGR)\n    \n    for blob in blobs:\n        # get the coordinates for each blob\n        y, x, s = blob\n        # get the color of the pixel from Train Dotted in the center of the blob\n        b,g,r = image_1[int(y)][int(x)][:]\n        \n        # decision tree to pick the class of the blob by looking at the color in Train Dotted\n        if r > 200 and b < 50 and g < 50: # RED\n            count_df[\"adult_males\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (0,0,255), 2)            \n        elif r > 200 and b > 200 and g < 50: # MAGENTA\n            count_df[\"subadult_males\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (250,10,250), 2)            \n        elif r < 100 and b < 100 and 150 < g < 200: # GREEN\n            count_df[\"pups\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (20,180,35), 2) \n        elif r < 100 and  100 < b and g < 100: # BLUE\n            count_df[\"juveniles\"][filename] += 1 \n            cv2.circle(image_7, (int(x),int(y)), 8, (180,60,30), 2)\n        elif r < 150 and b < 50 and g < 100:  # BROWN\n            count_df[\"adult_females\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (0,42,84), 2)            \n        else:\n            count_df[\"error\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (255,255,155), 2)\n    \n    # output the results\n          \n    f, ax = plt.subplots(3,2,figsize=(10,16))\n    (ax1, ax2, ax3, ax4, ax5, ax6) = ax.flatten()\n    plt.title('%s'%filename)\n    \n    ax1.imshow(cv2.cvtColor(image_2[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax1.set_title('Train')\n    ax2.imshow(cv2.cvtColor(image_1[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax2.set_title('Train Dotted')\n    ax3.imshow(cv2.cvtColor(image_3[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax3.set_title('Train Dotted - Train')\n    ax4.imshow(cv2.cvtColor(image_5[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax4.set_title('Mask blackened areas of Train Dotted')\n    ax5.imshow(image_6[700:1200,2130:2639], cmap='gray')\n    ax5.set_title('Grayscale for input to blob_log')\n    ax6.imshow(cv2.cvtColor(image_7[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax6.set_title('Result')\n\n    plt.show()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"83399c69-96ca-6146-aead-01fc6ccc921a"},"outputs":[],"source":"count_df"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"0c0dbd2e-c78e-bab9-e5be-78c9a7836482"},"outputs":[],"source":"reference = pd.read_csv('../input/Train/train.csv')\nreference.ix[0:1]"}],"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}