You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since July 10, 2020 TensorFlow [announced that the Object Detection API officially supports TensorFlow 2](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html). Therefore, an updated version of the tutorial was created to cover TensorFlow 2.
Copy file name to clipboardExpand all lines: docs/source/auto_examples/object_detection_camera.ipynb
+8-8
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@
15
15
"cell_type": "markdown",
16
16
"metadata": {},
17
17
"source": [
18
-
"\nDetect Objects Using Your Webcam\n================================\n"
18
+
"\n# Detect Objects Using Your Webcam\n"
19
19
]
20
20
},
21
21
{
@@ -29,7 +29,7 @@
29
29
"cell_type": "markdown",
30
30
"metadata": {},
31
31
"source": [
32
-
"Create the data directory\n~~~~~~~~~~~~~~~~~~~~~~~~~\nThe snippet shown below will create the ``data`` directory where all our data will be stored. The\ncode will create a directory structure as shown bellow:\n\n.. code-block:: bash\n\n data\n\u2514\u2500\u2500 models\n\nwhere the ``models`` folder will will contain the downloaded models.\n\n"
32
+
"## Create the data directory\nThe snippet shown below will create the ``data`` directory where all our data will be stored. The\ncode will create a directory structure as shown bellow:\n\n.. code-block:: bash\n\n data\n\u2514\u2500\u2500 models\n\nwhere the ``models`` folder will will contain the downloaded models.\n\n"
33
33
]
34
34
},
35
35
{
@@ -47,7 +47,7 @@
47
47
"cell_type": "markdown",
48
48
"metadata": {},
49
49
"source": [
50
-
"Download the model\n~~~~~~~~~~~~~~~~~~\nThe code snippet shown below is used to download the object detection model checkpoint file,\nas well as the labels file (.pbtxt) which contains a list of strings used to add the correct\nlabel to each detection (e.g. person).\n\nThe particular detection algorithm we will use is the `SSD ResNet101 V1 FPN 640x640`. More\nmodels can be found in the `TensorFlow 2 Detection Model Zoo <https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md>`_.\nTo use a different model you will need the URL name of the specific model. This can be done as\nfollows:\n\n1. Right click on the `Model name` of the model you would like to use;\n2. Click on `Copy link address` to copy the download link of the model;\n3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;\n4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;\n5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.\n\nFor example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz``\n\n"
50
+
"## Download the model\nThe code snippet shown below is used to download the object detection model checkpoint file,\nas well as the labels file (.pbtxt) which contains a list of strings used to add the correct\nlabel to each detection (e.g. person).\n\nThe particular detection algorithm we will use is the `SSD ResNet101 V1 FPN 640x640`. More\nmodels can be found in the `TensorFlow 2 Detection Model Zoo <https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md>`_.\nTo use a different model you will need the URL name of the specific model. This can be done as\nfollows:\n\n1. Right click on the `Model name` of the model you would like to use;\n2. Click on `Copy link address` to copy the download link of the model;\n3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;\n4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;\n5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.\n\nFor example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz``\n\n"
51
51
]
52
52
},
53
53
{
@@ -65,7 +65,7 @@
65
65
"cell_type": "markdown",
66
66
"metadata": {},
67
67
"source": [
68
-
"Load the model\n~~~~~~~~~~~~~~\nNext we load the downloaded model\n\n"
68
+
"## Load the model\nNext we load the downloaded model\n\n"
69
69
]
70
70
},
71
71
{
@@ -83,7 +83,7 @@
83
83
"cell_type": "markdown",
84
84
"metadata": {},
85
85
"source": [
86
-
"Load label map data (for plotting)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nLabel maps correspond index numbers to category names, so that when our convolution network\npredicts `5`, we know that this corresponds to `airplane`. Here we use internal utility\nfunctions, but anything that returns a dictionary mapping integers to appropriate string labels\nwould be fine.\n\n"
86
+
"## Load label map data (for plotting)\nLabel maps correspond index numbers to category names, so that when our convolution network\npredicts `5`, we know that this corresponds to `airplane`. Here we use internal utility\nfunctions, but anything that returns a dictionary mapping integers to appropriate string labels\nwould be fine.\n\n"
87
87
]
88
88
},
89
89
{
@@ -101,7 +101,7 @@
101
101
"cell_type": "markdown",
102
102
"metadata": {},
103
103
"source": [
104
-
"Define the video stream\n~~~~~~~~~~~~~~~~~~~~~~~\nWe will use `OpenCV <https://pypi.org/project/opencv-python/>`_ to capture the video stream\ngenerated by our webcam. For more information you can refer to the `OpenCV-Python Tutorials <https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera>`_\n\n"
104
+
"## Define the video stream\nWe will use `OpenCV <https://pypi.org/project/opencv-python/>`_ to capture the video stream\ngenerated by our webcam. For more information you can refer to the `OpenCV-Python Tutorials <https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera>`_\n\n"
105
105
]
106
106
},
107
107
{
@@ -119,7 +119,7 @@
119
119
"cell_type": "markdown",
120
120
"metadata": {},
121
121
"source": [
122
-
"Putting everything together\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\nThe code shown below loads an image, runs it through the detection model and visualizes the\ndetection results, including the keypoints.\n\nNote that this will take a long time (several minutes) the first time you run this code due to\ntf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be\nfaster.\n\nHere are some simple things to try out if you are curious:\n\n* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).\n* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).\n* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.\n\n"
122
+
"## Putting everything together\nThe code shown below loads an image, runs it through the detection model and visualizes the\ndetection results, including the keypoints.\n\nNote that this will take a long time (several minutes) the first time you run this code due to\ntf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be\nfaster.\n\nHere are some simple things to try out if you are curious:\n\n* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).\n* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).\n* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.\n\n"
This demo will take you through the steps of running an "out-of-the-box" detection model to
16
27
detect objects in the video stream extracted from your camera.
17
28
29
+
.. GENERATED FROM PYTHON SOURCE LINES 13-24
30
+
18
31
Create the data directory
19
32
~~~~~~~~~~~~~~~~~~~~~~~~~
20
33
The snippet shown below will create the ``data`` directory where all our data will be stored. The
@@ -27,6 +40,7 @@ code will create a directory structure as shown bellow:
27
40
28
41
where the ``models`` folder will will contain the downloaded models.
29
42
43
+
.. GENERATED FROM PYTHON SOURCE LINES 24-32
30
44
31
45
.. code-block:: default
32
46
@@ -39,6 +53,8 @@ where the ``models`` folder will will contain the downloaded models.
39
53
os.mkdir(dir)
40
54
41
55
56
+
.. GENERATED FROM PYTHON SOURCE LINES 33-51
57
+
42
58
Download the model
43
59
~~~~~~~~~~~~~~~~~~
44
60
The code snippet shown below is used to download the object detection model checkpoint file,
@@ -58,6 +74,7 @@ follows:
58
74
59
75
For example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz``
60
76
77
+
.. GENERATED FROM PYTHON SOURCE LINES 51-82
61
78
62
79
.. code-block:: default
63
80
@@ -93,10 +110,13 @@ For example, the download link for the model used below is: ``download.tensorflo
93
110
print('Done')
94
111
95
112
113
+
.. GENERATED FROM PYTHON SOURCE LINES 83-86
114
+
96
115
Load the model
97
116
~~~~~~~~~~~~~~
98
117
Next we load the downloaded model
99
118
119
+
.. GENERATED FROM PYTHON SOURCE LINES 86-121
100
120
101
121
.. code-block:: default
102
122
@@ -136,25 +156,31 @@ Next we load the downloaded model
136
156
137
157
138
158
159
+
.. GENERATED FROM PYTHON SOURCE LINES 122-128
160
+
139
161
Load label map data (for plotting)
140
162
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141
163
Label maps correspond index numbers to category names, so that when our convolution network
142
164
predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility
143
165
functions, but anything that returns a dictionary mapping integers to appropriate string labels
We will use `OpenCV <https://pypi.org/project/opencv-python/>`_ to capture the video stream
156
181
generated by our webcam. For more information you can refer to the `OpenCV-Python Tutorials <https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera>`_
157
182
183
+
.. GENERATED FROM PYTHON SOURCE LINES 136-140
158
184
159
185
.. code-block:: default
160
186
@@ -163,6 +189,8 @@ generated by our webcam. For more information you can refer to the `OpenCV-Pytho
163
189
cap = cv2.VideoCapture(0)
164
190
165
191
192
+
.. GENERATED FROM PYTHON SOURCE LINES 141-155
193
+
166
194
Putting everything together
167
195
~~~~~~~~~~~~~~~~~~~~~~~~~~~
168
196
The code shown below loads an image, runs it through the detection model and visualizes the
@@ -178,6 +206,7 @@ Here are some simple things to try out if you are curious:
178
206
* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).
179
207
* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.
0 commit comments