mirror of
https://github.com/inzva/object-detection-with-street-view.git
synced 2021-06-01 09:33:47 +03:00
293 lines
12 KiB
Plaintext
293 lines
12 KiB
Plaintext
{
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0,
|
|
"metadata": {
|
|
"colab": {
|
|
"name": "Object_Detection_inzva.ipynb",
|
|
"provenance": [],
|
|
"collapsed_sections": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"name": "python3"
|
|
},
|
|
"accelerator": "GPU"
|
|
},
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "8k0QK_e7whr2"
|
|
},
|
|
"source": [
|
|
"This notebook explains how to make predictions for Google Street View images using Yolo v3 and ImageAI library with the help of this [tutorial](https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Detection).\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 815
|
|
},
|
|
"id": "P-q6wNsIHqTk",
|
|
"outputId": "f5baf85f-8688-44f1-cc78-47782f0404f3"
|
|
},
|
|
"source": [
|
|
"!pip install opencv-python==4.1.2.30 imageai\n",
|
|
"!pip install requests\n",
|
|
"from google.colab.patches import cv2_imshow\n",
|
|
"import os\n",
|
|
"import cv2 as cv\n",
|
|
"from imageai.Detection import ObjectDetection\n",
|
|
"from collections import Counter\n",
|
|
"import pandas as pd\n",
|
|
"import numpy as np\n",
|
|
"import requests as req\n",
|
|
"import os as os"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Requirement already satisfied: opencv-python==4.1.2.30 in /usr/local/lib/python3.7/dist-packages (4.1.2.30)\n",
|
|
"Collecting imageai\n",
|
|
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/73/44/3d5d8ef572888025666eec284e85f9243faf06ca8c12085dcff1ca9754ed/imageai-2.1.6-py3-none-any.whl (160kB)\n",
|
|
"\u001b[K |████████████████████████████████| 163kB 18.2MB/s \n",
|
|
"\u001b[?25hRequirement already satisfied: numpy>=1.14.5 in /usr/local/lib/python3.7/dist-packages (from opencv-python==4.1.2.30) (1.19.5)\n",
|
|
"Requirement already satisfied: keras==2.4.3 in /usr/local/lib/python3.7/dist-packages (from imageai) (2.4.3)\n",
|
|
"Collecting pillow==7.0.0\n",
|
|
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/f5/79/b2d5695d1a931474fa68b68ec93bdf08ba9acbc4d6b3b628eb6aac81d11c/Pillow-7.0.0-cp37-cp37m-manylinux1_x86_64.whl (2.1MB)\n",
|
|
"\u001b[K |████████████████████████████████| 2.1MB 35.7MB/s \n",
|
|
"\u001b[?25hCollecting matplotlib==3.3.2\n",
|
|
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/87/a6/8d7d06f6b69236a3c1818157875ceb1259ba0d9df4194f4fe138ffdc0f41/matplotlib-3.3.2-cp37-cp37m-manylinux1_x86_64.whl (11.6MB)\n",
|
|
"\u001b[K |████████████████████████████████| 11.6MB 241kB/s \n",
|
|
"\u001b[?25hRequirement already satisfied: h5py==2.10.0 in /usr/local/lib/python3.7/dist-packages (from imageai) (2.10.0)\n",
|
|
"Collecting keras-resnet==0.2.0\n",
|
|
" Downloading https://files.pythonhosted.org/packages/76/d4/a35cbd07381139dda4db42c81b88c59254faac026109022727b45b31bcad/keras-resnet-0.2.0.tar.gz\n",
|
|
"Requirement already satisfied: scipy==1.4.1 in /usr/local/lib/python3.7/dist-packages (from imageai) (1.4.1)\n",
|
|
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from keras==2.4.3->imageai) (3.13)\n",
|
|
"Requirement already satisfied: certifi>=2020.06.20 in /usr/local/lib/python3.7/dist-packages (from matplotlib==3.3.2->imageai) (2020.12.5)\n",
|
|
"Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib==3.3.2->imageai) (2.8.1)\n",
|
|
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib==3.3.2->imageai) (1.3.1)\n",
|
|
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /usr/local/lib/python3.7/dist-packages (from matplotlib==3.3.2->imageai) (2.4.7)\n",
|
|
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib==3.3.2->imageai) (0.10.0)\n",
|
|
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from h5py==2.10.0->imageai) (1.15.0)\n",
|
|
"Building wheels for collected packages: keras-resnet\n",
|
|
" Building wheel for keras-resnet (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
|
|
" Created wheel for keras-resnet: filename=keras_resnet-0.2.0-py2.py3-none-any.whl size=20486 sha256=bfd0ac4844597cdd78f235432acbd86f52780d24e51cb2c09dc6a279636824ca\n",
|
|
" Stored in directory: /root/.cache/pip/wheels/5f/09/a5/497a30fd9ad9964e98a1254d1e164bcd1b8a5eda36197ecb3c\n",
|
|
"Successfully built keras-resnet\n",
|
|
"\u001b[31mERROR: bokeh 2.3.1 has requirement pillow>=7.1.0, but you'll have pillow 7.0.0 which is incompatible.\u001b[0m\n",
|
|
"\u001b[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.\u001b[0m\n",
|
|
"\u001b[31mERROR: imageai 2.1.6 has requirement numpy==1.19.3, but you'll have numpy 1.19.5 which is incompatible.\u001b[0m\n",
|
|
"Installing collected packages: pillow, matplotlib, keras-resnet, imageai\n",
|
|
" Found existing installation: Pillow 7.1.2\n",
|
|
" Uninstalling Pillow-7.1.2:\n",
|
|
" Successfully uninstalled Pillow-7.1.2\n",
|
|
" Found existing installation: matplotlib 3.2.2\n",
|
|
" Uninstalling matplotlib-3.2.2:\n",
|
|
" Successfully uninstalled matplotlib-3.2.2\n",
|
|
"Successfully installed imageai-2.1.6 keras-resnet-0.2.0 matplotlib-3.3.2 pillow-7.0.0\n"
|
|
],
|
|
"name": "stdout"
|
|
},
|
|
{
|
|
"output_type": "display_data",
|
|
"data": {
|
|
"application/vnd.colab-display-data+json": {
|
|
"pip_warning": {
|
|
"packages": [
|
|
"PIL",
|
|
"matplotlib",
|
|
"mpl_toolkits"
|
|
]
|
|
}
|
|
}
|
|
},
|
|
"metadata": {
|
|
"tags": []
|
|
}
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (2.23.0)\n",
|
|
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests) (2.10)\n",
|
|
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests) (3.0.4)\n",
|
|
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests) (1.24.3)\n",
|
|
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests) (2020.12.5)\n"
|
|
],
|
|
"name": "stdout"
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "09miic5WzgPR",
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"outputId": "769b8cd7-5248-426f-aef1-ca1dd942026c"
|
|
},
|
|
"source": [
|
|
"# At our work images were uploaded into Google Drive so we mouth here the drive.\n",
|
|
"from google.colab import drive\n",
|
|
"drive.mount('/content/drive')"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Mounted at /content/drive\n"
|
|
],
|
|
"name": "stdout"
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "Otae3QFWDA8E"
|
|
},
|
|
"source": [
|
|
"#Some utility functions.\n",
|
|
"\n",
|
|
"def showImage(img):\n",
|
|
" window_name = 'image'\n",
|
|
" cv2_imshow(img)\n",
|
|
" cv.waitKey(0)\n",
|
|
" cv.destroyAllWindows()"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "TwzsrXl70MPD"
|
|
},
|
|
"source": [
|
|
"#TO DO: This part will be filled with your input path for images.\n",
|
|
"input_path = ''"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "S0TbsoI69dla"
|
|
},
|
|
"source": [
|
|
"# Here we will list links to images we are going to detect objects.\n",
|
|
"test_images = os.listdir(input_path)"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "C4f9mR9v-ppq"
|
|
},
|
|
"source": [
|
|
"# Getting our YOLOv3 model.\n",
|
|
"modelYOLOv3 = 'https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5'\n",
|
|
"\n",
|
|
"if not os.path.exists('yolo.h5'):\n",
|
|
" r = req.get(modelYOLOv3, timeout=0.5)\n",
|
|
" with open('yolo.h5', 'wb') as outfile:\n",
|
|
" outfile.write(r.content)"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "76txCkmh_CSh"
|
|
},
|
|
"source": [
|
|
"detector = ObjectDetection()\n",
|
|
"detector.setModelTypeAsYOLOv3()\n",
|
|
"detector.setModelPath('yolo.h5')\n",
|
|
"detector.loadModel()"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "10A2wGwT_OEQ"
|
|
},
|
|
"source": [
|
|
"# Predicting for images. This may take sometime depending on number of images.\n",
|
|
"results = []\n",
|
|
"bounded_images = []\n",
|
|
"for i in test_images:\n",
|
|
" detectedImage, detections = detector.detectObjectsFromImage(output_type=\"array\", input_image=input_path + i, minimum_percentage_probability=30)\n",
|
|
" convertedImage = cv.cvtColor(detectedImage, cv.COLOR_RGB2BGR)\n",
|
|
" results.append(detections)\n",
|
|
" bounded_images.append(convertedImage)\n"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "GbqFaT_SEvx9"
|
|
},
|
|
"source": [
|
|
"# Counting images in each item and writing into dataframe.\n",
|
|
"all_items = []\n",
|
|
"final_counts=[]\n",
|
|
"new_item = {}\n",
|
|
"for i in range(len(results)):\n",
|
|
" result_dict = results[i]\n",
|
|
" for z in range(len(result_dict)):\n",
|
|
" image_objects= []\n",
|
|
" new_item[z] = result_dict[z]['name']\n",
|
|
" image_objects.append(new_item)\n",
|
|
" number_of_items = Counter(image_objects[0].values())\n",
|
|
" image_dict = {'image_name': test_images[i], 'no_of_items':number_of_items}\n",
|
|
" final_counts.append(image_dict)"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "nZoxbXcUEiDB"
|
|
},
|
|
"source": [
|
|
"# Finalizing into dataframe.\n",
|
|
"final_df=pd.DataFrame(columns=['index','image_name','no_of_items'])\n",
|
|
"for i in range(len(final_counts)):\n",
|
|
" df = pd.DataFrame(final_counts[i]).reset_index()\n",
|
|
" final_df =pd.concat([final_df,df],axis=0)\n"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"id": "8qTTMnGD58SD"
|
|
},
|
|
"source": [
|
|
"# Showing example predictions. You can change the images you would like to show by changing the number below.\n",
|
|
"showImage(bounded_images[1])\n"
|
|
],
|
|
"execution_count": null,
|
|
"outputs": []
|
|
}
|
|
]
|
|
}
|