├── Install-TensorFlow-Python3.7.ps1
├── README.md
├── generate_labelmap.py
├── generate_tfrecord.py
├── object_detection_tutorial.ipynb
└── xml_to_csv.py
/Install-TensorFlow-Python3.7.ps1:
--------------------------------------------------------------------------------
1 | # This is the link to download Python 3.7.0 from Python.org
2 | # See https://www.python.org/downloads/
3 | $pythonUrl = "https://www.python.org/ftp/python/3.7.0/python-3.7.0-amd64.exe"
4 |
5 | # This is the directory that the exe is downloaded to
6 | $tempDirectory = "C:\temp_provision\"
7 |
8 | # Installation Directory
9 | # Some packages look for Python here
10 | $targetDir = "C:\Python37"
11 |
12 | # create the download directory and get the exe file
13 | $pythonNameLoc = $tempDirectory + "python370.exe"
14 | New-Item -ItemType directory -Path $tempDirectory -Force | Out-Null
15 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
16 | (New-Object System.Net.WebClient).DownloadFile($pythonUrl, $pythonNameLoc)
17 |
18 |
19 | #Creating object detection directory
20 |
21 | $working_dir = "C:\Object_detection\"
22 |
23 | New-Item -Path $working_dir -ItemType Directory
24 |
25 |
26 | # These are the silent arguments for the install of python
27 | # See https://docs.python.org/3/using/windows.html
28 | $Arguments = @()
29 | $Arguments += "/i"
30 | $Arguments += 'InstallAllUsers="1"'
31 | $Arguments += 'TargetDir="' + $targetDir + '"'
32 | $Arguments += 'DefaultAllUsersTargetDir="' + $targetDir + '"'
33 | $Arguments += 'AssociateFiles="1"'
34 | $Arguments += 'PrependPath="1"'
35 | $Arguments += 'Include_doc="1"'
36 | $Arguments += 'Include_debug="1"'
37 | $Arguments += 'Include_dev="1"'
38 | $Arguments += 'Include_exe="1"'
39 | $Arguments += 'Include_launcher="1"'
40 | $Arguments += 'InstallLauncherAllUsers="1"'
41 | $Arguments += 'Include_lib="1"'
42 | $Arguments += 'Include_pip="1"'
43 | $Arguments += 'Include_symbols="1"'
44 | $Arguments += 'Include_tcltk="1"'
45 | $Arguments += 'Include_test="1"'
46 | $Arguments += 'Include_tools="1"'
47 | $Arguments += 'Include_launcher="1"'
48 | $Arguments += 'Include_launcher="1"'
49 | $Arguments += 'Include_launcher="1"'
50 | $Arguments += 'Include_launcher="1"'
51 | $Arguments += 'Include_launcher="1"'
52 | $Arguments += 'Include_launcher="1"'
53 | $Arguments += "/passive"
54 |
55 | #Install Python
56 | Start-Process $pythonNameLoc -ArgumentList $Arguments -Wait
57 |
58 | Function Get-EnvVariableNameList {
59 | [cmdletbinding()]
60 | $allEnvVars = Get-ChildItem Env:
61 | $allEnvNamesArray = $allEnvVars.Name
62 | $pathEnvNamesList = New-Object System.Collections.ArrayList
63 | $pathEnvNamesList.AddRange($allEnvNamesArray)
64 | return ,$pathEnvNamesList
65 | }
66 |
67 | Function Add-EnvVarIfNotPresent {
68 | Param (
69 | [string]$variableNameToAdd,
70 | [string]$variableValueToAdd
71 | )
72 | $nameList = Get-EnvVariableNameList
73 | $alreadyPresentCount = ($nameList | Where{$_ -like $variableNameToAdd}).Count
74 | if ($alreadyPresentCount -eq 0)
75 | {
76 | [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Machine)
77 | [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Process)
78 | [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::User)
79 | $message = "Enviromental variable added to machine, process and user to include $variableNameToAdd"
80 | }
81 | else
82 | {
83 | $message = 'Environmental variable already exists. Consider using a different function to modify it'
84 | }
85 | Write-Information $message
86 | }
87 |
88 | Function Get-EnvExtensionList {
89 | [cmdletbinding()]
90 | $pathExtArray = ($env:PATHEXT).Split("{;}")
91 | $pathExtList = New-Object System.Collections.ArrayList
92 | $pathExtList.AddRange($pathExtArray)
93 | return ,$pathExtList
94 | }
95 |
96 | Function Add-EnvExtension {
97 | Param (
98 | [string]$pathExtToAdd
99 | )
100 | $pathList = Get-EnvExtensionList
101 | $alreadyPresentCount = ($pathList | Where{$_ -like $pathToAdd}).Count
102 | if ($alreadyPresentCount -eq 0)
103 | {
104 | $pathList.Add($pathExtToAdd)
105 | $returnPath = $pathList -join ";"
106 | [System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::Machine)
107 | [System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::Process)
108 | [System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::User)
109 | $message = "Path extension added to machine, process and user paths to include $pathExtToAdd"
110 | }
111 | else
112 | {
113 | $message = 'Path extension already exists'
114 | }
115 | Write-Information $message
116 | }
117 |
118 | Function Get-EnvPathList {
119 | [cmdletbinding()]
120 | $pathArray = ($env:PATH).Split("{;}")
121 | $pathList = New-Object System.Collections.ArrayList
122 | $pathList.AddRange($pathArray)
123 | return ,$pathList
124 | }
125 |
126 | Function Add-EnvPath {
127 | Param (
128 | [string]$pathToAdd
129 | )
130 | $pathList = Get-EnvPathList
131 | $alreadyPresentCount = ($pathList | Where{$_ -like $pathToAdd}).Count
132 | if ($alreadyPresentCount -eq 0)
133 | {
134 | $pathList.Add($pathToAdd)
135 | $returnPath = $pathList -join ";"
136 | [System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::Machine)
137 | [System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::Process)
138 | [System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::User)
139 | $message = "Path added to machine, process and user paths to include $pathToAdd"
140 | }
141 | else
142 | {
143 | $message = 'Path already exists'
144 | }
145 | Write-Information $message
146 | }
147 |
148 | Add-EnvExtension '.PY'
149 | Add-EnvExtension '.PYW'
150 | Add-EnvPath 'C:\Python37\'
151 | Add-EnvPath 'C:\Python37\'
152 | Add-EnvPath 'C:\Python37\Scripts\'
153 |
154 |
155 |
156 |
157 | ### Installing TesnsorFlow dependency
158 | ### change here for 32 bit System
159 | $vc_redistURL = "https://aka.ms/vs/16/release/vc_redist.x64.exe"
160 | $vc_redistLoc = $tempDirectory + "vc_redist.x64.exe"
161 |
162 |
163 | New-Item -ItemType directory -Path $tempDirectory -Force | Out-Null
164 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
165 | (New-Object System.Net.WebClient).DownloadFile($vc_redistURL, $vc_redistLoc)
166 |
167 | #Install redist
168 | Start-Process $vc_redistLoc -Wait
169 |
170 | $VS_toolsURL = "https://download.microsoft.com/download/5/f/7/5f7acaeb-8363-451f-9425-68a90f98b238/visualcppbuildtools_full.exe?fixForIE=.exe"
171 | $VS_toolstLoc = $tempDirectory + "VS_tools.exe"
172 | New-Item -ItemType directory -Path $tempDirectory -Force | Out-Null
173 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
174 | (New-Object System.Net.WebClient).DownloadFile($VS_toolsURL, $VS_toolstLoc)
175 |
176 | #Install VStool
177 | Start-Process $VS_toolstLoc -Wait
178 |
179 | # Install a library using Pip
180 | python -m pip install --upgrade pip
181 | python -m pip install -U --pre tensorflow=="2.*"
182 | python -m pip install tf_slim
183 | python -m pip install pywin32==225
184 | python -m pip install pycocotools
185 | python -m pip install pandas
186 | python -m pip install tf-models-official
187 | python -m pip install opencv-python==3.4.11.43
188 | python -m pip install lvis
189 |
190 |
191 |
192 | ### Downloading protobuff for model files extraction
193 | $protobuffURL = "https://github.com/protocolbuffers/protobuf/releases/download/v3.13.0/protoc-3.13.0-win64.zip"
194 | $protobuffLoc = $tempDirectory + "protobuff.zip"
195 |
196 | New-Item -ItemType directory -Path $tempDirectory -Force | Out-Null
197 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
198 | (New-Object System.Net.WebClient).DownloadFile($protobuffURL, $protobuffLoc)
199 |
200 |
201 |
202 |
203 | Get-ChildItem -Path "C:\Object_detection" -Include *.* -File -Recurse | foreach { $_.Delete()}
204 |
205 | Add-Type -AssemblyName System.IO.Compression.FileSystem
206 | function Unzip
207 | {
208 | param([string]$zipfile, [string]$outpath)
209 |
210 | [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
211 | }
212 | Unzip $protobuffLoc $working_dir
213 |
214 | Get-ChildItem -Path $tempDirectory -Include *.* -File -Recurse | foreach { $_.Delete()}
215 |
216 |
217 |
218 | ### Cloning Github Ripo and extracting it
219 |
220 | $tf_git_URL = "https://github.com/tensorflow/models/archive/master.zip"
221 | $tf_git_Loc = $tempDirectory + "master.zip"
222 |
223 | New-Item -ItemType directory -Path $tempDirectory -Force | Out-Null
224 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
225 | (New-Object System.Net.WebClient).DownloadFile($tf_git_URL, $tf_git_Loc)
226 |
227 | Unzip $tf_git_Loc $working_dir
228 |
229 | Start-Process "C:/Object_detection/bin/protoc.exe" "C:\\Object_detection\\models-master\\research\\object_detection\\protos\\*.proto --python_out=."
230 |
231 |
232 |
233 | ## Getting into directory for processing protec file
234 | cd "C:/Object_detection/models-master/research"
235 |
236 | Start-Process -FilePath "C:/Object_detection/bin/protoc.exe" -ArgumentList "object_detection/protos/*.proto --python_out=."
237 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Widows-Object-Detection-Setup
2 |
3 | Hi Everyone, here is a Power Shell script for you if you are quite new with TensorFlow object Detection API.
4 |
5 | I have seen developers having issues while setting up their system for object detection some time environment issues sometimes version issues its quite irritating. For ending this all for good this script is made to install all requited packages as well as dependencies within your system so that you can start having fun with object detection without worrying about the installation in windows.
6 |
7 | ## How to use.
8 |
9 | Just clone this repository and run the .ps script in PowerShell and you are done.
10 |
11 |
--------------------------------------------------------------------------------
/generate_labelmap.py:
--------------------------------------------------------------------------------
1 |
2 | classnames_file = "images/class-names.txt"
3 | protobuf_file = "images/labelmap.pbtxt"
4 |
5 | file = open(classnames_file,'r')
6 | output_dict ={}
7 | classname = file.readline().strip()
8 | count=1
9 | while len(classname) >0 :
10 | output_dict[classname] = count
11 | classname = file.readline().strip()
12 | count+=1
13 | file.close()
14 |
15 | outfile = open(protobuf_file,'w+')
16 | outfile.truncate(0)
17 | for i in output_dict.keys():
18 | outfile.write("item {\n"+" id:"+ str(output_dict[i]) + '\n'+' name:'+"'" +str(i)+"'" +'\n'+ "}\n")
19 | outfile.close()
20 |
--------------------------------------------------------------------------------
/generate_tfrecord.py:
--------------------------------------------------------------------------------
1 | """
2 | Usage:
3 | # From tensorflow/models/
4 | # Create train data:
5 | python generate_tfrecord.py --csv_input=images/train_labels.csv --image_dir=images/train --output_path=train.record
6 | # Create test data:
7 | python generate_tfrecord.py --csv_input=images/test_labels.csv --image_dir=images/test --output_path=test.record
8 | """
9 | from __future__ import division
10 | from __future__ import print_function
11 | from __future__ import absolute_import
12 |
13 | import os
14 | import io
15 | import pandas as pd
16 |
17 | from tensorflow.python.framework.versions import VERSION
18 | import tensorflow.compat.v1 as tf
19 |
20 | from PIL import Image
21 | from utils import dataset_util
22 | from collections import namedtuple, OrderedDict
23 |
24 | flags = tf.app.flags
25 | flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
26 | flags.DEFINE_string('image_dir', '', 'Path to the image directory')
27 | flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
28 | FLAGS = flags.FLAGS
29 |
30 |
31 |
32 | file = open(r"images/class-names.txt",'r')
33 | output_dict ={}
34 | classname = file.readline().strip()
35 | count=1
36 | while len(classname) >0 :
37 | output_dict[classname] = count
38 | classname = file.readline().strip()
39 | count+=1
40 | file.close()
41 |
42 |
43 | # TO-DO replace this with label map
44 | def class_text_to_int(row_label):
45 | return output_dict[row_label]
46 |
47 | def split(df, group):
48 | data = namedtuple('data', ['filename', 'object'])
49 | gb = df.groupby(group)
50 | return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
51 |
52 |
53 | def create_tf_example(group, path):
54 | with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
55 | encoded_jpg = fid.read()
56 | encoded_jpg_io = io.BytesIO(encoded_jpg)
57 | image = Image.open(encoded_jpg_io)
58 | width, height = image.size
59 |
60 | filename = group.filename.encode('utf8')
61 | image_format = b'jpg'
62 | xmins = []
63 | xmaxs = []
64 | ymins = []
65 | ymaxs = []
66 | classes_text = []
67 | classes = []
68 |
69 | for index, row in group.object.iterrows():
70 | xmins.append(row['xmin'] / width)
71 | xmaxs.append(row['xmax'] / width)
72 | ymins.append(row['ymin'] / height)
73 | ymaxs.append(row['ymax'] / height)
74 | classes_text.append(row['class'].encode('utf8'))
75 | classes.append(class_text_to_int(row['class']))
76 |
77 | tf_example = tf.train.Example(features=tf.train.Features(feature={
78 | 'image/height': dataset_util.int64_feature(height),
79 | 'image/width': dataset_util.int64_feature(width),
80 | 'image/filename': dataset_util.bytes_feature(filename),
81 | 'image/source_id': dataset_util.bytes_feature(filename),
82 | 'image/encoded': dataset_util.bytes_feature(encoded_jpg),
83 | 'image/format': dataset_util.bytes_feature(image_format),
84 | 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
85 | 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
86 | 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
87 | 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
88 | 'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
89 | 'image/object/class/label': dataset_util.int64_list_feature(classes),
90 | }))
91 | return tf_example
92 |
93 |
94 | def main(_):
95 | writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
96 | path = os.path.join(os.getcwd(), FLAGS.image_dir)
97 | examples = pd.read_csv(FLAGS.csv_input)
98 | grouped = split(examples, 'filename')
99 | for group in grouped:
100 | tf_example = create_tf_example(group, path)
101 | writer.write(tf_example.SerializeToString())
102 |
103 | writer.close()
104 | output_path = os.path.join(os.getcwd(), FLAGS.output_path)
105 | print('Successfully created the TFRecords: {}'.format(output_path))
106 |
107 |
108 | if __name__ == '__main__':
109 | tf.app.run()
110 |
--------------------------------------------------------------------------------
/object_detection_tutorial.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "colab_type": "text",
7 | "id": "V8-yl-s-WKMG"
8 | },
9 | "source": [
10 | "# Object Detection API Demo\n",
11 | "\n",
12 | "
"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {
25 | "colab_type": "text",
26 | "id": "3cIrseUv6WKz"
27 | },
28 | "source": [
29 | "Welcome to the [Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection). This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image."
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {
35 | "colab_type": "text",
36 | "id": "VrJaG0cYN9yh"
37 | },
38 | "source": [
39 | "> **Important**: This tutorial is to help you through the first step towards using [Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection) to build models. If you just just need an off the shelf model that does the job, see the [TFHub object detection example](https://colab.sandbox.google.com/github/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb)."
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {
45 | "colab_type": "text",
46 | "id": "kFSqkTCdWKMI"
47 | },
48 | "source": [
49 | "# Setup"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {
55 | "colab_type": "text",
56 | "id": "awjrpqy-6MaQ"
57 | },
58 | "source": [
59 | "Important: If you're running on a local machine, be sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md). This notebook includes only what's necessary to run in Colab."
60 | ]
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {
65 | "colab_type": "text",
66 | "id": "p3UGXxUii5Ym"
67 | },
68 | "source": [
69 | "### Install"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {
76 | "colab": {},
77 | "colab_type": "code",
78 | "id": "hGL97-GXjSUw"
79 | },
80 | "outputs": [],
81 | "source": [
82 | "!pip install -U --pre tensorflow==\"2.*\"\n",
83 | "!pip install tf_slim"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {
89 | "colab_type": "text",
90 | "id": "n_ap_s9ajTHH"
91 | },
92 | "source": [
93 | "Make sure you have `pycocotools` installed"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": null,
99 | "metadata": {
100 | "colab": {},
101 | "colab_type": "code",
102 | "id": "Bg8ZyA47i3pY"
103 | },
104 | "outputs": [],
105 | "source": [
106 | "!pip install pycocotools"
107 | ]
108 | },
109 | {
110 | "cell_type": "markdown",
111 | "metadata": {
112 | "colab_type": "text",
113 | "id": "-vsOL3QR6kqs"
114 | },
115 | "source": [
116 | "Get `tensorflow/models` or `cd` to parent directory of the repository."
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {
123 | "colab": {},
124 | "colab_type": "code",
125 | "id": "ykA0c-om51s1"
126 | },
127 | "outputs": [],
128 | "source": [
129 | "import os\n",
130 | "import pathlib\n",
131 | "\n",
132 | "\n",
133 | "if \"models\" in pathlib.Path.cwd().parts:\n",
134 | " while \"models\" in pathlib.Path.cwd().parts:\n",
135 | " os.chdir('..')\n",
136 | "elif not pathlib.Path('models').exists():\n",
137 | " !git clone --depth 1 https://github.com/tensorflow/models"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {
143 | "colab_type": "text",
144 | "id": "O219m6yWAj9l"
145 | },
146 | "source": [
147 | "Compile protobufs and install the object_detection package"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {
154 | "colab": {},
155 | "colab_type": "code",
156 | "id": "PY41vdYYNlXc"
157 | },
158 | "outputs": [],
159 | "source": [
160 | "%%bash\n",
161 | "cd models/research/\n",
162 | "protoc object_detection/protos/*.proto --python_out=."
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": null,
168 | "metadata": {
169 | "colab": {},
170 | "colab_type": "code",
171 | "id": "s62yJyQUcYbp"
172 | },
173 | "outputs": [],
174 | "source": [
175 | "%%bash \n",
176 | "cd models/research\n",
177 | "pip install ."
178 | ]
179 | },
180 | {
181 | "cell_type": "markdown",
182 | "metadata": {
183 | "colab_type": "text",
184 | "id": "LBdjK2G5ywuc"
185 | },
186 | "source": [
187 | "### Imports"
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": 5,
193 | "metadata": {
194 | "colab": {},
195 | "colab_type": "code",
196 | "id": "hV4P5gyTWKMI"
197 | },
198 | "outputs": [],
199 | "source": [
200 | "import numpy as np\n",
201 | "import os\n",
202 | "import six.moves.urllib as urllib\n",
203 | "import sys\n",
204 | "import tarfile\n",
205 | "import tensorflow as tf\n",
206 | "import zipfile\n",
207 | "\n",
208 | "from collections import defaultdict\n",
209 | "from io import StringIO\n",
210 | "from matplotlib import pyplot as plt\n",
211 | "from PIL import Image\n",
212 | "from IPython.display import display"
213 | ]
214 | },
215 | {
216 | "cell_type": "markdown",
217 | "metadata": {
218 | "colab_type": "text",
219 | "id": "r5FNuiRPWKMN"
220 | },
221 | "source": [
222 | "Import the object detection module."
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": 6,
228 | "metadata": {
229 | "colab": {},
230 | "colab_type": "code",
231 | "id": "4-IMl4b6BdGO"
232 | },
233 | "outputs": [],
234 | "source": [
235 | "from object_detection.utils import ops as utils_ops\n",
236 | "from object_detection.utils import label_map_util\n",
237 | "from object_detection.utils import visualization_utils as vis_util"
238 | ]
239 | },
240 | {
241 | "cell_type": "markdown",
242 | "metadata": {
243 | "colab_type": "text",
244 | "id": "RYPCiag2iz_q"
245 | },
246 | "source": [
247 | "Patches:"
248 | ]
249 | },
250 | {
251 | "cell_type": "code",
252 | "execution_count": 7,
253 | "metadata": {
254 | "colab": {},
255 | "colab_type": "code",
256 | "id": "mF-YlMl8c_bM"
257 | },
258 | "outputs": [],
259 | "source": [
260 | "# patch tf1 into `utils.ops`\n",
261 | "utils_ops.tf = tf.compat.v1\n",
262 | "\n",
263 | "# Patch the location of gfile\n",
264 | "tf.gfile = tf.io.gfile"
265 | ]
266 | },
267 | {
268 | "cell_type": "markdown",
269 | "metadata": {
270 | "colab_type": "text",
271 | "id": "cfn_tRFOWKMO"
272 | },
273 | "source": [
274 | "# Model preparation "
275 | ]
276 | },
277 | {
278 | "cell_type": "markdown",
279 | "metadata": {
280 | "colab_type": "text",
281 | "id": "X_sEBLpVWKMQ"
282 | },
283 | "source": [
284 | "## Variables\n",
285 | "\n",
286 | "Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing the path.\n",
287 | "\n",
288 | "By default we use an \"SSD with Mobilenet\" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies."
289 | ]
290 | },
291 | {
292 | "cell_type": "markdown",
293 | "metadata": {
294 | "colab_type": "text",
295 | "id": "_1MVVTcLWKMW"
296 | },
297 | "source": [
298 | "## Loading label map\n",
299 | "Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine"
300 | ]
301 | },
302 | {
303 | "cell_type": "code",
304 | "execution_count": 13,
305 | "metadata": {
306 | "colab": {},
307 | "colab_type": "code",
308 | "id": "hDbpHkiWWKMX"
309 | },
310 | "outputs": [],
311 | "source": [
312 | "# List of the strings that is used to add correct label for each box.\n",
313 | "PATH_TO_LABELS = 'images\\labelmap.pbtxt'\n",
314 | "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)"
315 | ]
316 | },
317 | {
318 | "cell_type": "markdown",
319 | "metadata": {
320 | "colab_type": "text",
321 | "id": "oVU3U_J6IJVb"
322 | },
323 | "source": [
324 | "For the sake of simplicity we will test on 2 images:"
325 | ]
326 | },
327 | {
328 | "cell_type": "code",
329 | "execution_count": 14,
330 | "metadata": {
331 | "colab": {},
332 | "colab_type": "code",
333 | "id": "jG-zn5ykWKMd"
334 | },
335 | "outputs": [
336 | {
337 | "data": {
338 | "text/plain": [
339 | "[WindowsPath('object_detection/test_images/image1.jpg'),\n",
340 | " WindowsPath('object_detection/test_images/image2.jpg')]"
341 | ]
342 | },
343 | "execution_count": 14,
344 | "metadata": {},
345 | "output_type": "execute_result"
346 | }
347 | ],
348 | "source": [
349 | "import pathlib\n",
350 | "# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.\n",
351 | "PATH_TO_TEST_IMAGES_DIR = pathlib.Path('object_detection/test_images')\n",
352 | "TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob(\"*.jpg\")))\n",
353 | "TEST_IMAGE_PATHS"
354 | ]
355 | },
356 | {
357 | "cell_type": "markdown",
358 | "metadata": {
359 | "colab_type": "text",
360 | "id": "H0_1AGhrWKMc"
361 | },
362 | "source": [
363 | "# Detection"
364 | ]
365 | },
366 | {
367 | "cell_type": "markdown",
368 | "metadata": {
369 | "colab_type": "text",
370 | "id": "f7aOtOlebK7h"
371 | },
372 | "source": [
373 | "Load an object detection model:"
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": 17,
379 | "metadata": {
380 | "colab": {},
381 | "colab_type": "code",
382 | "id": "1XNT0wxybKR6"
383 | },
384 | "outputs": [
385 | {
386 | "name": "stdout",
387 | "output_type": "stream",
388 | "text": [
389 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
390 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
391 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
392 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
393 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
394 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
395 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
396 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
397 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
398 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
399 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
400 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
401 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
402 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
403 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
404 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
405 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
406 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
407 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
408 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
409 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
410 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
411 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
412 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_96002) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
413 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
414 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
415 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
416 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
417 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
418 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
419 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
420 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
421 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
422 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
423 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
424 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
425 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
426 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
427 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
428 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
429 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
430 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
431 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
432 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n"
433 | ]
434 | },
435 | {
436 | "name": "stdout",
437 | "output_type": "stream",
438 | "text": [
439 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
440 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
441 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
442 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_64913) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
443 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
444 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
445 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
446 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
447 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
448 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
449 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
450 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
451 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
452 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
453 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
454 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
455 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
456 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
457 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
458 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
459 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
460 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
461 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
462 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
463 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
464 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
465 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
466 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_92394) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
467 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
468 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
469 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
470 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
471 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
472 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
473 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
474 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
475 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
476 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
477 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
478 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
479 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
480 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
481 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
482 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n"
483 | ]
484 | },
485 | {
486 | "name": "stdout",
487 | "output_type": "stream",
488 | "text": [
489 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
490 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
491 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
492 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
493 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
494 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
495 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
496 | "WARNING:tensorflow:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_66533) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
497 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
498 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
499 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
500 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
501 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
502 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
503 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
504 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
505 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
506 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
507 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
508 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
509 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
510 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
511 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
512 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
513 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
514 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
515 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
516 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
517 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
518 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
519 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
520 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
521 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
522 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
523 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
524 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
525 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
526 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
527 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
528 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
529 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
530 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
531 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
532 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
533 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
534 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
535 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
536 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
537 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
538 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
539 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
540 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
541 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
542 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n"
543 | ]
544 | },
545 | {
546 | "name": "stdout",
547 | "output_type": "stream",
548 | "text": [
549 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
550 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
551 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
552 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
553 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
554 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
555 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
556 | "WARNING:tensorflow:Importing a function (__inference_call_func_21282) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
557 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
558 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
559 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
560 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
561 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
562 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
563 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
564 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
565 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
566 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
567 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
568 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
569 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
570 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
571 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
572 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
573 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
574 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
575 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
576 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
577 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
578 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
579 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
580 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_82781) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
581 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
582 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
583 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
584 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
585 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
586 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
587 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
588 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
589 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
590 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
591 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
592 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
593 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n"
594 | ]
595 | },
596 | {
597 | "name": "stdout",
598 | "output_type": "stream",
599 | "text": [
600 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
601 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
602 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
603 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
604 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
605 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
606 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
607 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
608 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
609 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n",
610 | "WARNING:tensorflow:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_86389) with ops with custom gradients. Will likely fail if a gradient is requested.\n"
611 | ]
612 | }
613 | ],
614 | "source": [
615 | "### change the location accordingly\n",
616 | "detection_model = tf.saved_model.load('inference_graph/saved_model')"
617 | ]
618 | },
619 | {
620 | "cell_type": "markdown",
621 | "metadata": {
622 | "colab_type": "text",
623 | "id": "yN1AYfAEJIGp"
624 | },
625 | "source": [
626 | "Check the model's input signature, it expects a batch of 3-color images of type uint8:"
627 | ]
628 | },
629 | {
630 | "cell_type": "code",
631 | "execution_count": 18,
632 | "metadata": {
633 | "colab": {},
634 | "colab_type": "code",
635 | "id": "ajmR_exWyN76"
636 | },
637 | "outputs": [],
638 | "source": [
639 | "def run_inference_for_single_image(model, image):\n",
640 | " image = np.asarray(image)\n",
641 | " # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.\n",
642 | " input_tensor = tf.convert_to_tensor(image)\n",
643 | " # The model expects a batch of images, so add an axis with `tf.newaxis`.\n",
644 | " input_tensor = input_tensor[tf.newaxis,...]\n",
645 | "\n",
646 | " # Run inference\n",
647 | " model_fn = model.signatures['serving_default']\n",
648 | " output_dict = model_fn(input_tensor)\n",
649 | "# print(output_dict)\n",
650 | " # All outputs are batches tensors.\n",
651 | " # Convert to numpy arrays, and take index [0] to remove the batch dimension.\n",
652 | " # We're only interested in the first num_detections.\n",
653 | " num_detections = int(output_dict.pop('num_detections'))\n",
654 | " output_dict = {key:value[0, :num_detections].numpy() \n",
655 | " for key,value in output_dict.items()}\n",
656 | " output_dict['num_detections'] = num_detections\n",
657 | "\n",
658 | " # detection_classes should be ints.\n",
659 | " output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)\n",
660 | "# print(output_dict['detection_classes'])\n",
661 | " # Handle models with masks:\n",
662 | " if 'detection_masks' in output_dict:\n",
663 | " # Reframe the the bbox mask to the image size.\n",
664 | " detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(\n",
665 | " output_dict['detection_masks'], output_dict['detection_boxes'],\n",
666 | " image.shape[0], image.shape[1]) \n",
667 | " detection_masks_reframed = tf.cast(detection_masks_reframed > 0.8,\n",
668 | " tf.uint8)\n",
669 | " output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()\n",
670 | " \n",
671 | " return output_dict\n",
672 | "\n",
673 | "\n",
674 | "def show_inference(model, image_np):\n",
675 | " # the array based representation of the image will be used later in order to prepare the\n",
676 | " # result image with boxes and labels on it.\n",
677 | "# image_np = np.array(Image.open(image_path))\n",
678 | " # Actual detection.\n",
679 | " output_dict = run_inference_for_single_image(model, image_np)\n",
680 | "\n",
681 | "# print(category_index)\n",
682 | " # Visualization of the results of a detection.\n",
683 | " final_img =vis_util.visualize_boxes_and_labels_on_image_array(\n",
684 | " image_np,\n",
685 | " output_dict['detection_boxes'],\n",
686 | " output_dict['detection_classes'],\n",
687 | " output_dict['detection_scores'],\n",
688 | " category_index,\n",
689 | " instance_masks=output_dict.get('detection_masks_reframed', None),\n",
690 | " use_normalized_coordinates=True,\n",
691 | " line_thickness=8)\n",
692 | " return(final_img)\n",
693 | "# display(Image.fromarray(image_np))"
694 | ]
695 | },
696 | {
697 | "cell_type": "markdown",
698 | "metadata": {
699 | "colab_type": "text",
700 | "id": "z1wq0LVyMRR_"
701 | },
702 | "source": [
703 | "Run it on each test image and show the results:"
704 | ]
705 | },
706 | {
707 | "cell_type": "code",
708 | "execution_count": 19,
709 | "metadata": {
710 | "colab": {},
711 | "colab_type": "code",
712 | "id": "DWh_1zz6aqxs"
713 | },
714 | "outputs": [],
715 | "source": [
716 | "import cv2\n",
717 | "\n",
718 | "cap = cv2.VideoCapture(0)\n",
719 | "\n",
720 | "while 1:\n",
721 | " _,img = cap.read()\n",
722 | " \n",
723 | " img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n",
724 | " final_img = show_inference(detection_model,img)\n",
725 | " \n",
726 | " final_img = cv2.cvtColor(final_img,cv2.COLOR_RGB2BGR)\n",
727 | "\n",
728 | " cv2.imshow('img',final_img)\n",
729 | "\n",
730 | "# cv2.imshow('img',img)\n",
731 | " if cv2.waitKey(1) == ord('q'):\n",
732 | " break\n",
733 | "\n",
734 | "cap.release()\n",
735 | "cv2.destroyAllWindows()\n"
736 | ]
737 | }
738 | ],
739 | "metadata": {
740 | "accelerator": "GPU",
741 | "colab": {
742 | "collapsed_sections": [],
743 | "last_runtime": {
744 | "build_target": "//learning/brain/python/client:colab_notebook",
745 | "kind": "private"
746 | },
747 | "name": "object_detection_tutorial.ipynb",
748 | "private_outputs": true,
749 | "provenance": [
750 | {
751 | "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/colab_tutorials/object_detection_tutorial.ipynb",
752 | "timestamp": 1594335690840
753 | },
754 | {
755 | "file_id": "1LNYL6Zsn9Xlil2CVNOTsgDZQSBKeOjCh",
756 | "timestamp": 1566498233247
757 | },
758 | {
759 | "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc",
760 | "timestamp": 1566488313397
761 | },
762 | {
763 | "file_id": "/piper/depot/google3/third_party/py/tensorflow_docs/g3doc/en/r2/tutorials/generative/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc",
764 | "timestamp": 1566145894046
765 | },
766 | {
767 | "file_id": "1nBPoWynOV0auSIy40eQcBIk9C6YRSkI8",
768 | "timestamp": 1566145841085
769 | },
770 | {
771 | "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc",
772 | "timestamp": 1556295408037
773 | },
774 | {
775 | "file_id": "1layerger-51XwWOwYMY_5zHaCavCeQkO",
776 | "timestamp": 1556214267924
777 | },
778 | {
779 | "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc",
780 | "timestamp": 1556207836484
781 | },
782 | {
783 | "file_id": "1w6mqQiNV3liPIX70NOgitOlDF1_4sRMw",
784 | "timestamp": 1556154824101
785 | },
786 | {
787 | "file_id": "https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb",
788 | "timestamp": 1556150293326
789 | }
790 | ]
791 | },
792 | "kernelspec": {
793 | "display_name": "Python 3.8.5 64-bit ('tf2.3': conda)",
794 | "language": "python",
795 | "name": "python38564bittf23conda3af7763b02884590a8a16b3e29b65cde"
796 | },
797 | "language_info": {
798 | "codemirror_mode": {
799 | "name": "ipython",
800 | "version": 3
801 | },
802 | "file_extension": ".py",
803 | "mimetype": "text/x-python",
804 | "name": "python",
805 | "nbconvert_exporter": "python",
806 | "pygments_lexer": "ipython3",
807 | "version": "3.8.5"
808 | }
809 | },
810 | "nbformat": 4,
811 | "nbformat_minor": 1
812 | }
813 |
--------------------------------------------------------------------------------
/xml_to_csv.py:
--------------------------------------------------------------------------------
1 | import os
2 | import glob
3 | import pandas as pd
4 | import xml.etree.ElementTree as ET
5 |
6 |
7 | def xml_to_csv(path):
8 | xml_list = []
9 | for xml_file in glob.glob(path + '/*.xml'):
10 | tree = ET.parse(xml_file)
11 | root = tree.getroot()
12 | for member in root.findall('object'):
13 | value = (root.find('filename').text,
14 | int(root.find('size')[0].text),
15 | int(root.find('size')[1].text),
16 | member[0].text,
17 | int(member[4][0].text),
18 | int(member[4][1].text),
19 | int(member[4][2].text),
20 | int(member[4][3].text)
21 | )
22 | xml_list.append(value)
23 | column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
24 | xml_df = pd.DataFrame(xml_list, columns=column_name)
25 | return xml_df
26 |
27 |
28 | def main():
29 | for folder in ['train','test']:
30 | image_path = os.path.join(os.getcwd(), ('images/' + folder))
31 | xml_df = xml_to_csv(image_path)
32 | xml_df.to_csv(('images/' + folder + '_labels.csv'), index=None)
33 | print('Successfully converted xml to csv.')
34 |
35 |
36 | main()
--------------------------------------------------------------------------------