In [1]:
import numpy as np
import pandas as pd
import os
import cv2
from PIL import Image
from scipy.misc import imread
import matplotlib.pyplot as plt
import skimage.feature
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
import keras
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, Lambda, Cropping2D
from keras.utils import np_utils

from collections import Counter

%matplotlib inline


Using TensorFlow backend.

In [2]:
class_names = ['adult_females', 'adult_males', 'juveniles', 'pups', 'subadult_males']

my_dir = "/Users/kylefrankovich/Desktop/seal_the_deal/src/data/TrainSmall2/"

file_names = os.listdir(my_dir + "Train/")
file_names = sorted(file_names, key=lambda 
                    item: (int(item.partition('.')[0]) if item[0].isdigit() else float('inf'), item)) 

# select a subset of files to run on
file_names = file_names[0:6]
print(file_names)
# dataframe to store results in
coordinates_df = pd.DataFrame(index=file_names, columns=class_names)


['41.jpg', '42.jpg', '43.jpg', '44.jpg', '45.jpg', '46.jpg']

It looks like the code below is using the dotted images to get the class pixel coordinates per image:


In [3]:
for filename in file_names:
    
    # read the Train and Train Dotted images
    image_1 = cv2.imread(my_dir + "/TrainDotted/" + filename)
    image_2 = cv2.imread(my_dir + "/Train/" + filename)
    
    cut = np.copy(image_2)
    
    # absolute difference between Train and Train Dotted
    image_3 = cv2.absdiff(image_1,image_2)
    
# mask out blackened regions from Train Dotted
    mask_1 = cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY)
    mask_1[mask_1 < 20] = 0
    mask_1[mask_1 > 0] = 255
    
    mask_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)
    mask_2[mask_2 < 20] = 0
    mask_2[mask_2 > 0] = 255
    
    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_1)
    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_2) 
    
    # convert to grayscale to be accepted by skimage.feature.blob_log
    image_3 = cv2.cvtColor(image_3, cv2.COLOR_BGR2GRAY)
    
    # detect blobs
    blobs = skimage.feature.blob_log(image_3, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)
    
    adult_males = []
    subadult_males = []
    pups = []
    juveniles = []
    adult_females = [] 
    
    image_circles = image_1
    
    for blob in blobs:
        # get the coordinates for each blob
        y, x, s = blob
        # get the color of the pixel from Train Dotted in the center of the blob
        g,b,r = image_1[int(y)][int(x)][:]
        
        # decision tree to pick the class of the blob by looking at the color in Train Dotted
        if r > 200 and g < 50 and b < 50: # RED
            adult_males.append((int(x),int(y)))
            cv2.circle(image_circles, (int(x),int(y)), 20, (0,0,255), 10) 
        elif r > 200 and g > 200 and b < 50: # MAGENTA
            subadult_males.append((int(x),int(y))) 
            cv2.circle(image_circles, (int(x),int(y)), 20, (250,10,250), 10)
        elif r < 100 and g < 100 and 150 < b < 200: # GREEN
            pups.append((int(x),int(y)))
            cv2.circle(image_circles, (int(x),int(y)), 20, (20,180,35), 10)
        elif r < 100 and  100 < g and b < 100: # BLUE
            juveniles.append((int(x),int(y))) 
            cv2.circle(image_circles, (int(x),int(y)), 20, (180,60,30), 10)
        elif r < 150 and g < 50 and b < 100:  # BROWN
            adult_females.append((int(x),int(y)))
            cv2.circle(image_circles, (int(x),int(y)), 20, (0,42,84), 10)  
            
        cv2.rectangle(cut, (int(x)-112,int(y)-112),(int(x)+112,int(y)+112), 0,-1)
            
    coordinates_df["adult_males"][filename] = adult_males
    coordinates_df["subadult_males"][filename] = subadult_males
    coordinates_df["adult_females"][filename] = adult_females
    coordinates_df["juveniles"][filename] = juveniles
    coordinates_df["pups"][filename] = pups
    
print(coordinates_df)


                                            adult_females  \
41.jpg  [(3049, 2550), (2981, 2536), (3052, 2497), (29...   
42.jpg  [(3832, 1686), (4319, 1658), (4198, 1643), (43...   
43.jpg  [(3319, 3622), (3203, 3606), (3097, 3504), (30...   
44.jpg  [(3301, 2214), (3256, 2171), (3427, 2166), (34...   
45.jpg  [(2267, 2665), (2151, 2132), (2046, 2037), (20...   
46.jpg                                                 []   

                                              adult_males  \
41.jpg  [(2749, 2516), (3110, 2349), (2293, 2315), (25...   
42.jpg  [(4940, 2506), (4973, 1930), (4816, 1883), (44...   
43.jpg  [(3365, 3405), (3295, 3267), (2636, 3115), (28...   
44.jpg         [(3360, 2145), (3599, 1867), (3068, 1494)]   
45.jpg  [(2633, 3199), (2108, 2048), (2546, 1423), (25...   
46.jpg                                     [(5418, 1338)]   

                                                juveniles  \
41.jpg  [(2922, 2514), (3081, 2421), (3015, 2240), (29...   
42.jpg                                      [(4175, 546)]   
43.jpg  [(3168, 3552), (3152, 3542), (3408, 3351), (25...   
44.jpg  [(3273, 2242), (3272, 2221), (3326, 2206), (31...   
45.jpg  [(2199, 1917), (1756, 1874), (1725, 1868), (18...   
46.jpg                                                 []   

                                                     pups  \
41.jpg  [(2977, 2576), (2960, 2561), (2833, 2530), (29...   
42.jpg                                                 []   
43.jpg  [(3268, 3585), (3279, 3572), (3029, 3513), (29...   
44.jpg                                                 []   
45.jpg                                                 []   
46.jpg                                                 []   

                                           subadult_males  
41.jpg                                                 []  
42.jpg  [(3617, 1716), (4010, 1641), (4337, 1605), (36...  
43.jpg  [(2999, 3583), (3801, 3125), (1817, 2212), (27...  
44.jpg                       [(3375, 1740), (2950, 1240)]  
45.jpg  [(1711, 1699), (1549, 1650), (1689, 1424), (26...  
46.jpg  [(5372, 2086), (5600, 1485), (5569, 1432), (54...  

In [4]:
f, ax = plt.subplots(1,1,figsize=(10,16))
ax.imshow(cv2.cvtColor(image_circles, cv2.COLOR_BGR2RGB))
plt.show()



In [9]:
f, ax = plt.subplots(1,1,figsize=(10,16))
ax.imshow(cv2.cvtColor(cut, cv2.COLOR_BGR2RGB))
plt.show()