{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this competition we are given **20579** images of **120** dogs breeds taken from  [**Stanford Dogs Dataset**](http://vision.stanford.edu/aditya86/ImageNetDogs/ \"Stanford Dogs Dataset\"). Since there are images with several puppies in them we're dealing with total of **22125** images of dogs. Data isn't perfectly balanced between breeds, so lets take a look at examples of all the breeds going from the most represented to the least ones.\n\nYes, you got it right, it's just a kernel to take a look at ALL THESE LOVELY PUPPIES 🐶.\n\nIf you love dogs as I do please SMASH UPVOTE BUTTON.)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"from glob import glob\nfrom IPython.core.display import display, HTML\nimport numpy as np\nimport xml.etree.ElementTree as ET\nimport cv2\nimport imageio\n\n\ndef get_bboxes(annotation_file):\n    '''Extract and return bounding boxes from annotation file.'''\n    bboxes = []\n    objects = ET.parse(annotation_file).getroot().findall('object')\n    for obj in objects:\n        bbox = obj.find('bndbox')\n        bboxes.append(tuple(int(bbox.find(_).text) for _ in ('xmin', 'ymin', 'xmax', 'ymax')))\n    return bboxes\n\n\ndef center_crop(image):\n    h, w = image.shape[:2]\n    s = min(h, w)\n    return image[(h-s)//2:(h-s)//2 + s,(w-s)//2:(w-s)//2 + s]\n\n\nfor breed in sorted(glob('../input/annotation/Annotation/*'), key=lambda breed: len(glob(breed + '/*')), reverse=True):\n    breed_id, breed_name = breed.split('/')[-1].split('-', 1)\n    breed_name = ' '.join(s.capitalize() for s in breed_name.replace('_', ' ').replace('-', ' ').split())\n    annotations = glob(breed + '/*')\n    np.random.shuffle(annotations)\n    dogs = []\n    for annotation_file in annotations[:11]:\n        bboxes = get_bboxes(annotation_file)\n        img_path = '../input/all-dogs/all-dogs/{}.jpg'.format(annotation_file.split('/')[-1])\n        try:\n            image = imageio.imread(img_path)\n            for xmin, ymin, xmax, ymax in bboxes:\n                dogs.append(cv2.resize(center_crop(image[ymin:ymax, xmin:xmax, :]), (128, 128)))\n        except Exception as e:\n            pass\n    np.random.shuffle(dogs)\n    dogs = np.hstack(dogs[:10])\n    imageio.imwrite(f'{breed_name}.png', dogs)\n    display(HTML('<div style=\"text-align:center\"><span title=\"{1}\"><a href=\"http://vision.stanford.edu/aditya86/ImageNetDogs/{0}.html\"><b>{1}</b></a>, {2} samples</span></div>'.format(breed_id, breed_name, len(annotations))))\n    display(HTML(f'<img src=\"{breed_name}.png\" alt=\"Examples of {breed_name}\" width=\"100%\">'))","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}