{"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)\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 all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport numpy as np\nimport time\nimport tarfile\n\n\nnet = cv2.dnn.readNet(\"/kaggle/input/yolo-coco-data/yolov3.weights\",\"/kaggle/input/yolo-coco-data/yolov3.cfg\")\nclasses = []\nwith open(\"/kaggle/input/yolo-coco-data/coco.names\",\"r\") as f:\n    classes = [line.strip() for line in f.readlines()]\n    \nlayer_names = net.getLayerNames()\noutputlayers = [layer_names[i[0]-1] for i in net.getUnconnectedOutLayers()]\ncolors = np.random.uniform(0, 255, size=(len(classes), 3))\n\n\ntar = tarfile.open(\"/kaggle/input/imagenet-object-localization-challenge/imagenet_object_localization_patched2019.tar.gz\", \"r:gz\")\ntar.extractall()\ntar.close()\n\nfor tarinfo in tar:\n    img = cv2.imread(tarinfo.name, 1)\n    img = cv2.resize(img, None,fx=0.4,fy=0.4)\n\n    #cap = cv2.VideoCapture(0)\n\n    font = cv2.FONT_HERSHEY_PLAIN\n    starting_time = time.time()\n\n    height, width, channels = img.shape\n        \n    #detecting object\n    blob = cv2.dnn.blobFromImage(img, 0.00392, (416,416),(0,0,0),True,crop=False)\n        \n               \n    net.setInput(blob)\n    outs = net.forward(outputlayers)\n        \n    # showing information on the screen\n    class_ids = []\n    confidences = []\n    boxes = []\n    for out in outs:\n        for detection in out:\n            scores = detection[5:]\n            class_id = np.argmax(scores)\n            confidence = scores[class_id]\n            if confidence > 0.2:\n                center_x = int(detection[0] * width)\n                center_y = int(detection[1] * height)\n                w = int(detection[2] * width)\n                h = int(detection[3] * height)\n                    \n                x = int(center_x - w / 2)\n                y = int(center_y - h / 2)\n                    \n                boxes.append([x,y,w,h])\n                confidences.append(float(confidence))\n                class_ids.append(class_id)\n                    \n    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)         \n    number_objects_detected = len(boxes)\n    for i in range(len(boxes)):\n        if i in indexes:\n            x,y,w,h = boxes[i]\n            label = str(classes[class_ids[i]])\n            print(label)\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}