{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport xml.etree.ElementTree as ET\nimport glob\nimport matplotlib.pyplot as plt\nimport cv2\nimport numpy as np\nimport time\nfrom IPython.display import clear_output\nglob.glob(\"../input/all-dogs/*\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Taking all the path of annotation files**"},{"metadata":{"trusted":true},"cell_type":"code","source":"annoPaths = glob.glob('../input/annotation/Annotation/*/*')\nimgPaths = \"../input/all-dogs/all-dogs/\"\nimgSize = (64, 64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Total annotation files: \",len(annoPaths))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"** Looking at annotation file **"},{"metadata":{"trusted":true},"cell_type":"code","source":"!cat ../input/annotation/Annotation/n02105855-Shetland_sheepdog/n02105855_19944","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"** Function to crop image **\n\nparam : Image Path, Image Name, Coordinates\n\nreturn : Cropped Image or None if image not found"},{"metadata":{"trusted":true},"cell_type":"code","source":"def makeData(iPath, imgName, bbox):\n    imgPath = iPath+imgName+\".jpg\"\n    try:\n        img = cv2.cvtColor(cv2.imread(imgPath), cv2.COLOR_BGR2RGB)\n        cropped = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]\n        sized = cv2.resize(cropped, (64,64))\n    except:\n        print(imgName)\n        return None\n        \n    return sized","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"** Function To Fetch Bounding Box from XML File **\n\nparam : All annotation path, Image parent folder path\n\nreturn : Cropped Images, Labels to that images"},{"metadata":{"trusted":true},"cell_type":"code","source":"def getImgLab(aPath, iPath):\n    imgs = []\n    labels = []\n    for itr, xmlPath in enumerate(aPath):\n        tree = ET.parse(xmlPath)\n        root = tree.getroot()\n        objTags = root.findall('object')\n        imgName = xmlPath.split('/')[-1]\n        for obj in objTags:\n            bndbox = obj.find('bndbox')\n            xmin = int(bndbox.find('xmin').text)\n            ymin = int(bndbox.find('ymin').text)\n            xmax = int(bndbox.find('xmax').text)\n            ymax = int(bndbox.find('ymax').text)\n            label = obj.find('name').text\n            bbox = (xmin, ymin, xmax, ymax)\n            img = makeData(iPath, imgName, bbox)\n            if img is not None:\n                imgs.append(img) \n                labels.append(label)\n        if itr%500==0:\n            clear_output()\n            print((itr/len(aPath))*100)     \n    return imgs, labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time()\nimgs, labels = getImgLab(annoPaths, imgPaths)\nprint(\"--- %s minutes ---\" % ((time.time() - start_time)/60))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(3,3, figsize = (10,10))\nfor itr, i in enumerate(ax.ravel()):\n    i.imshow(imgs[itr])","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}