{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# inspired by https://github.com/OlafenwaMoses/ImageAI/ , https://www.kaggle.com/shivamb\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"#importing required libraries\nimport matplotlib.pyplot as plt\nimport pandas as pd \nimport numpy as np \nimport math, os\nfrom keras.applications.inception_v3 import preprocess_input\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils.data_utils import GeneratorEnqueuer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0104c4578f7846885bbdfa9511568fab483d9b7"},"cell_type":"code","source":"image_path =\"../input/google-ai-open-images-object-detection-track/test/\"\nbatch_size = 100\nimg_generator = ImageDataGenerator().flow_from_directory(image_path, shuffle=False, batch_size = batch_size)                                                                                      \n#calculating size of epoch                                                                                         \nn_rounds = math.ceil(img_generator.samples / img_generator.batch_size)  # size of an epoch\n\nfilenames = img_generator.filenames\nimg_generator = GeneratorEnqueuer(img_generator)\nimg_generator.start()\nimg_generator = img_generator.get()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1738086f9b59984d163cd4d90c51d71217a34445"},"cell_type":"code","source":"##Using image Object Detection, using yolo-tiny which is optimized for speed \nfrom imageai.Detection import ObjectDetection\nimport os\nweights_path = \"../input/yolotiny/yolo-tiny.h5\"\n\ndetector = ObjectDetection()\ndetector.setModelTypeAsTinyYOLOv3()\ndetector.setModelPath( weights_path)\ndetector.loadModel()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe93684e5fc3f36c48a1f9dbafc2e42bb017a012"},"cell_type":"code","source":"for i in range(n_rounds):\n    batch = next(img_generator)\n    for j, prediction in enumerate(batch):\n        image = filenames[i * batch_size + j]\n        detections = detector.detectObjectsFromImage(input_image=image_path+image, output_image_path=\"image_with_box.png\", minimum_percentage_probability = 80)        \n        for eachObject in detections:\n            print(eachObject[\"name\"] , \" : \" , eachObject[\"percentage_probability\"], \" : \", eachObject[\"box_points\"] )\n            plt.figure(figsize=(12,12))\n            plt.imshow(plt.imread(\"image_with_box.png\"))\n            plt.show()\n    if i==10:\n        break     ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"858039c2dfe36815cf3398dfd372dffc9fda1db0"},"cell_type":"code","source":"##ADDED VIDEO DETECTION \nfrom imageai.Detection import VideoObjectDetection\n\ndetector_video = VideoObjectDetection()\ndetector_video.setModelTypeAsTinyYOLOv3()\ndetector_video.setModelPath('../input/yolotiny/yolo-tiny.h5')\ndetector_video.loadModel()\n\nprint(detector_video)\nvideo_path = detector_video.detectObjectsFromVideo(input_file_path='../input/traffic/traffic.mp4',\n                                            output_file_path='../input/traffic_detected',\n                                            frames_per_second=20, log_progress=True)\nprint(video_path)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}