├── 1.0 Object Detection Tutorial.ipynb ├── 1.1 Customized Object Detection - Images.ipynb ├── 1.2 Customized Object Detection - Video.ipynb ├── README.md ├── protoc command.txt └── protoc.exe /1.1 Customized Object Detection - Images.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import os\n", 10 | "import pathlib\n", 11 | "\n", 12 | "\n", 13 | "if \"models\" in pathlib.Path.cwd().parts:\n", 14 | " while \"models\" in pathlib.Path.cwd().parts:\n", 15 | " os.chdir('..')\n", 16 | "elif not pathlib.Path('models').exists():\n", 17 | " !git clone --depth 1 https://github.com/tensorflow/models" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "import numpy as np\n", 27 | "import os\n", 28 | "import six.moves.urllib as urllib\n", 29 | "import sys\n", 30 | "import tarfile\n", 31 | "import tensorflow as tf\n", 32 | "import zipfile\n", 33 | "import pathlib\n", 34 | "\n", 35 | "from collections import defaultdict\n", 36 | "from io import StringIO\n", 37 | "from matplotlib import pyplot as plt\n", 38 | "from PIL import Image\n", 39 | "from IPython.display import display\n", 40 | "\n", 41 | "from object_detection.utils import ops as utils_ops\n", 42 | "from object_detection.utils import label_map_util\n", 43 | "from object_detection.utils import visualization_utils as vis_util" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# patch tf1 into `utils.ops`\n", 53 | "utils_ops.tf = tf.compat.v1\n", 54 | "\n", 55 | "# Patch the location of gfile\n", 56 | "tf.gfile = tf.io.gfile" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "def load_model(model_name):\n", 66 | " base_url = 'http://download.tensorflow.org/models/object_detection/'\n", 67 | " model_file = model_name + '.tar.gz'\n", 68 | " model_dir = tf.keras.utils.get_file(\n", 69 | " fname=model_name, \n", 70 | " origin=base_url + model_file,\n", 71 | " untar=True)\n", 72 | "\n", 73 | " model_dir = pathlib.Path(model_dir)/\"saved_model\"\n", 74 | "\n", 75 | " model = tf.saved_model.load(str(model_dir))\n", 76 | " model = model.signatures['serving_default']\n", 77 | "\n", 78 | " return model" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "# List of the strings that is used to add correct label for each box.\n", 88 | "PATH_TO_LABELS = 'object_detection/data/mscoco_label_map.pbtxt'\n", 89 | "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "INFO:tensorflow:Saver not created because there are no variables in the graph to restore\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "model_name = 'ssd_mobilenet_v1_coco_2017_11_17'\n", 107 | "detection_model = load_model(model_name)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 7, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "def run_inference_for_single_image(model, image):\n", 117 | " image = np.asarray(image)\n", 118 | " # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.\n", 119 | " input_tensor = tf.convert_to_tensor(image)\n", 120 | " # The model expects a batch of images, so add an axis with `tf.newaxis`.\n", 121 | " input_tensor = input_tensor[tf.newaxis,...]\n", 122 | "\n", 123 | " # Run inference\n", 124 | " output_dict = model(input_tensor)\n", 125 | "\n", 126 | " # All outputs are batches tensors.\n", 127 | " # Convert to numpy arrays, and take index [0] to remove the batch dimension.\n", 128 | " # We're only interested in the first num_detections.\n", 129 | " num_detections = int(output_dict.pop('num_detections'))\n", 130 | " output_dict = {key:value[0, :num_detections].numpy() \n", 131 | " for key,value in output_dict.items()}\n", 132 | " output_dict['num_detections'] = num_detections\n", 133 | "\n", 134 | " # detection_classes should be ints.\n", 135 | " output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)\n", 136 | " \n", 137 | " # Handle models with masks:\n", 138 | " if 'detection_masks' in output_dict:\n", 139 | " # Reframe the the bbox mask to the image size.\n", 140 | " detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(\n", 141 | " output_dict['detection_masks'], output_dict['detection_boxes'],\n", 142 | " image.shape[0], image.shape[1]) \n", 143 | " detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,\n", 144 | " tf.uint8)\n", 145 | " output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()\n", 146 | " \n", 147 | " return output_dict" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 39, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "def show_inference(model, image_path):\n", 157 | " # the array based representation of the image will be used later in order to prepare the\n", 158 | " # result image with boxes and labels on it.\n", 159 | " image_np = image_path\n", 160 | " image_np=cv2.cvtColor(image_np,cv2.COLOR_BGR2RGB)\n", 161 | " # Actual detection.\n", 162 | " output_dict = run_inference_for_single_image(model, image_np)\n", 163 | " # Visualization of the results of a detection.\n", 164 | " vis_util.visualize_boxes_and_labels_on_image_array(\n", 165 | " image_np,\n", 166 | " output_dict['detection_boxes'],\n", 167 | " output_dict['detection_classes'],\n", 168 | " output_dict['detection_scores'],\n", 169 | " category_index,\n", 170 | " instance_masks=output_dict.get('detection_masks_reframed', None),\n", 171 | " use_normalized_coordinates=True,\n", 172 | " line_thickness=2)\n", 173 | " \n", 174 | " image_np=cv2.cvtColor(image_np,cv2.COLOR_BGR2RGB)\n", 175 | " return image_np" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 40, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "import cv2\n", 185 | "\n", 186 | "img=cv2.imread(r'C:\\Users\\User\\Desktop\\edXcope\\pedestrians-transportation.jpg')\n", 187 | "img=show_inference(detection_model,img)\n", 188 | "cv2.imshow('IMG',img)\n", 189 | "cv2.waitKey(0)\n", 190 | "cv2.destroyAllWindows()" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 3", 204 | "language": "python", 205 | "name": "python3" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 3 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython3", 217 | "version": "3.7.4" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 2 222 | } 223 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-object-detection-api-configuration 2 | This tutorial discusses how to configure the Tensorflow Object Detection API in windows and how implement custom object detection. 3 | 4 | ## Credits & Links 5 | 6 | 1. [Download Tensorflow Object Detection API](https://github.com/tensorflow/models) 7 | 2. [How to install Tensorflow Object Detection](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) 8 | 9 | ## Installing the Tensorflow Object Detection API 10 | 11 | 1. Download the tensorflow object detection api from [Github](https://github.com/tensorflow/models) 12 | 2. Open the Anaconda Prompt and install the dependencies for windows, 13 | 14 | ``` 15 | pip install tensorflow==2.4.1 16 | pip install Cython 17 | pip install contextlib2 18 | pip install pillow 19 | pip install lxml 20 | pip install jupyter 21 | pip install matplotlib 22 | pip install tf_slim 23 | pip install opencv-python 24 | ``` 25 | 26 | 3. Download the files from this repository 27 | 4. Copy and paste ```protoc.exe``` file in the path ```models-master\research``` 28 | 5. Open the Commmand Prompt in ```models-master\research``` and copy and run the command included in ```protoc command.txt``` 29 | 6. Copy the files ```object_detection_tutorial.ipynb```, ```1.0 Customized Object Detection.ipynb``` & ```1.1 Customized Object Detection-Video.ipynb``` into ```models-master\research``` 30 | 7. Run above codes and check 31 | 32 | ## Models used in Tensorflow Object Detection API 33 | 34 | ![Models used in Tensorflow Object Detection API](https://cdn-images-1.medium.com/max/800/1*-EyxSs2OiyWm-E6MSpSJiA.png) 35 | -------------------------------------------------------------------------------- /protoc command.txt: -------------------------------------------------------------------------------- 1 | for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=. -------------------------------------------------------------------------------- /protoc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aieml/tensorflow-object-detection-api-configuration/c124f7b22079997e0e4d987a5a12a961b54f7588/protoc.exe --------------------------------------------------------------------------------